akka/akka-http

Incorrect behaviour for header parsing with akka-http-testkit

開放

#2,132 建立於 2018年7月31日

 (8 則留言) (0 個反應) (0 位負責人)Scala (598 個分叉)batch import
1 - triagedhelp wantedt:coret:modelt:testing

倉庫指標

星標
 (1,311 顆星)
PR 合併指標
 (平均合併 1天 10小時) (30 天內合併 2 個 PR)

描述

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.

貢獻者指南