喷雾测试gzip解码

Rin*_*iev 4 scala spray spray-json spray-test spray-client

我尝试喷涂测试

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")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我跟着路由器

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
                }
              }
            }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我使用GZIP压缩进行响应,但是

无法解密对类型'java.lang.String'的响应以进行responseAs断言:MalformedContent(未知令牌近:,某些(org.json4s.ParserUtil $ ParseException:未知令牌近:))

如何设置autodecode GZIP HttpResponse为String?

lmm*_*lmm 5

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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • `decode(Gzip)`是一个`HttpResponse => HttpResponse`而tilde函数转换了一个`RouteResult`对象.不知道写这个的好方法是什么,但我发现我可以通过从spray.routing.BasicDirectives交换`mapHttpResponse(decode(Gzip))(userRoute)`的路由来使它工作. (2认同)