Spray unmarshalling generic type

60 views
Skip to first unread message

Anatoly Rabinovich

unread,
Aug 25, 2015, 11:31:19 AM8/25/15
to spray.io User List

I'm using spray-client to generate http requests to my server in e2e tests. I also use specs2 to test for the desired response from the server. And everything works fine.
I've built some custom specs2 matchers to simplify my test code. My test looks like this: 

    val response = get(<server_endpoint_url>)

    response must beSuccessfulWith(content = expected_data)

I have a trait that somewhat simplifies the usage of spray in the test itself:

    trait SprayTestClientSupport {
        implicit val system = ActorSystem()
        import system.dispatcher // execution context for futures

        val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

        def get(url: String): Future[HttpResponse] = pipeline(Get(url))
    }

I also have a trait where I define the custom matchers I use in the test:

    trait SprayTestClientSupport extends ShouldMatchers with SprayJsonSupport with DefaultJsonProtocol {
        def beSuccessfulWith(content: Seq[Int]): Matcher[Future[HttpResponse]] = beSuccessful and haveBodyWith(content)

        def haveBodyWith(content: Seq[Int]): Matcher[Future[HttpResponse]] = 
            containTheSameElementsAs(content) ^^ { (f: Future[HttpResponse]) => { Await.result(f, await).entity.as[Seq[Int]].right.get } }

        def beSuccessful: Matcher[Future[HttpResponse]] = { 
            ===(StatusCode.int2StatusCode(200)) ^^ { (f: Future[HttpResponse]) => { Await.result(f, await).status } } 
        }
    }

My problem starts when I try to make the matchers more general and support any Scala type for instance. I define something like this:

    def haveBodyWith[T: TypeTag](content: T): Matcher[Future[HttpResponse]] = ===(content) ^^ { (f: Future[HttpResponse]) => { Await.result(f, await).entity.as[T].right.get } }

But then I get the following error message:

    Error:(49, 86) could not find implicit value for parameter unmarshaller: spray.httpx.unmarshalling.Unmarshaller[T]
        ===(content) ^^ { (f: Future[HttpResponse]) => { Await.result(f, await).entity.as[T].right.get } }

Is there anything simple that I'm missing?

Thanks!

P.S.
I'm use the following spray versions:
spray-client_2.10 -> 1.3.3
spray-can_2.10    -> 1.3.3
spray-http_2.10   -> 1.3.3
spray-httpx_2.10  -> 1.3.3
spray-util_2.10   -> 1.3.3
spray-json_2.10   -> 1.3.2

Johannes Rudolph

unread,
Aug 25, 2015, 11:35:06 AM8/25/15
to spray...@googlegroups.com
(Cross-posted here:
http://stackoverflow.com/questions/32207330/spray-unmarshalling-generic-type)

Please at least include links if you cross-post questions somewhere
else so that questions are not answered twice.
> --
> You received this message because you are subscribed to the Google Groups
> "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/spray-user/408a8b7a-cd1f-4c43-9d66-04bbb6d80d16%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Anatoly Rabinovich

unread,
Aug 25, 2015, 11:37:54 AM8/25/15
to spray.io User List
You're right, forgot to add the link to my post on stackoverflow.

Anatoly Rabinovich

unread,
Aug 26, 2015, 3:32:25 AM8/26/15
to spray.io User List
Got an answer on stackoverflow
Just needed to add another constraint to my T parameter.

def haveBodyWith[T: TypeTag : Unmarshaller](content: T): Matcher[Future[HttpResponse]] = 
  ===(content) ^^ { f: Future[HttpResponse] => 
     Await.result(f, await).entity.as[T].right.get
}

Johannes Rudolph

unread,
Aug 26, 2015, 7:41:58 AM8/26/15
to spray...@googlegroups.com
Good to know and thanks for posting the solution here :)
> --
> You received this message because you are subscribed to the Google Groups
> "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/spray-user/70f2b3f7-327d-4b8d-9c4b-c146cec2bcc2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages