-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.bqn
executable file
·188 lines (183 loc) · 5.89 KB
/
tests.bqn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bqn
⟨
Get,Post,
OpenSession,ResetSession,CloseSession,
SetURL,SetHeaders,SetVerbose,SetTimeout,SetTimeoutms,SetData,
Perform,
⟩←•Import"curl.bqn"
httpbinURL←"http://localhost:8080"
RunTest←{•term.OutRaw (4⥊' ')∾"Test "∾𝕨 ⋄ 𝕏 @ ⋄ •Out ((69-≠𝕨)⥊'.')∾"OK"}
RunSuite←{•Out 𝕨∾':' ⋄ RunTest´¨𝕩 ⋄ •Out " All "∾(•Fmt ≠𝕩)∾" tests passed."}
RunAll←{RunSuite´¨𝕩 ⋄ •Out "All "∾(•Fmt +´≠¨⊢´¨𝕩)∾" tests passed."}
simpleAPI_testsuite←⟨
"simple GET"‿{𝕊𝕩:
r←Get httpbinURL∾"/get"
! 200=r.code
! 0=r.redirectCount
! 1=⊑"HTTP/"⍷r.headers
! 1=+´"Content-Type: "⍷r.headers
! 1=+´"Content-Length: "⍷r.headers
! 1=+´"""Host"": ""localhost:8080"""⍷r.content
! 1=+´"""User-Agent"": ""curl/bqn"""⍷r.content
! 0=+´"""Hello"": ""World"""⍷r.content
},
"simple GET with headers"‿{𝕊𝕩:
headers←⟨"User-Agent: toto","Content-Type: application/json","Hello: World"⟩
r←headers 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
! 1=+´"""Hello"": ""World"""⍷r.content
},
"simple POST with headers"‿{𝕊𝕩:
headers←⟨"Content-Type: application/json"⟩
r←headers Post ⟨httpbinURL∾"/post","{""key"": ""value""}"⟩
! 200=r.code
! 0=r.redirectCount
! 1=⊑"HTTP/"⍷r.headers
! 1=+´"Content-Type: "⍷r.headers
! 1=+´"Content-Length: "⍷r.headers
! 1=+´"""Content-Type"": ""application/json"""⍷r.content
! 1=+´"""data"": ""{\""key\"": \""value\""}"""⍷r.content
}
⟩
advancedAPI_testsuite←⟨
"open session"‿{𝕊𝕩:
session←OpenSession @
! 8=≠session.sessionPtr
! (8↑0)≢session.headersSlist
CloseSession session
},
"GET"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/get") SetURL session
! 200=r.code
! 0=r.redirectCount
! 10≤≠r.content
CloseSession session
},
"POST"‿{𝕊𝕩:
session←OpenSession @
r←Perform "toto"SetData (httpbinURL∾"/post") SetURL session
! 200=r.code
! 0=r.redirectCount
! 1=+´"toto"⍷r.content
CloseSession session
},
"basic auth fail"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/basic-auth/username/s3cr3tpa55word") SetURL session
r←Perform session
! 401=r.code
CloseSession session
},
"basic auth with header"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/basic-auth/username/s3cr3tpa55word") SetURL session
r←Perform ⟨"Authorization: Basic dXNlcm5hbWU6czNjcjN0cGE1NXdvcmQ="⟩ SetHeaders session
! 200=r.code
CloseSession session
},
"basic auth with URL"‿{𝕊𝕩:
session←OpenSession @
session↩("http://username:s3cr3tpa55word@localhost:8080/basic-auth/username/s3cr3tpa55word") SetURL session
r←Perform session
! 200=r.code
CloseSession session
},
"bearer auth fail"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/bearer") SetURL session
r←Perform session
! 401=r.code
CloseSession session
},
"bearer auth"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/bearer") SetURL session
r←Perform ⟨"Authorization: Bearer hey!"⟩ SetHeaders session
! 200=r.code
CloseSession session
},
"status GET"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/status/418") SetURL session
! 418=r.code
CloseSession session
},
"status POST"‿{𝕊𝕩:
session←OpenSession @
r←Perform ""SetData (httpbinURL∾"/status/418") SetURL session
! 418=r.code
CloseSession session
},
"set headers"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/headers") SetURL session
r←Perform ⟨"Hello: World"⟩ SetHeaders session
! 1=+´"""Hello"": ""World"""⍷r.content
CloseSession session
},
"default user-agent"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/user-agent") SetURL session
r←Perform session
! 1=+´"curl/bqn"⍷r.content
CloseSession session
},
"custom user-agent"‿{𝕊𝕩:
session←OpenSession @
session↩(httpbinURL∾"/user-agent") SetURL session
r←Perform ⟨"User-Agent: hello/world"⟩ SetHeaders session
! 0=+´"curl/bqn"⍷r.content
! 1=+´"hello/world"⍷r.content
CloseSession session
},
"timeout with fast request"‿{𝕊𝕩:
session←OpenSession @
session↩1010 SetTimeoutms (httpbinURL∾"/delay/1") SetURL session
r←Perform session
! 200=r.code
CloseSession session
},
"request timeout"‿{𝕊𝕩:
session←OpenSession @
session↩100 SetTimeoutms (httpbinURL∾"/delay/1") SetURL session
! Perform⎊1 session
CloseSession session
},
"follow redirects"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
CloseSession session
},
"follow absolute redirects"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/absolute-redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
CloseSession session
},
"follow relative redirects"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/relative-redirect/5") SetURL session
! 200=r.code
! 5=r.redirectCount
CloseSession session
},
"parameters"‿{𝕊𝕩:
session←OpenSession @
r←Perform (httpbinURL∾"/anything?hello=world&zig=zag") SetURL session
! 1=+´"""hello"": ""world"""⍷r.content
! 1=+´"""zig"": ""zag"""⍷r.content
CloseSession session
},
⟩
RunAll⟨
"Simple API"‿simpleAPI_testsuite,
"Advanced API"‿advancedAPI_testsuite,
⟩