Mocking HTTP services on spray can for integration testing
Pre-canned helps you to mock out the HTTP services your application depends on. This can be especially useful for your integration testing.
For SBT add the dependency "com.netaporter" %% "pre-canned" % "0.0.8" % "test"
Pre-canned currently comes in two flavours:
Help make Pre-canned better and submit a new improved flavour via a PR, or ideas for one in an issue.
There are a basic set of expectations and canned responses. Feel free to contribute more via a PR.
import com.netaporter.precanned.dsl.basic._
val animalApi = httpServerMock(system).bind(8765).block
animalApi.expect(get, path("/animals"), query("name" -> "giraffe"))
.andRespondWith(resource("/responses/giraffe.json"))
.blockUpTo(5.seconds)
resource("example.json")
will look for files in src/main/resources/example.json
or src/test/resources/example.json
import com.netaporter.precanned.dsl.fancy._
val animalApi = httpServerMock(system).bind(8766).block
animalApi expect
get and path("/animals") and query("name" -> "giraffe") and
respond using
resource("/responses/giraffe.json") end()
You can add an artificial latency with delay()
. For example, adding a 5 second delay:
import scala.concurrent.duration._
animalApi.expect(get, path("/animals"))
.andRespondWith(resource("/responses/giraffe.json"), delay(5.seconds))
import scala.concurrent.duration._
animalApi expect
get and path("/animals") and
respond using
resource("/responses/giraffe.json") and delay(5.seconds) end()
Normally, when you use the DSL, expectations are added asynchronously.
To block until an expectation is successfully added, use blockUpTo = duration
as shown in the examples below. This will return as soon the expectation has
been added, or the blockFor
has been reached, whichever is sooner.
import scala.concurrent.duration._
animalApi.expect(get, path("/animals"))
.andRespondWith(resource("/responses/giraffe.json"))
.blockUpTo(5.seconds)
By default the fancy DSL blocks up to 3 seconds, however you can change it like so:
import scala.concurrent.duration._
animalApi expect
get and path("/animals") and
respond using
resource("/responses/giraffe.json") end(blockUpTo = 5.seconds)
You can disable blocking of adding expectations, like so:
import scala.concurrent.duration._
animalApi expect
get and path("/animals") and
respond using
resource("/responses/giraffe.json") end(blockUpTo = Duration.Zero)