HTTP mocking library for Rust.

Overview

httpmock

HTTP mocking library for Rust.

Build Status codecov crates.io Rust License

Documentation · Crate · Report Bug · Request Feature · Changelog

Features

  • Simple, expressive, fluent API.
  • Many built-in helpers for easy request matching.
  • Parallel test execution.
  • Extensible request matching.
  • Fully asynchronous core with synchronous and asynchronous APIs.
  • Debugging support.
  • Network delay simulation.
  • Standalone mode with an accompanying Docker image.
  • Support for Regex matching, JSON, serde, cookies, and more.

Getting Started

Add httpmock to Cargo.toml:

[dev-dependencies]
httpmock = "0.5.5"

You can then use httpmock as follows:

use httpmock::MockServer;
use httpmock::Method::GET;

// 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.
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.

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.

Comments
  • Panic: Cannot create absolute path from string

    Panic: Cannot create absolute path from string

    Whenever I try to run my test in a debugger, I get the following error

    running 1 test
    thread 'metadata::tests::test_fetching_src' panicked at 'Cannot create absolute path from string '/var/folders/hf/phjl9q7501gb5wt_qr4ltn3m0000gn/T/.tmpIrQDOX/fake-crate.tar.gz': "environment variable not found"', /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/api/mock.rs:1115:63
    stack backtrace:
       0: rust_begin_unwind
                 at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/std/src/panicking.rs:475
       1: core::panicking::panic_fmt
                 at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/panicking.rs:85
       2: core::option::expect_none_failed
                 at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/option.rs:1221
       3: core::result::Result<T,E>::expect
                 at /Users/user/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:933
       4: httpmock::api::mock::Mock::return_body_from_file
                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/api/mock.rs:1115
       5: httpmock::Responders::body_from_file
                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/lib.rs:1185
       6: cargo_raze::metadata::tests::test_fetching_src::{{closure}}
                 at ./src/metadata.rs:389
       7: httpmock::MockServer::mock_async::{{closure}}
                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/lib.rs:481
       8: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
                 at /Users/user/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/future/mod.rs:79
       9: <F as httpmock::util::Join>::join
                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/util.rs:71
      10: httpmock::MockServer::mock
                 at /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/httpmock-0.5.0-beta.1/src/lib.rs:456
      11: cargo_raze::metadata::tests::test_fetching_src
                 at ./src/metadata.rs:385
      12: cargo_raze::metadata::tests::test_fetching_src::{{closure}}
                 at ./src/metadata.rs:342
      13: core::ops::function::FnOnce::call_once
                 at /Users/user/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:227
      14: core::ops::function::FnOnce::call_once
                 at /rustc/18bf6b4f01a6feaf7259ba7cdae58031af1b7b39/library/core/src/ops/function.rs:227
    note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
    

    Repro

    I created an example workspace that outlines this issue. repro.zip

    I'm using Vscode and by simply clicking the debug button that appears over tests from the rust-analyzer plugin, I hit this panic.

    I'm using 0.5.0-beta.1.

    opened by UebelAndre 10
  • Mocking multipart uploads

    Mocking multipart uploads

    It seems the library fails on multipart/form-data uploads. It works fine with regular requests with json body.

    Requests to real server upload files without any issues. I'm using ureq as HTTP client:

        fn request_with_form_data<T1: serde::ser::Serialize, T2: serde::de::DeserializeOwned>(
            &self,
            method: &str,
            params: T1,
            parameter_name: &str,
            file_path: PathBuf,
        ) -> Result<T2, ureq::Error> {
            let json_string = serde_json::to_string(&params).unwrap();
            let json_struct: Value = serde_json::from_str(&json_string).unwrap();
    
            let mut form = Multipart::new();
            for (key, val) in json_struct.as_object().unwrap().iter() {
                if key != parameter_name {
                    let val = match val {
                        &Value::String(ref val) => format!("{}", val),
                        etc => format!("{}", etc),
                    };
    
                    form.add_text(key, val);
                }
            }
    
            let file = std::fs::File::open(&file_path).unwrap();
            let file_extension = file_path.extension().and_then(|s| s.to_str()).unwrap_or("");
            let mime = mime_guess::from_ext(&file_extension).first_or_octet_stream();
    
            form.add_stream(
                parameter_name,
                file,
                file_path.file_name().unwrap().to_str(),
                Some(mime),
            );
    
            let url = format!("{}/{}", self.api_url, method);
            let form_data = form.prepare().unwrap();
    
            let response = ureq::post(&url)
                .set(
                    "Content-Type",
                    &format!("multipart/form-data; boundary={}", form_data.boundary()),
                )
                .send(form_data)?;
    
            let parsed_response: T2 = serde_json::from_reader(response.into_reader()).unwrap();
    
            Ok(parsed_response)
        }
    

    mock:

            let server = MockServer::start();
    
            server.mock(|when, then| {
                when.method(POST).path(path);
                then.status(200).body(response);
            });
    
    opened by ayrat555 6
  • Inconvenient MockRef<'a> API

    Inconvenient MockRef<'a> API

    Hi,

    I'm trying to write a non-trivial mock server. To do so I'd need to store both the MockServer object and a bunch of MockRef<'a> in the same struct (the one representing my mock server). Unfortunately it's pretty hard to do as MockRef is itself keeping a ref on Server, introducing the well-known self-referential problem.

    It is possible to workaround it with tricks such as rental or owning_ref but it greatly increase the code complexity. An easier approach would be to remove the server borrow in MockRef by wrapping it around a Rc or Arc. Would you be opened to such change?

    opened by gdesmott 6
  • WARN  httpmock::server::matchers::targets] Cannot parse json value: EOF while parsing a value at line 1 column 0

    WARN httpmock::server::matchers::targets] Cannot parse json value: EOF while parsing a value at line 1 column 0

    If I set up a mock for a GET request:

        pub async fn provision_api(&self) {
            let _get_colo_info = self
                .server
                .mock_async(|when, then| {
                    when.method("GET");
                    then.body("hi").status(200);
                })
                .await;
        }
        ```
    Then send a GET using reqwest:
        ```
        let body = reqwest::get()?
        .text()?;
        ```
    I see:
    `WARN  httpmock::server::matchers::targets] Cannot parse json value: EOF while parsing a value at line 1 column 0`
    It appears this is because it thinks there is a body on the GET request and tries to parse it has json.  Everything appears to work as intended except for the warning prints.
    opened by sbohrer 5
  • Issues with hard pinning dependencies

    Issues with hard pinning dependencies

    Can you actually not hard pin everything? I didn't realize what affect this would have downstream when you first mentioned it in #21

    error: failed to select a version for `regex`.
        ... required by package `httpmock v0.5.1`
        ... which is depended on by `cargo-raze v0.6.1 (/Users/user/Code/cargo-raze/impl)`
    versions that meet the requirements `=1.3.9` are: 1.3.9
    
    all possible versions conflict with previously selected packages.
    
      previously selected package `regex v1.4.1`
        ... which is depended on by `cargo-clone-crate v0.1.4`
        ... which is depended on by `cargo-raze v0.6.1 (/Users/user/Code/cargo-raze/impl)`
    
    
    opened by UebelAndre 5
  • Build error caused by `futures-util=0.3.7`

    Build error caused by `futures-util=0.3.7`

    I feel like futures-rs messed up by making that update a patch version change only but alas, It would be great to have this fixed.

    httpmock % cargo test
    
       Compiling httpmock v0.5.1-alpha.0 (/Users/user/Code/httpmock)
    error[E0433]: failed to resolve: could not find `core_reexport` in `futures_util`
       --> src/lib.rs:202:19
        |
    202 | use futures_util::core_reexport::time::Duration;
        |                   ^^^^^^^^^^^^^ could not find `core_reexport` in `futures_util`
    
    error[E0433]: failed to resolve: could not find `core_reexport` in `futures_util`
       --> src/lib.rs:202:19
        |
    202 | use futures_util::core_reexport::time::Duration;
        |                   ^^^^^^^^^^^^^ could not find `core_reexport` in `futures_util`
    
    error[E0412]: cannot find type `Duration` in this scope
        --> src/lib.rs:1407:26
         |
    1407 |     pub fn delay<D: Into<Duration>>(self, duration: D) -> Self {
         |                          ^^^^^^^^ not found in this scope
         |
    help: consider importing one of these items
         |
    196  | use core::time::Duration;
         |
    196  | use crate::data::Duration;
         |
    196  | use std::time::Duration;
         |
    196  | use tokio::time::Duration;
         |
    
    error[E0412]: cannot find type `Duration` in this scope
        --> src/lib.rs:1407:26
         |
    1407 |     pub fn delay<D: Into<Duration>>(self, duration: D) -> Self {
         |                          ^^^^^^^^ not found in this scope
         |
    help: consider importing one of these items
         |
    196  | use core::time::Duration;
         |
    196  | use crate::data::Duration;
         |
    196  | use std::time::Duration;
         |
    196  | use tokio::time::Duration;
         |
    
    error: aborting due to 2 previous errors
    
    Some errors have detailed explanations: E0412, E0433.
    For more information about an error, try `rustc --explain E0412`.
    error: could not compile `httpmock`.
    
    To learn more, run the command again with --verbose.
    warning: build failed, waiting for other jobs to finish...
    error: aborting due to 2 previous errors
    
    Some errors have detailed explanations: E0412, E0433.
    For more information about an error, try `rustc --explain E0412`.
    error: build failed
    

    I also noticed a BUNCH of packages are out of date. Do you think it'd be possible to update them and as a check for any lingering uses of "deprecated" code?

    opened by UebelAndre 5
  • Improve httpmock's standalone capabilities

    Improve httpmock's standalone capabilities

    This PR does a couple of things:

    • Allow for keeping mocks after runs by setting the HTTPMOCK_KEEP_MOCKS variable
    • Allow for serving all existing mocks by accessing __mocks/
    opened by flaper87 5
  • Multiple Mocks isn't working as expected

    Multiple Mocks isn't working as expected

    I'm trying to use the library to test some code that uses a Google API. The endpoint will return a pagination token in each response until there are no more resources to fetch. I was trying to test some code to see if the pagination was working and came across this unexpected behavior. In my test, I have two mocks with the same path, but different query string parameters -- here is a simplified version.

    #[test]
    fn test_different_qs_responses() -> Result<(), Box<dyn std::error::Error>> {
        let server = MockServer::start();
    
        let mock_first = server.mock(|when, then| {
            when.method(GET)
                .path("/v1/mediaItems")
                .query_param("pageSize", "3");
            then.status(200)
                .header("Content-Type", "application/json")
                .json_body(json!({
                    "mediaItems": [],
                    "nextPageToken": "the_next_page"
                    }));
        });
    
        let mock_last = server.mock(|when, then| {
            when.method(GET)
                .path("/v1/mediaItems")
                .query_param("pageSize", "3")
                .query_param("pageToken", "the_next_page");
            then.status(200)
                .header("Content-Type", "application/json")
                .json_body(json!({
                    "mediaItems": [],
                    }));
        });
    
        let client = reqwest::blocking::Client::new();
        let mut query = vec![("pageSize", "3")];
        
        // first
        client.get(&server.url("/v1/mediaItems"))
              .query(&query)
              .send()?;
        
        query.push(("pageToken", "the_next_page"));
    
        // last
        client.get(&server.url("/v1/mediaItems"))
        .query(&query)
        .send()?;
        
        mock_first.assert();
        mock_last.assert();
        Ok(())
    }
    

    I'd expect mock_first to match the first request and mock_last to match the last since the query parameters specified in the match and in each request are different, but that doesn't appear to be the case. When I run the code I get this error:

    thread 'test_different_qs_responses' panicked at 'assertion failed: `(left == right)`
      left: `2`,
     right: `1`: The number of matching requests was higher than expected (expected 1 but was 2)', C:\Users\likar\.cargo\registry\src\github.com-1ecc6299db9ec823\httpmock-0.6.5\src\api\mock.rs:207:13
    

    Do I have a misunderstanding of how multiple mocks can be used or is this a bug in how mocks match requests?

    opened by ttiimm 3
  • Handle url encoding in query param match

    Handle url encoding in query param match

    Hi, first of all, thanks for the great library. We found an issue with query_param. According to the documentation, it's not necessary to encode the query value.

    Sets a query parameter that needs to be provided. Attention!: The request query keys and values are implicitly allowed, but is not required to be urlencoded! The value you pass here, however, must be in plain text (i.e. not encoded)!

    In order to match, we need to replace spaces with +. I'd expect that this library can handle it for me so I don't need to care about encoding internals.

    Reference: https://url.spec.whatwg.org/#url-parsing (spaceAsPlus)

    let m = server.mock(|when, then|{
        when.query_param("query", "Metallica is cool".replace(" ", "+"));
        then.status(200);
    });
    
    let req = hyper::Request::builder()
        .method(Method::GET)
        .uri(
            Url::parse_with_params(
                "http://example.com/",
                &[
                    ("query", "Metallica is cool")
                ],
            )
            .unwrap()
            .to_string(),
        )
        .header("content-type", "application/json")
        .body(Body::empty())
        .unwrap();
    
    bug 
    opened by StarpTech 3
  • FR: Support for tokio 1.0

    FR: Support for tokio 1.0

    I've done very little investigation here so I don't know the scope of this request but it'd be nice to update the version of tokio used here to help shrink my build graph. Hopefully this is as easy as changing the number 😅

    opened by UebelAndre 3
  • Redirecting function's requests to mock server

    Redirecting function's requests to mock server

    Hi, I want to be able to test a function that does not have the server's url as one of it's arguments. For example - if I have a function like eval_code(code: String) which uses (directly or through yet another function) e.g reqwests POST requests in its body, and let's say the requests are being sent to https://example.com/endpoint - then I want to be able to redirect those requests to the mock server in my tests, automatically. So theoretically what I want to have is define a mock server on some real address - and then, each time a request/response is being sent to/from that address it will automatically be redirected to my mock server, allowing me to test the functions that interact with that address. I hope my explanation was clear enough, and maybe that's even possible right now - I just couldn't find a way to do it.

    opened by oren0e 2
  • Simulate server unreachable

    Simulate server unreachable

    I was wondering whether it is currently possible to simulate a MockServer being unreachable so client functionality that deals with such issues can be tested.

    I can kind of replicate this by just starting the server whenever I'd like it to be reachable, but stopping it and starting it again using the same address / port is what seems tricky.

    opened by sr-gi 3
  • Share variables between requests and responses

    Share variables between requests and responses

    I've noticed something missing that would be, IMHO, pretty useful for testing external clients agains mocked servers.

    That's sharing variables between requests and responses. For instance, let's say we have a client posting some JSON data to a server, and the server may reply based on that data. The when clause should be able to expose the request data so the then clase can read it and act accordingly.

    enhancement 
    opened by sr-gi 1
  • Add features to reduce dependencies

    Add features to reduce dependencies

    With my team we are evaluating httpmock; it really looks like a great project. Anyway, our test compile-time increased substantially due to its number of dependencies. Would it maybe be possible to reduce the dependencies using cargo features? For example, I guess isahc and some other "client-side" libraries are used only with a remote server; could they be made optional and enabled with a remote feature?

    enhancement 
    opened by ufoscout 4
  • RFC: Portability/Relative Path - Then body_from_file Into type String

    RFC: Portability/Relative Path - Then body_from_file Into type String

    Error and/or Documentation Error and/or Relative Path Handling Error

    I was using a relative path for the fn call directly per doc example for Then::body_from_file

    But environment variable not found error popped up instead:

    Cannot create absolute path from string 'x':  "environment variable not found"
    

    I tried to look whether the doc said much about environment variables but nothing to evaluate.

    However Looking into I can find the call to crate::util::get_test_resource_file_path() when path.is_absolute is false:

    match env::var("CARGO_MANIFEST_DIR") {
    

    Problem is this env may not be visible to cargo sub-commands as I found the hard way with tarpaulin:

    env RUN_MODE=development cargo +nightly tarpaulin --run-types Tests,Doctests,Benchmarks,Examples,Lib,Bins -v
    
    • Works okay with cargo test but tarpaulin sub-command seems to omit that environment variable.
    • Omitting the env seems to be the case with cargo sub-command.
    • Cargo documentation only lists one env passed down without explicitly ruling out others.

    Doc wise Mock::return_body_from_file says explicitly either relative/absolute:

    resource_file_path - The file path to the file with the response body content. The file path can either be absolute or relative to the project root directory.
    

    As for relative path definition, I would expect this to be the OsStr<> from the Current working directory (CWD) which may be set to be elsewhere than the directory holding the cargo manifest.

    For backwards compatibility the document probably should say that the environment variable is used for relative path or behavior could be changed some way to rely on CWD that would break things relying on it to be the manifest path

    Plus CWD typically is not UTF-8 guaranteed path if Into is relied (not absolutely sure whether CARGO_MANIFEST_DIR is guaranteed to be either for lossless conversion Into either if it's allowed to be non UTF-8 :1234: ?)

    I personally use this pattern and document the var_os OssStr returning environment variable use for my app:

      let mut path_locator = match env::var_os("APP_CONFIG_PATH") {
          Some(val) => PathBuf::from(val),
          None => env::current_dir().unwrap()
    };
    

    If I feel I need to enforce UTF-8 paths like Rust ecosystem like cargo does I use camino Utf8Path to see if I can upgrade from Path and match appropriate error.

    Happy to submit a PR to fix doc and automatically check the truth whatever is decided when/if there is a decision what to do with this.

    Code Blocks Involved

    Then::body_from_file(httpmock/0.5.8/source/src/lib.rs)

    pub fn body_from_file<S: Into<String>>(self, body: S) -> Self {
            self.mock.set(self.mock.take().return_body_from_file(body));
    

    Mock::return_body_from_file(httpmock/0.5.8/source/src/api/mock.rs):around 1317

     pub fn return_body_from_file<S: Into<String>>(mut self, resource_file_path: S) -> Self {
            let resource_file_path = resource_file_path.into();
            let path = Path::new(&resource_file_path);
            let absolute_path = match path.is_absolute() {
                true => path.to_path_buf(),
                false => get_test_resource_file_path(&resource_file_path).expect(&format!(
                    "Cannot create absolute path from string '{}'",
         //--snip--
    

    Dilemma

    • Documentation advertises/promises both the relative and absolute path use
    • Relative path is implicitly derived from documentation as current working directory (CWD)
    • If relative path is used CARGO_MANIFEST_DIR is used as base path which may or may not be lossless Into
    • Cargo may or may not pass this env down to sub-command
    • std::String is always UTF-8
    • std::ffi::OsString implements Into String type but is lossy and breaks relative path guarantee
    • OsString and Path is supposed to work everywhere safely abstracting it
    • Environmental variable etc. can be non-UTF8 and requires appropriate handling boilerplate before it hits Then::body_from_file
    • Lossy conversion is due to POSIX filenames allowing anything except \0
    • Requires conversion to String for the whole path (if I read the code right:ok_woman:)
    • String/std::path::Path conversion has been always a pain
    • Many OS allows wide array of bytes in OsStr

    Solution 1: Allow From &std::path::Path without using CARGO env

    Pros:

    • Straightforward pattern is to pass std::path::Path directly e.g. with config::from
    • Allows relative path handling at ease over "somebody else handles it" abstraction
    • Library already uses std::Path internally despite Into String
    • Library using this can handle easily cross-platform TEST_DATA path which may contain "" or "/" path separators etc.
    • The world is not perfect problem is easier - less friction to use the library esp re: relative path
    • Documentation is the source of truth, though implicit Type impl loses (String is only UTF-8)
    • Doesn't rely on cargo env

    Cons:

    • Rust configured paths are UTF-8
    • Lossy Display/Debug
    • Either confusing implementation if String is still allowed
    • Libs using may/will break if on same method - needs new method
    • Current working directory derived from var_os needs to be used instead of CARGO env

    Example is using config::File::from

    impl<'a> From<&'a Path> for File<source::file::FileSourceFile> {
        fn from(path: &'a Path) -> Self {
            File {
                source: source::file::FileSourceFile::new(path.to_path_buf()),
                //--snip--
    

    Where I can easily construct and pass the full Path natively and use relative path if I want

    let mut path_locator = match env::var_os("CONFIG_PATH") {
      Some(val) => PathBuf::from(val),
      None => env::current_dir().unwrap()
    };
    
    path_locator.push("subdir");
    
    // And then finally merge the file I want with full POSIX compatible Path
    
    s.merge(File::from(path_locator.join("default")).required(false))?;
    
    

    Solution 2: Enforce and be explicit about UTF-8 and handle Error thru camino

    Pros:

    • Proper taint check and error
    • Lossless Display/Debug
    • Less friction for anyone who uses the library as it's checked for validity
    • Does not need new method as it was always UTF-8
    • Document implicit Type impl wins to some degree (String is only UTF-8 and we are just enforcing that)

    Cons:

    • Relative path use is a pattern sadly good or bad.
    • Friction for anyone who uses the library
    • Library using this have to handle cross platform paths for TEST_DATA path which may contain "" or "/" path separators etc.
    • Adds camino crate that handles sanity on Utf8Paths
    • Documentation which advertised flexibility between relative/absolute loses

    Solution 3: New fn pass by Buffer/Stream/Channel oneshot etc.

    Pros:

    • Many async libs just wait-read the whole buffer internally and then process/pass the whole of it
    • Would allow timing and replaying a server behaviour in timeline basis for writing into kernel buffer out
    • Implementation could say replay sequence from libpcap file the near timing packets were either sent/recvd
    • Could differentiate between Content-Length, gzip stream, chunked modes
    • WebSocket etc. streaming
    • Can serve as building block for HTTP/2 or gRPC/Protobuffers
    • More fun than simple singular delay :)

    Cons:

    • Library using this have to handle cross platform paths for TEST_DATA path which may contain "" or "/" path separators etc.
    • Library was never intended for buffer/stream handling?
    • Complexity

    Solution 4: Status quo

    Pros:

    • Supposed to have less burden for library

    Cons:

    • Friction to use the library
    • Library using this have to handle cross platform paths for TEST_DATA path which may contain "" or "/" path separators etc.
    • Misleading error (library is not using environment variable, the full path is passed to it)
    • Still using lossy Display/Debug as it is using Path internally

    Solution is probably somekind combination?

    I usually do both integration / units along my /// docs or even on README.md that gets included in test run and tarpaulin --run-types DocTests takes those into account

    #[cfg(doctest)]
    doc_comment::doctest!("../README.md");
    

    I will check with tarpaulin too to expose the environment var but it would be nice to get some clarity as it caused a bit confusion initially for me and not sure if there is anyone doing things without cargo env :)

    • pinkforest(she/her/hers)
    enhancement 
    opened by pinkforest 0
  • Asserting the absence of body, headers, etc

    Asserting the absence of body, headers, etc

    I would like to assert that my HTTP request body is empty or that a certain header doesn't exist. Is this something that httpmock supports at the moment?

    Currently, I can see there the following functions exist:

    • header_exists()
    • query_params_exists()
    • cookie_exists()

    It would be nice if an opposite version of them existed plus one for checking if the body is empty.

    enhancement 
    opened by ducaale 5
Releases(v0.6.7)
  • v0.6.7(Nov 21, 2022)

  • v0.6.6(Jan 11, 2022)

    • Extended some API methods to allow for more type flexibility (see https://github.com/alexliesenfeld/httpmock/issues/58). Thanks to @95th for providing the PR!
    • Fixed parsing query parameter values that contain + to represent space (see https://github.com/alexliesenfeld/httpmock/issues/56). Thanks to @95th for providing the PR!
    • Added a new Cargo feature cookie to shorten compile time (see https://github.com/alexliesenfeld/httpmock/pull/63). Thanks to mythmon for providing this PR!
    Source code(tar.gz)
    Source code(zip)
  • v0.6.5(Dec 22, 2021)

    • Fixes a race condition that could occur when deleting mocks from the mock server (see https://github.com/alexliesenfeld/httpmock/issues/53).
    • Replaced internal diff library (switched from difference to similar, see https://github.com/alexliesenfeld/httpmock/pull/55).
    Source code(tar.gz)
    Source code(zip)
  • v0.6.4(Nov 15, 2021)

  • v0.6.3(Nov 15, 2021)

    • This is a maintenance release that updates all dependencies to the most recent version.
    • Bumped minimum Rust version to 1.54 due to transitive dependency.
    Source code(tar.gz)
    Source code(zip)
  • v0.6.2(Aug 13, 2021)

    A bug was fixed that has unexported the When and Then structures. Both types are now exported again. Please refer to https://github.com/alexliesenfeld/httpmock/issues/47 for more info.

    Source code(tar.gz)
    Source code(zip)
  • v0.6.1(Aug 11, 2021)

  • v0.6.0(Aug 11, 2021)

    General

    • Old Mock structure based API was deprecated starting from version 0.5.0 and was removed with this version. Please switch to the new API based on the When / Then structures.
    • The two methods MockRef::times_called and MockRef::times_called_async were deprecated since version 0.5.0 and have now been removed.
    • A prelude module was added to shorten imports that are usually required when using httpmock in tests.
    • The struct MockRef has been renamed to Mock.
    • Trait MockRefExt has been renamed to MockExt.
    • Added support for x-www-form-urlencoded request bodies.

    Standalone Mock Server

    • Standalone server now has a request history limit that can be adjusted.
    • All standalone servers parameters now have an environment variable fallback.
    • Standalone servers exposed and disable_access_log parameters were changed, so that they now require a value in addition to the flag itself (this is due to a limitation of structopt/clap): Before: httpmock --expose, Now: httpmock --expose true.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.8(Apr 18, 2021)

  • v0.5.7(Mar 29, 2021)

    • Added static mock support based on YAML files for standalone mode.
    • Dockerfile Rust version has been fixed.
    • Bumped minimum Rust version to 1.46 due to transitive dependency.
    • Documentation on query parameters has been enhanced.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.6(Mar 12, 2021)

    • A bug has been fixed that caused false positive warnings in the log output.
    • Updated all dependencies to the most recent versions.
    • Assertion error messages (MockRef::assert and MockRef::assert_hits) now contain more details.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.5(Jan 28, 2021)

  • v0.5.4(Jan 15, 2021)

  • v0.5.3(Jan 7, 2021)

    • This is a maintenance release that updates all dependencies to the most recent version.
    • This release bumps the minimal Rust version from 1.43+ to 1.45+.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.2(Oct 25, 2020)

    • Updated dependencies to newest version.
    • Removed dependency version fixation from v0.5.1.
    • Mock::return_body_from_file and Then::body_from_file now accept absolute and relative file paths.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.1(Oct 24, 2020)

    • Updated dependency version of futures-util to fix compile errors
    • Fixed all dependency version numbers to avoid future problems with new dependency version releases.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.0(Oct 12, 2020)

    • Breaking Change: Function Mock::expect_json_body was renamed to expect_json_body_obj.
    • Breaking Change: Function Mock::return_json_body was renamed to return_json_body_obj.
    • 🚀 Attention: A new API for mock definition was added. The old API is still available and functional, but is deprecated from now on. Please consider switching to the new API.
    • 🚀 Attention: The following new assertion functions have been added that will provide you smart and helpful error output to support debugging:
      • MockRef::assert
      • MockRef::assert_hits
      • MockRef::assert_async
      • MockRef::assert_hits_async
    • The two methods MockRef::times_called and MockRef::times_called_async are now deprecated. Consider using MockRef::hits and MockRef::hits_async.
    • The two methods Mock::return_body and Then::body now accept binary content.
    • The following new methods accept a serde_json::Value:
      • Mock::expect_json_body
      • Mock::return_json_body
      • When::json_body
      • Then::json_body
    • 🔥 Improved documentation (a lot!).
    • 👏 Debug log output is now pretty printed!
    • 🍪 Cookie matching support.
    • Support for convenient temporary and permanent redirect.
    • The log level of some log messages was changed from debug to trace to make debugging easier.
    Source code(tar.gz)
    Source code(zip)
  • v0.5.0-beta.1(Oct 9, 2020)

Owner
Alexander Liesenfeld
Alexander Liesenfeld
Struct mocking library for Rust

faux   A library to create mocks out of structs. faux allows you to mock the methods of structs for testing without complicating or polluting your cod

Andres 322 Dec 20, 2022
A structure-aware HTTP fuzzing library

?? FeroxFuzz ?? A structure-aware HTTP fuzzing library ?? Another ferox? why? ?? Chill, it's not another command-line tool, this one's a library! ?? M

epi 141 Dec 27, 2022
Drill is an HTTP load testing application written in Rust inspired by Ansible syntax

Drill is an HTTP load testing application written in Rust inspired by Ansible syntax

Ferran Basora 1.5k Jan 1, 2023
Verdun is a HTTP stress-test/benchmark tool written in Rust.

Verdun is a HTTP stress-test/benchmark tool written in Rust. ?? It supports testing a single URL, loading multiples URLs from a file or automatically navigating a website (auto discovery)

Alex Hortopan 2 Feb 23, 2022
hb is an endpoint focused HTTP load testing / benchmark tool.

hb hb is an endpoint focused HTTP load testing / benchmark tool. Description The goal of hb is to provide a simple, robust tool to apply load against

Mark Pritchard 2 Aug 23, 2022
Playwright is a rust library to automate Chromium, Firefox and WebKit built on top of Node.js library.

?? Playwright for Rust Playwright is a rust library to automate Chromium, Firefox and WebKit built on top of Node.js library. Installation [dependenci

octaltree 132 Jan 6, 2023
A library for generating fake data in Rust.

Fake A Rust library for generating fake data. Installation Default (rand is required): [dependencies] fake = "2.4" rand = "0.8" If you want to use #[d

cksac 552 Dec 25, 2022
Advanced Fuzzing Library - Slot your Fuzzer together in Rust! Scales across cores and machines. For Windows, Android, MacOS, Linux, no_std, ...

LibAFL, the fuzzer library. Advanced Fuzzing Library - Slot your own fuzzers together and extend their features using Rust. LibAFL is written and main

Advanced Fuzzing League ++ 1.2k Dec 29, 2022
QuickCheck bug hunting in Rust standard library data structures

BugHunt, Rust This project is aiming to provide "stateful" QuickCheck models for Rust's standard library. That is, we build up a random list of operat

Brian L. Troutwine 161 Dec 15, 2022
Rust testing library

K9 - Rust Testing Library Snapshot testing + better assertions Available test macros snapshot assert_equal assert_greater_than assert_greater_than_or_

Aaron Abramov 269 Dec 10, 2022
beaver is a library for setting up Rust objects inspired by factory_bot.

beaver is a library for setting up Rust objects inspired by factory_bot. Usage | Examples | Docs Dependencies [dependenci

Takayuki Maeda 40 Sep 6, 2022
Rustress - stress testing library in Rust. For fun

rustress Simple network stress testing library. To get familiar with Rust Planned features (Subject to change) Multithreaded client/server Throughput

Hakan Sönmez 7 Sep 22, 2022
insta: a snapshot testing library for Rust

insta: a snapshot testing library for Rust Introduction Snapshots tests (also sometimes called approval tests) are tests that assert values against a

Armin Ronacher 1.4k Jan 1, 2023
`mutatis` is a library for writing custom, structure-aware test-case mutators for fuzzers in Rust.

mutatis Easily create custom, structure-aware mutators for fuzzing. Repository | Docs | Guide | Contributing About The most popular fuzzers — includin

Nick Fitzgerald 4 Aug 19, 2024
Simple assertion library for unit testing in python with a fluent API

Simple assertions library for unit testing in Python with a nice fluent API. Supports both Python 2 and 3.

snakedye 19 Sep 10, 2022
A tiny, super simple and portable benchmarking library.

benchmark-simple A tiny benchmarking library for Rust. Trivial to use Works pretty much everywhere, including WebAssembly (WASI, but also in-browser)

Frank Denis 3 Dec 26, 2022
This is a tiny (but delightful!) utility library for exhaustive testing.

Exhaustigen This is a tiny (but delightful!) utility library for exhaustive testing. It is based (directly) on the idea and code in the following blog

Graydon Hoare 34 Dec 14, 2022
A minimalist property-based testing library based on the arbitrary crate.

A minimalist property-based testing library based on the arbitrary crate.

Aleksey Kladov 61 Dec 21, 2022
An unofficial client library for the fuzz-introspector API.

fuzz-introspector-client An unofficial client library for the fuzz-introspector API. Quickstart Add package as a dependency; cargo add fuzz-introspect

Nathaniel Brough 4 Nov 25, 2023