restMock-story Pub Share

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}/diesel/react/service1/"...)

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

send::  msg snakk.json  (url ="${HOST}/diesel/react/service1/"..., snakkHttpOptions :JSON)

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

This is a custom binding:

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

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

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

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

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

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

send::  msg snakk.text  (url ="${HOST}/diesel/mock/account2/7"..., snakkHttpOptions :JSON)

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

Match an explicit diesel.flow.return

send::  msg snakk.text  (url ="${HOST}/diesel/mock/account2/4"..., snakkHttpOptions :JSON)

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}/diesel/mock/account2as"..., snakkHttpOptions :JSON)

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

send::  msg snakk.text  (url ="${HOST}/diesel/react/msg.notex"..., snakkHttpOptions :JSON)

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

send::  msg snakk.json  (url ="${HOST}/diesel/mock/account2/7"...)

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

send::  msg snakk.json  (url ="${HOST}/diesel/rest/path2/path"...)

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

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

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

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")


Was this useful?    

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


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

You need to log in to post a comment!

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