喷雾测试 gzip 解码
Posted
技术标签:
【中文标题】喷雾测试 gzip 解码【英文标题】:Spray-Test gzip decode 【发布时间】: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 自动解码为字符串?
【问题讨论】:
【参考方案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)
的路由来让它工作。以上是关于喷雾测试 gzip 解码的主要内容,如果未能解决你的问题,请参考以下文章