LukeMathWalker/tracing-actix-web

Using TracingLogger middleware with tests

darrell-roberts opened this issue · 1 comments

Hi,

I have a test module that has a single function that builds and returns an actix web test service that is used by all the test functions.
Ex:

async fn get_service() -> impl actix_service::Service<
  actix_http::Request,
  Response = actix_web::dev::ServiceResponse,
  Error = actix_web::Error,
> {
  let persist: web::Data<Arc<dyn UserPersistence>> =
    web::Data::new(Arc::new(TestPersistence));
  let service = test::init_service(
    App::new()
      .app_data(persist)
      .wrap(JwtAuth::default())
      .wrap(TracingLogger::default())
      .service(
        web::scope("/api/v1/user")
          .service(handlers::count_users)
          .service(handlers::get_user)
          .service(handlers::save_user)
          .service(handlers::search_users)
          .service(handlers::update_user),
      ),
  )
  .await;
  service
}

This won't compile with the wrap(TracingLogger::default()) given it requires the return type be:

error[E0271]: type mismatch resolving `<impl actix_service::Service<Request, Response = ServiceResponse<tracing_actix_web::middleware::StreamSpan<BoxBody>>, Error = actix_web::Error> as actix_service::Service<Request>>::Response == ServiceResponse`
  --> rust-actix-web/src/test.rs:89:27
   |
89 |   async fn get_service() -> impl actix_service::Service<
   |  ___________________________^
90 | |   actix_http::Request,
91 | |   Response = dev::ServiceResponse,
92 | |   Error = actix_web::Error,
93 | | > {
   | |_^ expected struct `BoxBody`, found struct `tracing_actix_web::middleware::StreamSpan`
   |
   = note: expected struct `ServiceResponse<BoxBody>`
              found struct `ServiceResponse<tracing_actix_web::middleware::StreamSpan<BoxBody>>`

I can change the return type to this:

async fn get_service() -> impl actix_service::Service<
  actix_http::Request,
  Response = actix_web::dev::ServiceResponse<
    tracing_actix_web::middleware::StreamSpan<actix_web::body::BoxBody>,
  >,
  Error = actix_web::Error,
> {
...
}

However that won't work because StreamSpan is in a private module:

error[E0603]: module `middleware` is private
   --> rust-actix-web/src/test.rs:92:24
    |
92  |     tracing_actix_web::middleware::StreamSpan<actix_web::body::BoxBody>,
    |                        ^^^^^^^^^^ private module
    |

Not sure what other options may be available in order to be able to have this function work with TracingLogger middleware.

I'm using:

[dependencies]
tracing-actix-web="=0.5.0-beta.11"

[dependencies.actix-web]
version = "4.0.0-beta.21"
features = ["openssl"]

I opened this and then shortly after having reviewed the source code I found that StreamSpan implements the MessageBody trait. I changed my return type to the following and got it to work:

async fn get_service() -> impl actix_service::Service<
  actix_http::Request,
  Response = actix_web::dev::ServiceResponse<impl MessageBody>,
  Error = actix_web::Error,
> {

I'll close this.