~rjframe/surf-vcr

a84eef885add1893c3a1c66c21a5a0aec0ac86f1 — Ryan Frame 2 months ago 78099b7
Simplify recording unit test

The second client is not under test, but verifies the behavior of the
VCR being tested; therefore we can and should be explicit about our
expectations rather than use the middleware to modify
requests/responses.

Our cookie assertion is potentially problematic; we have no real control
over which cookie is de/serialized first, so no control over which is
removed. If we re-record this test it could fail. It might be better to
just assert that the vector's length is 1.
1 files changed, 8 insertions(+), 5 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +8 -5
@@ 623,25 623,28 @@ mod tests {

        let mut expected_res = client.send(req).await.unwrap();


        // Now we'll create a client to replay what we just did.
        let client = surf::Client::new()
            .with(VcrMiddleware::new(VcrMode::Replay, path).await?.with_modify_request(hide_session_key));
            .with(VcrMiddleware::new(VcrMode::Replay, path).await?);

        let req = surf::get("https://example.com")
            .header("X-some-header", "another hello")
            .header("Content-Type", "application/octet-stream")
            .header("session-key", "ffeeddccbbaa99887766554433221100")
            .header("session-key", "(some key)")
            .build();

        let mut res = client.send(req).await.unwrap();
        let mut modified_res = VcrResponse::try_from_response(&mut res).await.unwrap();
        hide_cookie(&mut modified_res);
        let res = VcrResponse::try_from_response(&mut res).await.unwrap();

        assert_eq!(
            modified_res,
            res,
            VcrResponse::try_from_response(&mut expected_res).await.unwrap()
        );

        let cookies = &res.headers["set-cookie"];
        assert!(! cookies.contains(&"cookie2=val2; Expires=date2".into()));

        Ok(())
    }
}