Actix-web-4.0.0-rc.2: TestRequest: append_header or insert_header

Hello

I upgraded from actix-web-3 to actix-web-4.0.0-rc.2 - which worked fine. When testing I encountered an issue I don't really come up with a solution. How am I supposed to add a HTTP-header?

I'm getting a trait not satified error:

error[E0277]: the trait bound `HeaderMap: actix_http::header::Header` is not satisfied
   --> src/main.rs:549:40
    |
549 |                         .insert_header(hm)
    |                          ------------- ^^ the trait `actix_http::header::Header` is not implemented for `HeaderMap`
    |                          |
    |                          required by a bound introduced by this call
    |
    = note: required because of the requirements on the impl of `TryIntoHeaderPair` for `HeaderMap`
note: required by a bound in `actix_web::test::TestRequest::insert_header`
   --> /home/doerig/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-web-4.0.0-rc.2/src/test/test_request.rs:134:49
    |
134 |     pub fn insert_header(mut self, header: impl TryIntoHeaderPair) -> Self {
    |                                                 ^^^^^^^^^^^^^^^^^ required by this bound in `actix_web::test::TestRequest::insert_header`

My toml looks like this:

actix-multipart = "0.4.0-beta.12"
actix-rt = "2"
actix-web = "4.0.0-rc.2"
actix-http = "3.0.0-rc.1"

The code in the test section looks like this:

use actix_web::{http::StatusCode, test, App};
use actix_http::header::map::HeaderMap;
use actix_http::header::{Header, HeaderName, HeaderValue};

// big snip....

let mut hm: HeaderMap = HeaderMap::new();
hm.insert(actix_web::http::header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
hm.insert(actix_web::http::header::AUTHORIZATION, HeaderValue::from_static(&format!("Basic {}", sessiontoken)));

test::TestRequest::post()
   .uri(uri)
   .append_header(hm)
   .send_request(&mut app)
   .await

I just don't get it how to pass headers to test request except implementing this trait by myself.

Thanks

Stefan

The complete code can be found here https://github.com/sdoerig/nestbox/blob/dev_sqlx/nestboxd/src/main.rs starting from line 533.

1 Like

It seems like the insert_header method is used to insert a single header, but your HeaderMap contains many headers. Maybe you want something like this instead (untested)?

let mut req = test::TestRequest::post().uri(uri);
for (k, v) in hm {
    req = req.insert_header((k, v));
}
req.send_request(&mut app).await

Btw, to head off problems like this you should use the re-exported version of actix-rt under actix_web::rt instead of depending on actix-rt directly.

1 Like

@cole-miller Thanks now it compiles and looks like this:

.append_header((
   actix_web::http::header::CONTENT_TYPE,
   HeaderValue::from_static("application/json"),
 ))

Many thanks.

Edit: once the tests compiled again, they unveiled some problems with AppData - after fixing those anything works now as before - at least at the first glance - next I will replace MongoDB with PostreSQL - seems I'm more the relational database guy :slight_smile:

Stefan

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.