These are a few tests and examples of snakking REST and Telnet. See :
The specs for these messages are in rest spec++.
// $send snakk.xml (url = "http://localhost:9000/wiki/specs.Sample:sample-xml-data/content") // $send ctx.echo (x = payload / "Contact") // $send ctx.echo (x = payload.SaveContactRestRequest)
Test also that diesel.ping is public and contains some things:
As text:
send::
msg snakk.text (url ="/diesel/react/diesel.ping"
...)
expect:: (payload contains "limitedRequests"
)
expect:: (payload containsNot "NOPEGlobal.limitedApiRequests"
)
As json:
send::
msg snakk.json (url ="/diesel/react/diesel.ping"
...)
expect:: (payload[="global"][="limitedRequests"] is defined
)
expect:: (payload[=""] not defined
)
POST with headers:
send::
msg snakk.json (url ="/diesel/mock/dieselsample/echo"
..., verb="POST"
, body="haha113"
, headers :JSON)
expect:: (payload[="headers"][="Content-Type"="Content-Type"] is "application/texteh"
)
expect:: (snakkHttpCode is 200
)
expect:: (snakkHttpHeaders contains "content-type"
)
expect:: (snakkHttpResponse contains "haha"
)
expect:: (snakkError is undefined
)
CORRECT:
expect:: (snakk[="response"][="code"] is 200
)
expect:: (snakk[="response"][="headers"] contains "content-type"
)
expect:: (snakkHttpResponse contains "haha"
)
expect:: (snakkError is undefined
)
Call a simple echo service to get the body back:
send::
msg snakk.json (url ="/diesel/mock/dieselsample/echo"
..., verb="GET"
, body="haha113"
)
expect:: (payload[="body"] is "haha113"
)
If you need to parse strings...
Parsing JSON
send::
msg ctx.set (payload="{"status": "Failed"}"
)
send::
msg snakk.parse.json
expect:: (payload[="status"] is "Failed"
)
Parsing regex - use named groups
send::
msg ctx.set (payload="{"status": "Failed"}"
)
send::
msg snakk.parse.regex (regex :Regex="/(?s){.*status": *"(?<status>"
...)
expect:: (status is "Failed"
)
Headers are grouped in snakk.response.headers
and are lowercase:
The following will match the $mock testdiesel.getdoc.pdf(invoiceId)
from the rest-spec:
send::
msg snakk.text (url ="/diesel/mock/testdiesel/getdoc"
...)
expect:: (snakk[="response"][="headers"][="content-length"="content-length"] > 0
)
expect:: (snakk[="response"][="headers"][="content-type"="content-type"] is defined
)
expect:: (snakk[="response"][="headers"][="content-type"="content-type"] contains "pdf"
)
expect:: (snakk[="response"][="headers"][="Content-Type"="Content-Type"] not defined
)
send::
msg ctx.echo (m)
Send this test message - which will in turn snakk another service, via REST URL - see the spec.
send::
msg diesel.realm.set (diesel.rest.templates:Boolean)
send::
msg ctx.echo (x)
// $send test.diesel.testqueryp (p1="1", p2="2") // $expect (payload == "12")
send::
msg test.diesel.testjson (p1="3"
, p2="4"
)
expect:: (payload == "34"
)
send::
msg test.diesel.testpattern (p1="5"
, p2="6"
)
expect:: (payload == 11
)
Direct snakk from a TELNET service:
send::
msg snakk.telnet (host="www.google.ca"
, port="80"
, body="GET / HTTP 1.1\n\r"
)
expect:: (payload contains "Bad Request"
)
Same thing via a template:
// $send test.diesel.telnetGoogle // $expect (payload contains "404 Not Found")
// $send test.diesel.telnetsample(p1="7", p2="8") // $expect (payload contains "78")
send::
msg test.diesel.testXmlRequestOut (p1="1"
, p2="2"
)
expect:: (sumxml is "12"
)
This one won't actually snakk anything, just make sure this service is functional.
send::
msg test.diesel.testXmlRequestService (p1="1"
, p2="2"
)
expect:: (sumxml is "12"
)
Passing the invoiceId as query parm - this version will match the
// $send snakk.text (url="/diesel/mock/test/getPdf/Invoice-1.pdf?invoiceId=123") // $expect (payload contains "line197") // $expect (snakk.response.headers as String contains "application/pdf")
This version will extract it from the path (see the template definition in the spec):
send::
msg snakk.text (url ="/diesel/mock/test/getPdf2/Invo"
...)
expect:: (payload contains "line214"
)
send::
msg test.util.today
expect:: (payload ~= "....-..-.."
)
send::
msg test.util.addMonths (mths:Number=1
)
expect:: (payload ~= "....-..-.."
)
send::
msg snakk.json (url ="/diesel/mock/myActualServer/cr"
..., verb="POST"
)
expect:: (payload[="status"] is "Failed"
)
In a wiki context, inline js:
(this often fails within sbt in development, so it's skipped on localhost)
0 $mock::
testdiesel.p93
$ifc:: NOT (diesel[="isLocalhost"]) snakk.text (url ="/Topic/RestWikiTests?a=11&b=23"
...="/Topic/Re...)
send::
msg testdiesel.p93
expect:: (payload contains "34"
)$ifc:: NOT (diesel[="isLocalhost"])
expect:: (payload contains "haha"
)$ifc:: NOT (diesel[="isLocalhost"])
send::
msg snakk.text (url ="/diesel/rest/dieseltest/pub1"
...)
expect:: (payload is defined
)
send::
msg snakk.text (url ="/diesel/rest/dieseltest/nothin"
...)
expect:: (payload is undefined
)
expect:: (snakk[="response"][="code"] is 501
)
send::
msg snakk.text (url ="/diesel/mock/dieseltest/pub2"
...)
expect:: (payload is defined
)
send::
msg snakk.text (url ="/diesel/rest/dieseltest/pub3"
...)
expect:: (payload is undefined
)
expect:: (snakk[="response"][="headers"][="diesel-reason"="diesel-reason"] contains "no payload"
)
expect:: (snakk[="response"][="code"] is 404
)
send::
msg diesel.catch
val x=(dieselRealm[="diesel.rest.timeout.exclusions"="diesel.rest.timeout.exclusions"] || [])
send::
msg diesel.realm.set (diesel.rest.timeout.exclusions)
send::
msg snakk.text (url ="/diesel/mock/v1/local/myResour"
...)
expect:: (payload not defined
)
expect:: (snakk[="response"][="code"] is 504
)
send::
msg diesel.catch
Cleanup:
send::
msg diesel.realm.set (diesel.rest.timeout.exclusions)
payload wasn't always evaluated before returning. test with json val
send::
msg snakk.json (url ="/diesel/rest/dieseltest/332"
...)
expect:: (payload[="a"] is "332"
)
You need to log in to post a comment!