加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Spray-Test gzip解码

发布时间:2020-12-16 18:08:13 所属栏目:安全 来源:网络整理
导读:我尝试喷涂测试 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 { Ge
我尝试喷涂测试

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压缩进行响应,但是

Could not unmarshal response to type ‘java.lang.String’ for responseAs assertion: MalformedContent(unknown token
Near:,Some(org.json4s.ParserUtil$ParseException: unknown token
Near: ))

如何设置autodecode GZIP HttpResponse为String?

解决方法

在您的管道中包含解码(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)
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读