GithubHelp home page GithubHelp logo

standardgalactic / httpmock-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexliesenfeld/httpmock

0.0 1.0 0.0 2.44 MB

HTTP mocking library for Rust.

License: MIT License

Rust 99.52% Dockerfile 0.48%

httpmock-1's Introduction

httpmock

HTTP mocking library for Rust.

Build Status codecov crates.io Mentioned in Awesome Rust

Documentation · Crate · Report Bug · Request Feature · Changelog

Features

Getting Started

Add httpmock to Cargo.toml:

[dev-dependencies]
httpmock = "0.6"

You can then use httpmock as follows:

use httpmock::prelude::*;

// Start a lightweight mock server.
let server = MockServer::start();

// Create a mock on the server.
let hello_mock = server.mock(|when, then| {
    when.method(GET)
        .path("/translate")
        .query_param("word", "hello");
    then.status(200)
        .header("content-type", "text/html; charset=UTF-8")
        .body("Привет");
});

// Send an HTTP request to the mock server. This simulates your code.
let response = isahc::get(server.url("/translate?word=hello")).unwrap();

// Ensure the specified mock was called exactly one time (or fail with a detailed error description).
hello_mock.assert();

// Ensure the mock server did respond as specified.
assert_eq!(response.status(), 200);

The above example will spin up a lightweight HTTP mock server and configure it to respond to all GET requests to path /translate with query parameter word=hello. The corresponding HTTP response will contain the text body Привет.

Usage

See the reference docs for detailed API documentation.

Examples

You can find examples in the httpmock test directory. The reference docs also contain a lot of examples. There is an online tutorial as well.

Standalone Mock Server

You can use httpmock to run a standalone mock server that is executed in a separate process. There is a Docker image available at Dockerhub to get started quickly.

The standalone mode allows you to mock HTTP based APIs for many API clients, not only the ones inside your Rust tests, but also completely different programs running on remote hosts. This is especially useful if you want to use httpmock in system or end-to-end tests that require mocked services (such as REST APIs, data stores, authentication providers, etc.).

Please refer to the docs for more information

File Based Mock Specification

For convenience, the standalone mode also allows you to use YAML files for mock specification, so you do not need to use Rust or any other programming language at all. The mock specification file schema is very similar to the httpmock Rust API, so it's easy to jump between the two. Please find an example mock specification file here.

Please refer to the docs for more information.

License

httpmock is free software: you can redistribute it and/or modify it under the terms of the MIT Public License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MIT Public License for more details.

httpmock-1's People

Contributors

alexliesenfeld avatar 95th avatar ivd-git avatar uebelandre avatar davidpdrsn avatar theredfish avatar korrat avatar mythmon avatar incker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.