rest-story Pub Share

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++.

TODO fix the xml

// $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)

Simple GET

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

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/dieselsample/echo"..., verb="GET", body="haha113")

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

Parsing values

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

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)

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)

send::  msg ctx.echo  (x)

TODO fix this template issue

// $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)

More ways to snakk TELNET

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

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

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

expect::  (payload contains "line214")

js inline binding

send::  msg test.util.today 

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

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

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

mail server example from main page

send::  msg snakk.json  (url ="/diesel/mock/myActualServer/cr"..., verb="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"])

404

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 

504

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)

Specific issue:

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


Was this useful?    

By: Razie | 2016-10-26 .. 2024-11-16 | Tags: story , dsl


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

You need to log in to post a comment!

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