alexliesenfeld / httpmock

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

Multimap header support #34

Closed mesuutt closed 3 years ago

mesuutt commented 3 years ago

Hi, thanks for this good crate.

It should good if we can multiple headers with same name on then same as reqwest HeaderMap

server.mock(|when, then| {
    when.method("POST")
        .path("/foo")
    then.status(200)
        .header(header::SET_COOKIE.as_str(), "mycookie1")
        .header(header::SET_COOKIE.as_str(), "mycookie2")
        .body("resp");
});
alexliesenfeld commented 3 years ago

Hi @mesuutt, thanks for your feature request. It should also be already possible to accept and respond with multiple header values with the same key. For example, the following test runs just fine:

#[test]
fn multiple_headers_with_same_key_test() {
    // Arrange
    let server = MockServer::start();

    let m = server.mock(|when, then| {
        when.path("/test")
            .header("myKey", "value2")
            .header("myKey", "value1");
        then.status(201)
            .header("myResponseKey", "value1")
            .header("myResponseKey", "value2");
    });

    // Act: Send the request and deserialize the response to JSON
    let response = Request::post(&format!("http://{}/test", server.address()))
        .header("myKey", "value2")
        .header("myKey", "value1")
        .body(())
        .unwrap()
        .send()
        .unwrap();

    // Assert
    m.assert();
    assert_eq!(response.status(), 201);
}

The response object of reqwest create is a map though, so it does not provide the functionality to have multiple header keys in the response. I think this should be a feature request in the reqwest create rather than httpmock - however, this is non-standard functionality (see here), so chances are high it won't be accepted.

alexliesenfeld commented 3 years ago

Closing for now. Feel free to get back to it if you see open points.