lundi 1 février 2016

How do I test HTTP POST requests issued via spray-client?

I use spray-client to send http post requests, and I need to to test those preferrably with ScalaTest. Spray provides means to test how spray-can handles incoming http requests, but I can't find examples of how to test outcoming requests sent by spray-client.

I send post requests like this:

class Sender(url: String = "http://ift.tt/1nAtF2F")
    extends Actor
    with ActorLogging {

    import context.dispatcher // execution context for futures below

    val pipeline: HttpRequest => Future[String] = (
      addCredentials(BasicHttpCredentials("bob", "secret")) 
        ~> encode(Gzip)
        ~> sendReceive
        ~> unmarshal[String]
      )

    override def receive: Actor.Receive = {
      case _ =>
        pipeline {
          Post(url, "string to be gzipped")
        }.foreach{ s: String =>
          log.info(s"received response: $s")
        }
    }
}

And then when I run this with ScalaTest,

  test("sends post requests") {
    testedSender ! "hi"
    Thread.sleep(3000)
  }

I can see the requests logged by the dev http server, like:

FORM/POST PARAMETERS
None

HEADERS
...
Authorization: Basic Ym9iOnNlY3JldA==
Content-Encoding: gzip
Content-Type: text/plain; charset=UTF-8
User-Agent: spray-can/1.3.3
...
RAW BODY
<gzipped stuff goes here>

Is there a widely-used way to put my expectations on request contents in a test suite?

Aucun commentaire:

Enregistrer un commentaire