rest-story Pub

Testing remote snakking

These are a few tests and examples of snakking REST and Telnet. See :

  • how messages become REST services
  • how to call one message via REST, with templates
  • hot to use JSON, XML etc and more

The specs for these messages are in rest spec++.

Simple GET

Test also that diesel.ping is public and contains some things:

As text:

send::  msg snakk.text  (url ="/diesel/react/diesel.ping"...="/diesel/r...)

expect::  (payload contains "limitedRequests")
expect::  (payload containsNot "NOPEGlobal.limitedApiRequests")

As json:

send::  msg snakk.json  (url ="/diesel/react/diesel.ping"...="/diesel/r...)

expect::  (payload[="global"][="limitedRequests"] is defined)
expect::  (payload[=""] not defined)

POST with headers:

send::  msg snakk.json  (url ="/diesel/mock/diesel/echoJson"...="/diesel/m..., verb="POST"="POST", body="haha113"="haha113", headers:JSON={Content-T...)

expect::  (payload[="headers"][="Content-Type"="Content-Type"] is "application/texteh")

Snakk specific return values

TODO DEPRECATED - remove snakkHttpCode and snakkHttpHeaders

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)

GET with body

Call a simple echo service to get the body back:

send::  msg snakk.json  (url ="/diesel/mock/diesel/echoJson"...="/diesel/m..., verb="GET"="GET", body="haha113"="haha113")

expect::  (payload[="body"] is "haha113")

Parsing values

If you need to parse strings...

Parsing JSON

send::  msg ctx.set  (payload="{"status": "Failed"}"="{"status"...)

send::  msg snakk.parse.json 

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

Parsing regex - use named groups

send::  msg ctx.set  (payload="{"status": "Failed"}"="{"status"...)

send::  msg snakk.parse.regex  (regex :Regex="/(?s){.*status": *"(?<status>"...=/(?s){.*s...)

expect::  (status is "Failed")

Headers in response

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"...="/diesel/m...)

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=snakk[="re...)

other

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=true)

send::  msg ctx.echo  (x=diesel[="r...)

send::  msg test.diesel.testqueryp  (p1="1"="1", p2="2"="2")

expect::  (sumf == "12")

send::  msg test.diesel.testjson  (p1="3"="3", p2="4"="4")

expect::  (sumf == "34")

send::  msg test.diesel.testpattern  (p1="5"="5", p2="6"="6")

expect::  (sumf == 11)

More ways to snakk TELNET

Direct snakk from a TELNET service:

send::  msg snakk.telnet  (host="www.google.ca"="www.googl..., port="80"="80", body="GET / HTTP 1.1\n\r"="GET / HTT...)

expect::  (payload contains "400 Bad Request")

Same thing via a template:

// $send test.diesel.telnetGoogle // $expect (payload contains "404 Not Found")

TODO this specific telnet test does not work on specs.dieselapps.com - works on local!

// $send test.diesel.telnetsample(p1="7", p2="8") // $expect (payload contains "78")

send::  msg test.diesel.testXmlRequestOut  (p1="1"="1", p2="2"="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"="1", p2="2"="2")

expect::  (sumxml is "12")

More template versions

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"...="/diesel/m...)

expect::  (payload contains "line214")

js inline binding

send::  msg test.util.today 

expect::  (payload ~= "....-..-..")

send::  msg test.util.addMonths  (mths:Number=1=1)

expect::  (payload ~= "....-..-..")

mail server example from main page

send::  msg snakk.json  (url ="/diesel/mock/myActualServer/cr"...="/diesel/m..., verb="POST"="POST")

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

Query params

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


Was this useful?    

By: Razie | 2016-10-26 .. 2022-06-16 | Tags: story , dsl


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

You need to log in to post a comment!

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