【问题标题】:Spray-Test gzip decode喷雾测试 gzip 解码
【发布时间】:2015-03-03 21:05:15
【问题描述】:

我尝试编写喷雾测试

class FullTestKitExampleSpec extends Specification with Specs2RouteTest with UserController with HttpService {
  def actorRefFactory = system

  "The service" should {

    "return a greeting for GET requests to the root path" in {
      Get("/user") ~> `Accept-Encoding`(gzip) ~> userRoute ~> check {
        val responsex = response
        responseAs[String] must contain("Test1")
      }
    }
  }
}

我有关注路由器

trait UserController extends HttpService with Json4sSupport with CORSSupport{
  override implicit def json4sFormats: Formats = DefaultFormats

  val userRoute = {
    cors {
      compressResponse(Gzip) {
        path("user") {
          get {
            complete {
              "Test1"
            }
          } ~
            post {
              entity(as[UserRegister]) { person =>
                complete {
                  println(person.name)
                  person.name
                }
              }
            }
        }
      }
    }
  }
}

我使用 GZIP 压缩来响应,但是

无法解组对 responseAs 断言的类型“java.lang.String”的响应:MalformedContent(未知令牌 Near: ,Some(org.json4s.ParserUtil$ParseException: 未知令牌 附近:))

如何将 GZIP HttpResponse 自动解码为字符串?

【问题讨论】:

    标签: scala spray spray-json spray-client spray-test


    【解决方案1】:

    在您的管道中包含decode(Gzip)

    import spray.httpx.encoding.Gzip
    import spray.httpx.ResponseTransformation
    
    class MySprayRouteSpec extends FlatSpec
        with ShouldMatchers
        with ResponseTransformation
        with ScalatestRouteTest
        {
            Get("/") ~> mapHttpResponse(decode(Gzip))(userRoute) ~> check{
                  response.status should equal(OK)
            }
        }
    

    【讨论】:

    • 错误:(18, 65) not found: value decode Get("/user") ~> Accept-Encoding(identity) ~> userRoute ~> decode(Gzip) ~> check {跨度>
    • 混入ResponseTransformation,即class FullTestKitExampleSpec extends ResponseTransformation with ...
    • 错误:(19, 71) 类型不匹配;发现:FullTestKitExampleSpec.this.ResponseTransformer(扩展为) spray.http.HttpResponse => spray.http.HttpResponse required:
    • Get("/user") ~> userRoute ~> decode(Gzip) ~> check {
    • decode(Gzip) 是一个HttpResponse => HttpResponse,而波浪号函数转换一个RouteResult 对象。不知道写这个的好方法是什么,但我发现我可以通过从 spray.routing.BasicDirectives 换出mapHttpResponse(decode(Gzip))(userRoute) 的路由来让它工作。
    猜你喜欢
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多