Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing Content-Type in Response Header

I’m trying to run an integration test for Content-Type in the response. It fails with error:

   --> tests\greet.rs:18:9
    |
5   |     let response = client
    |         -------- move occurs because `response` has type `Response`, which does not implement the `Copy` trait
...
13  |         response.text().await.unwrap(),
    |                  ------ `response` moved due to this method call
...
18  |         response.content_length(),
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
    |
note: this function takes ownership of the receiver `self`, which moves `response`
   --> C:\Users\Saurabh Mishra\.cargo\registry\src\github.com-1ecc6299db9ec823\reqwest-0.11.12\src\async_impl\response.rs:146:23
    |
146 |     pub async fn text(self) -> crate::Result<String> {
    |                       ^^^^

And when I comment out the test for response body (response.text()...), all tests execute correctly.

The test suite is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#[tokio::test]
async fn greeting_works() {
    spawn_app();
    let client = reqwest::Client::new();
    let response = client
        .get("http://127.0.0.1:8080/hello")
        .send()
        .await
        .expect("Failed to execute request");

    assert!(response.status().is_success(), "Endpoint validity");
    assert_eq!(
        response.text().await.unwrap(),
        "Hello, World!",
        "Response from endpoint"
    );
    assert_eq!(
        response.content_length(),
        Some(13),
        "Response length is 13 characters"
    );
    assert_eq!(
        response.headers().get("Content-Type").unwrap(),
        "text/plain; charset=utf-8"
    );
}

fn spawn_app() {
    let server = mailrocket::run().expect("Failed to bind address");
    let _ = tokio::spawn(server);
}

How can I run this suite so that all four tests execute ?

>Solution :

.text() consumes the response, so you can no longer use it after you call the method.
A simple way to fix it would be to do the assertion on .text() last:

#[tokio::test]
async fn greeting_works() {
    spawn_app();
    let client = reqwest::Client::new();
    let response = client
        .get("http://127.0.0.1:8080/hello")
        .send()
        .await
        .expect("Failed to execute request");

    assert!(response.status().is_success(), "Endpoint validity");
    assert_eq!(
        response.content_length(),
        Some(13),
        "Response length is 13 characters"
    );
    assert_eq!(
        response.headers().get("Content-Type").unwrap(),
        "text/plain; charset=utf-8"
    );
    assert_eq!(
        response.text().await.unwrap(),
        "Hello, World!",
        "Response from endpoint"
    );
}

fn spawn_app() {
    let server = mailrocket::run().expect("Failed to bind address");
    let _ = tokio::spawn(server);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading