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

expect::  (sumf == "12")

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

expect::  (sumf == "34")

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

expect::  (sumf == 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 "400 Bad")

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

send::  msg snakk.text  (url<-"/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/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/getPdf/Invoice-1.pdf?invoiceId=123")

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

snakk.http.headers

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/create/John@", 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)

send::  msg snakk.text  (url<-"/Topic/RestWikiTests?a=11&b=23&c=ha&d=ha")

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/myActualServer/create/John@", verb<-"POST")

send::  msg snakk.parse.json 

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

Parsing regex - use named groups

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

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

GET with body

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

expect::  (payload contains "haha113")


Was this useful?    

By: Razie | 2016-10-26 .. 2019-09-30 | Tags: story , dsl


Viewed 123 times ( | Print ) this page.

You need to log in to post a comment!

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