restMock-story Pub

Testing mocked REST services

Here's a simple mock for a REST service - a basic "status" service which returns a JSON.

The rules below that implement it are defined in the "specs" realm, so we need to point to that project:

val HOST ="http://specs.dieselapps.com"="http://specs.dieselapps.com"

Testing the status service (see the [[restMock-spec]] for the implementation).

Testing the default binding:

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="status"] is "ok")

send::  msg snakk.json  (url="${HOST}/d..., snakkHttpOptions:JSON={responseC...)

expect::  (snakk[="response"][="code"] is 501)

This is a custom binding:

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="status"] is "ok")

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="someStats"] is "456")

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="accountId"] is "67")

send::  msg snakk.text  (url="${HOST}/d..., snakkHttpOptions:JSON={responseC...)

expect::  (snakk[="response"][="code"] is 501)

Match an explicit diesel.return

send::  msg snakk.text  (url="${HOST}/d..., snakkHttpOptions:JSON={responseC...)

expect::  (snakk[="response"][="code"] is 404)
expect::  (snakk[="response"][="headers"][="myheader"] is "mine")
send::  msg ctx.echo  (snakk.response.headers)

Not match anything - payload is empty

send::  msg snakk.text  (url="${HOST}/d..., snakkHttpOptions:JSON={responseC...)

expect::  (payload is empty)
expect::  (snakk[="response"][="code"] is 501)

send::  msg snakk.text  (url="${HOST}/d..., snakkHttpOptions:JSON={responseC...)

expect::  (payload is empty)
expect::  (snakk[="response"][="code"] is 501)

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="accountId"] is "78")

send::  msg snakk.json  (url="${HOST}/d...)

expect::  (payload[="accountId"] is "path1/path2/89")

Testing a POST with a JSON body to a service mock:

send::  msg snakk.json  (url="${HOST}/d..., verb="POST"="POST", body:JSON={companyCo...)

send::  msg ctx.echo  (payload.status)

expect::  (payload[="status"] is "ok")

Testing these directly - without going through an http request (we simulate the initial message):

send::  msg diesel.rest  (path="/getAccount/45"="/getAccou...)


Was this useful?    

By: Razie | 2019-04-27 | Tags: story , dsl


Viewed 252 times ( | History | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2022, all rights reserved.