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.

Send this test message - which will in turn snakk another service, via REST URL - see the spec.

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.google.ca", port="80"="80", body="GET / HTTP 1.1\n\r"="GET / HTTP 1.1\n\r")

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

Same thing via a template:

send::  msg 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

send::  msg snakk.text  (url ="/diesel/mock/test/ge"...="/diesel/mock/test/getPdf/Invoice-1.pdf?invoiceId=123")

expect::  (payload contains "line197")
// $expect (http.headers contains "application/pdf")

TODO match the response headers somehow

This version will extract it from the path (see the template definition in the spec):

send::  msg snakk.text  (url ="/diesel/mock/test/ge"...="/diesel/mock/test/getPdf2/Invoice-1/pdf")

expect::  (payload contains "line214")

Headers in response

Headers are grouped in snakkHttpHeaders and are lowercase:

send::  msg snakk.text  (url ="/diesel/mock/test/ge"...="/diesel/mock/test/getPdf/Invoice-1.pdf?invoiceId=123")

expect::  (snakkHttpHeaders[="content-length"="content-length"] > 0)
expect::  (snakkHttpHeaders[="content-type"="content-type"] is defined)
expect::  (snakkHttpHeaders[="content-type"="content-type"] contains "pdf")
expect::  (snakkHttpHeaders[="Content-TypeNOT_THERE"="Content-TypeNOT_THERE"] not defined)
send::  msg ctx.echo  (m=snakkHttpHeaders[="content-type"="content-type"])

snakk.http.headers

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/myActua"...="/diesel/mock/myActualServer/create/John@", 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"...="/Topic/RestWikiTests?a=11&b=23&c=ha&d=ha")

send::  msg testdiesel.p93 

expect::  (payload contains "34")$ifc::NOT (diesel[="isLocalhost"])
expect::  (payload contains "haha")$ifc::NOT (diesel[="isLocalhost"])

Parsing values

Parsing JSON

send::  msg snakk.text  (url ="/diesel/mock/myActua"...="/diesel/mock/myActualServer/create/John@", verb="POST"="POST")

send::  msg snakk.parse.json 

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

Parsing regex - use named groups

send::  msg snakk.text  (url ="/diesel/mock/myActua"...="/diesel/mock/myActualServer/create/John@", verb="POST"="POST")

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

GET with body

send::  msg snakk.text  (url="/diesel/mock/getbody"="/diesel/mock/getbody", verb="GET"="GET", body="haha113"="haha113")

expect::  (payload contains "haha113")

diesel.ping is public

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

expect::  (payload[="global"][="limitedApiRequests"] is defined)
expect::  (payload[="NOPEGlobal.limitedApiRequests"="NOPEGlobal.limitedApiRequests"] not defined)


Was this useful?    

By: Razie | 2016-10-26 .. 2020-12-26 | Tags: story , dsl


Viewed 280 times ( | Print ) this page.

You need to log in to post a comment!

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