4

I try write test for spray

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")
      }
    }
  }
}

I have follow router

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
                }
              }
            }
        }
      }
    }
  }
}

I use GZIP compression for response, but

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

How to set autodecode GZIP HttpResponse to String?

Rinat Mukhamedgaliev
  • 5,401
  • 8
  • 41
  • 59

1 Answers1

5

Include a decode(Gzip) in your pipeline:

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)
        }
    }
Zouzias
  • 2,330
  • 1
  • 22
  • 32
lmm
  • 17,386
  • 3
  • 26
  • 37
  • Error:(18, 65) not found: value decode Get("/user") ~> `Accept-Encoding`(identity) ~> userRoute ~> decode(Gzip) ~> check { – Rinat Mukhamedgaliev Jan 06 '15 at 11:12
  • Mix in `ResponseTransformation`, i.e. `class FullTestKitExampleSpec extends ResponseTransformation with ...` – lmm Jan 06 '15 at 11:14
  • Error:(19, 71) type mismatch; found : FullTestKitExampleSpec.this.ResponseTransformer (which expands to) spray.http.HttpResponse => spray.http.HttpResponse required: – Rinat Mukhamedgaliev Jan 06 '15 at 11:20
  • Get("/user") ~> userRoute ~> decode(Gzip) ~> check { – Rinat Mukhamedgaliev Jan 06 '15 at 11:20
  • I too don't know what required... Now i wont understand how to work pipelines. – Rinat Mukhamedgaliev Jan 06 '15 at 12:00
  • No I mean, what does the error message say is required? You've cut it off just before the important part – lmm Jan 06 '15 at 12:07
  • 2
    `decode(Gzip)` is an `HttpResponse => HttpResponse` whereas the tilde function transforms a `RouteResult` object. Not sure what the nice way to write this is but I found I could get it to work by swapping out the route for `mapHttpResponse(decode(Gzip))(userRoute)`, from spray.routing.BasicDirectives. – William Carter Feb 23 '15 at 01:07
  • Thanks William, your comment helped me. Also asking myself if there is an nicer solution – Chris W. Feb 23 '15 at 12:35