restMock-story Pub

Testing mocked REST services

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

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

Testing the default binding:

send::  msg snakk.json  (url<-"/diesel/react/service1/status")

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

send::  msg snakk.json  (url<-"/diesel/react/service1/noSuchMethod", snakkHttpOptions:JSON<-{responseCode:"*"})

expect::  (snakkHttpCode is 501)

This is a custom binding:

send::  msg snakk.json  (url<-"/diesel/mock/myStatus")

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

send::  msg snakk.json  (url<-"/diesel/mock/getAccount/45")

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

send::  msg snakk.json  (url<-"/diesel/mock/getAccount1/67")

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

send::  msg snakk.text  (url<-"/diesel/mock/account2/78", snakkHttpOptions:JSON<-{responseCode:"*"})

expect::  (snakkHttpCode is 501)

Match an explicit diesel.return

send::  msg snakk.text  (url<-"/diesel/mock/account2/404/id", snakkHttpOptions:JSON<-{responseCode:"*"})

expect::  (snakkHttpCode is 404)
expect::  (snakkHttpHeaders[="myheader"] is "mine")
send::  msg ctx.echo  (snakkHttpHeaders)

Not match anything - payload is empty

send::  msg snakk.text  (url<-"/diesel/mock/account2asdf", snakkHttpOptions:JSON<-{responseCode:"*"})

expect::  (payload is empty)
expect::  (snakkHttpCode is 501)

send::  msg snakk.text  (url<-"/diesel/react/msg.notexists", snakkHttpOptions:JSON<-{responseCode:"*"})

expect::  (payload is empty)
expect::  (snakkHttpCode is 501)

send::  msg snakk.json  (url<-"/diesel/mock/account2/78/id")

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

send::  msg snakk.json  (url<-"/diesel/rest/path2/path1/path2/89")

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

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

send::  msg snakk.json  (url<-"/diesel/react/service1/status", verb<-"POST", body:JSON<-{companyCode:"HG",customerNumber:"1502465"})

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 68 times ( | Print ) this page.

You need to log in to post a comment!

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