Add tests

This commit is contained in:
Dimitri Lozeve 2023-10-02 19:40:19 +02:00
parent a72fe043b2
commit 3c42890e15

154
tests.bqn Normal file → Executable file
View file

@ -1,10 +1,20 @@
Get,Post•Import"curl.bqn"
#!/usr/bin/env bqn
Get,Post,
OpenSession,ResetSession,CloseSession,
SetURL,SetHeaders,SetVerbose,SetTimeout,SetTimeoutms,SetData,
Perform,
•Import"curl.bqn"
httpbinURL"http://localhost:8080"
r0
## Simple API
•Out " Test GET"
rGet httpbinURL"/get"
rGet httpbinURL"/get"
! 200=r.code
! 0=r.redirectCount
! 1="HTTP/"r.headers
! 1=+´"Content-Type: "r.headers
! 1=+´"Content-Length: "r.headers
@ -16,6 +26,7 @@ r←Get httpbinURL∾"/get"
•Out " Test GET with headers"
r"User-Agent: toto","Content-Type: application/json","Hello: World"Get httpbinURL"/get"
! 200=r.code
! 0=r.redirectCount
! 1=+´"""Host"": ""localhost:8080"""r.content
! 1=+´"""User-Agent"": ""toto"""r.content
! 1=+´"""Content-Type"": ""application/json"""r.content
@ -25,6 +36,7 @@ r↩⟨"User-Agent: toto","Content-Type: application/json","Hello: World"⟩Get
•Out " Test POST with headers"
r"Content-Type: application/json"PosthttpbinURL"/post","{""key"": ""value""}"
! 200=r.code
! 0=r.redirectCount
! 1="HTTP/"r.headers
! 1=+´"Content-Type: "r.headers
! 1=+´"Content-Length: "r.headers
@ -32,4 +44,142 @@ r↩⟨"Content-Type: application/json"⟩Post⟨httpbinURL∾"/post","{""key"":
! 1=+´"""data"": ""{\""key\"": \""value\""}"""r.content
•Out "OK"
## Advanced API
•Out " Test open session"
sessionOpenSession @
! 8=session.sessionPtr
! (80)session.headersSlist
•Out "OK"
•Out " Test GET"
sessionResetSession session
rPerform (httpbinURL"/get") SetURL session
! 200=r.code
! 0=r.redirectCount
! 10r.content
•Out "OK"
•Out " Test POST"
sessionResetSession session
rPerform "toto"SetData (httpbinURL"/post") SetURL session
! 200=r.code
! 0=r.redirectCount
! 1=+´"toto"r.content
•Out "OK"
•Out " Test basic auth fail"
sessionResetSession session
session(httpbinURL"/basic-auth/username/s3cr3tpa55word") SetURL session
rPerform session
! 401=r.code
•Out "OK"
•Out " Test basic auth with header"
sessionResetSession session
session(httpbinURL"/basic-auth/username/s3cr3tpa55word") SetURL session
rPerform "Authorization: Basic dXNlcm5hbWU6czNjcjN0cGE1NXdvcmQ=" SetHeaders session
! 200=r.code
•Out "OK"
•Out " Test basic auth with URL"
sessionResetSession session
session("http://username:s3cr3tpa55word@localhost:8080/basic-auth/username/s3cr3tpa55word") SetURL session
rPerform session
! 200=r.code
•Out "OK"
•Out " Test bearer auth fail"
sessionResetSession session
session(httpbinURL"/bearer") SetURL session
rPerform session
! 401=r.code
•Out "OK"
•Out " Test bearer auth"
sessionResetSession session
session(httpbinURL"/bearer") SetURL session
rPerform "Authorization: Bearer hey!" SetHeaders session
! 200=r.code
•Out "OK"
•Out " Test status GET"
sessionResetSession session
rPerform (httpbinURL"/status/418") SetURL session
! 418=r.code
•Out "OK"
•Out " Test status POST"
sessionResetSession session
rPerform ""SetData (httpbinURL"/status/418") SetURL session
! 418=r.code
•Out "OK"
•Out " Test set headers"
sessionResetSession session
session(httpbinURL"/headers") SetURL session
rPerform "Hello: World" SetHeaders session
! 1=+´"""Hello"": ""World"""r.content
•Out "OK"
•Out " Test default user-agent"
sessionResetSession session
session(httpbinURL"/user-agent") SetURL session
rPerform session
! 1=+´"curl/bqn"r.content
•Out "OK"
•Out " Test custom user-agent"
sessionResetSession session
session(httpbinURL"/user-agent") SetURL session
rPerform "User-Agent: hello/world" SetHeaders session
! 0=+´"curl/bqn"r.content
! 1=+´"hello/world"r.content
•Out "OK"
•Out " Test timeout with fast request"
sessionResetSession session
session1010 SetTimeoutms (httpbinURL"/delay/1") SetURL session
rPerform session
! 200=r.code
•Out "OK"
•Out " Test request timeout"
sessionResetSession session
session100 SetTimeoutms (httpbinURL"/delay/1") SetURL session
! Perform1 session
•Out "OK"
•Out " Test follow redirects"
sessionResetSession session
rPerform (httpbinURL"/redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
•Out "OK"
•Out " Test follow absolute redirects"
sessionResetSession session
rPerform (httpbinURL"/absolute-redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
•Out "OK"
•Out " Test follow relative redirects"
sessionResetSession session
rPerform (httpbinURL"/relative-redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
•Out "OK"
•Out " Test parameters"
sessionResetSession session
rPerform (httpbinURL"/anything?hello=world&zig=zag") SetURL session
! 1=+´"""hello"": ""world"""r.content
! 1=+´"""zig"": ""zag"""r.content
•Out "OK"
•Out " Test close session"
CloseSession session
•Out "OK"
•Out "All tests passed."