Reputation: 6920
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:
#[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 ?
Upvotes: 1
Views: 759
Reputation: 27186
.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);
}
Upvotes: 2