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.

贡献者指南