alexliesenfeld / httpmock

HTTP mocking library for Rust.
MIT License
436 stars 40 forks source link

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

Closed sbohrer closed 3 years ago

sbohrer commented 3 years ago

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.

alexliesenfeld commented 3 years ago

Hmm, interesting. I'll look into it in the next few days. Thanks for reporting!

alexliesenfeld commented 3 years ago

Unfortunately I could not reproduce this, neither in tests nor in standalone mode. On a first sight, the warning comes from this code line though. Could you please give me a little more information about the context of how you use httpmock in this particular example? What do you use for logging in your tests?

sbohrer commented 3 years ago

Cargo.toml:

[package]
name = "httpmock-examp"
version = "0.1.0"
authors = ["Shawn Bohrer <sbohrer@cloudflare.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
flexi_logger = "0.11"
httpmock = "0.5"
reqwest = { version = "0.10", features = ["json", "rustls-tls"] }
tokio = { version = "0.2", features = ["full"] }

src/main.rs:

use httpmock::MockServer;

#[tokio::main]
async fn main() {
    flexi_logger::Logger::with_str("warn")
        .start()
        .expect("failed to create logger");

    let server = MockServer::start_async().await;
    server
        .mock_async(|when, then| {
            when.method("GET").path_contains("/hello");
            then.body("Hello").status(200);
        })
        .await;

    let body = reqwest::get(&server.url("/hello"))
        .await
        .unwrap()
        .text()
        .await
        .unwrap();
    println!("body: {}", body);
}
cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/httpmock-examp`
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
body: Hello
alexliesenfeld commented 3 years ago

@sbohrer This issue should be fixed in version 0.5.6.

sbohrer commented 3 years ago

Thanks!