Vladimir Dyuzhev, author of MockMotor

Vladimir Dyuzhev
MockMotor Creator

Mocking Plain Text Services


Not only JSON, XML or SOAP mock responses. MockMotor can produce plain text, too. Tweet This

Most of the world services exchange either JSON, SOAP or plain XML. Some, however, pass around different payload types.

MockMotor can read and produce many of those other content types, too.

Reading and Generating Plain Text

With JS

When a JS mock response cannot parse the incoming request as a JSON, it considers the whole request a text. The response script can then read the content of that text from the request (a.k.a. input) variable and use it to produce a response.

For example, here is a simple HelloWorld response, replying Hello, X where X is sent in as the plain text request:

The request variable contains the whole name as it was sent in (say, John Doe) and the response contains Hello, John Doe as plain text too, with an appropriate text/plain content type.

With XQ

XQ is oriented to work with XML. It can, however, produce plain text, too.

When an XQ mock response cannot parse the incoming request as XML, it considers the whole request a text. The response script can then read the content of that text from the $request (a.k.a. $input) variable and use it to produce a response.

Here is a version of HelloWorld response from the previous section, but built using XQ. It also replies Hello, X where X is sent in as the plain text request:

The request variable contains the whole name as it was sent in (say, John Doe). XQ has to use string-oriented functions to concatenate strings, hence the call to concat(). The response contains Hello, John Doe as plain text.

Example: CSV Service

For building more complex textual responses, I suggest to use JS responses and accumulate the result in the output variable.

For example, here is a very basic CSV (comma-separated value) response that builds its response payload programmatically:

The output (a.k.a. response) variable contains the response payload. The response builds this payload from data provided in the mockmeta variable, but can also build it from the accounts, or perform some computations and add the results to CSV.

The end result is plain text content in output. Since the content type is set to plain/csv, the client can read the response payload as CSV data.

Please Share

Was this post useful? Then please share! Tweet This