Simlets
Overview
An API Simulation consists of one or more simlets.
A simlet simulates the HTTP response to an HTTP request. |
The expressive Java DSL (Domain Specific Language) of the Embedded API Simulator allows us to define simlets using Behavior-Driven Development (BDD) constructs. Let’s look at the following example:
apiSimulation.add(simlet("get-place-formatted-as-json")
.when(httpRequest()
.whereMethod(GET)
.whereUriPath(isEqualTo("/api/places/place/zE2toxSo"))
.whereQueryParameter("fmt", isEqualTo("json"))
)
.then(httpResponse()
.withStatus(OK)
.withHeader(CONTENT_TYPE, "application/json; charset=UTF-8")
.withBody(
"{\n" +
" \"results\" : [\n" +
" {\n" +
" \"name\" : \"Coo-coo Restaurant\",\n" +
" \"place_id\" : \"zE2toxSo\",\n" +
" \"rating\" : 4.2\n" +
" },\n" +
" ],\n" +
" \"status\" : \"OK\"\n" +
"}"
)
)
);
What the simlet does should be obvious just by reading the code but let’s read it aloud:
[Given] API Simulation apiSimulation, add simlet [named] "get-place-formatted-as-json" [such that]
When HTTP Request method is GET
And URI path is equal to "/api/places/place/zE2toxSo"
And Query String Parameter "fmt" is equal to "json"
Then [reply with] HTTP Response with Status OK (200)
And with Header CONTENT_TYPE ("Content-Type") with value "application/json; charset=UTF-8"
And with Body "{….}"
We would love to hear your feedback! Shoot us a quick email to [feedback at APISimulator.com] to let us know what you think.
Happy API Simulating!