akka/akka-http

Incorrect behaviour for header parsing with akka-http-testkit

Aperta

#2132 aperta il 31 lug 2018

 (8 commenti) (0 reazioni) (0 assegnatari)Scala (598 fork)batch import
1 - triagedhelp wantedt:coret:modelt:testing

Metriche repository

Star
 (1311 stelle)
Metriche merge PR
 (Merge medio 1g 10h) (2 PR mergiate in 30 g)

Descrizione

It seems that header parsing with akka-http-testkit is behaving differently than a real akka HTTP server. I am not sure it is a bug but the behaviour is unexpected. Here is my failing test:

import akka.http.scaladsl.model.headers._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest._

class Test extends FlatSpec with Matchers with ScalatestRouteTest {
  val route =
    extractCredentials { creds =>
      complete {
        creds match {
          case Some(c) => "Found: " + c
          case _ => "No credentials"
        }
      }
    }

  it should "verify headers" in {
    RawHeader("Authorization", """Basic dXNlcjpwd2Q=""").toString() shouldBe "Authorization: Basic dXNlcjpwd2Q="

    Authorization(BasicHttpCredentials("user", "pwd")).toString() shouldBe "Authorization: Basic dXNlcjpwd2Q="
  }

  it should "test 1" in {
    Get("/") ~> addHeader(RawHeader("Authorization", """Basic dXNlcjpwd2Q=""")) ~>
      route ~> check {
      responseAs[String] shouldEqual """Found: Basic dXNlcjpwd2Q="""
    }
  }

  it should "test 2" in {
    Get("/") ~> addHeader(Authorization(BasicHttpCredentials("user", "pwd"))) ~>
      route ~> check {
      responseAs[String] shouldEqual """Found: Basic dXNlcjpwd2Q="""
    }
  }
}

From my understanding, the test test 1 should not fail. If we launch a real server and perform curl -H 'Authorization: Basic dXNlcjpwd2Q=' localhost:8080 then we get the expected result.

Guida contributor