喷雾测试gzip解码

时间:2015-01-06 08:13:05

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

我尝试喷涂测试

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(未知令牌)     附近:,一些(org.json4s.ParserUtil $ ParseException:未知令牌     附近:))

  

如何将自动编码GZIP HttpResponse设置为String?

1 个答案:

答案 0 :(得分: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)
        }
    }