Response Configuration

Overview

HTTP API Simulator determines the simlet upon matching a request. What to reply with is defined in simlet’s response. Below we look at how to define the HTTP Status, Version, Headers, and Body of a response.

Response Status

There’s support for various methods to specify the HTTP Status of a response:

httpResponse() // HTTP Status defaults to '200 OK' if no 'withStatus(..)' is specified

httpResponse(200)
httpResponse().withStatus(200)
httpResponse().withStatus(999, "Say What?")

// These require 'import static com.apisimulator.http.HttpStatus.*;'
httpResponse(OK)
httpResponse().withStatus(OK)

Response Headers

Use one of the following methods to specify headers to return in the response:

// Specify a custom or standard header name using any letter casing
.withHeader(String name, String value)

// This requires 'import static com.apisimulator.http.Http1Header.*;'
.withHeader(Http1Header headerName, String value)

// This requires 'import static com.apisimulator.http.Http2Header.*;'
// Header names are all lower case
.withHeader(Http2Header headerName, String value)

Response Body

Text and binary body content are supported:

// Text body. The character set defaults to "UTF-8"
.withBody(String body)

// Text body using the provided character set
.withBody(String body, String charsetName)

// Text body using the provided character set object
.withBody(String body, Charset charset)

// Allows binary payload, for example images
.withBody(byte[] body)

Simulating Latency

The Embedded API Simulator allows for configuring latency as a delay added to any network latency and request processing time. It is a time interval added to the total response time an API consumer would experience.

Here is how to configure it:

// Add these import statements
import static com.apisimulator.embedded.Latency.*;
import java.util.concurrent.TimeUnit;

// This configures latency with a fixed, always the same delay
.then(httpResponse()
   .withLatency(fixed(250, TimeUnit.NANOSECONDS))
   ...
)

// This configures latency such that the delay for the first
// request is 300 milliseconds and then 100 milliseconds for
// all subsequent requests. It can be useful in simulating
// delay due to class loading or code compilation that may
// occur upon the first request
.then(httpResponse()
   .withLatency(fixed(300, 100, TimeUnit.MILLISECONDS))
   ...
)

// The following configures latency with a random, uniformly
// distributed delay between 50 and 550 milliseconds
.then(httpResponse()
   .withLatency(random(50, 550, TimeUnit.MILLISECONDS))
   ...
)

We would love to hear your feedback - send us a quick email to [feedback at APISimulator.com]

Happy API Simulating!