-
Notifications
You must be signed in to change notification settings - Fork 27
/
testing.gleam
289 lines (266 loc) · 8.54 KB
/
testing.gleam
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import gleam/bit_array
import gleam/bytes_builder
import gleam/crypto
import gleam/http
import gleam/http/request
import gleam/json.{type Json}
import gleam/option.{None, Some}
import gleam/string
import gleam/string_builder
import gleam/uri
import simplifile
import wisp.{type Request, type Response, Bytes, Empty, File, Text}
/// The default secret key base used for test requests.
/// This should never be used outside of tests.
///
pub const default_secret_key_base: String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/// Create a test HTTP request that can be used to test your request handler
/// functions.
///
/// Note not all HTTP methods are expected to have an accompanying body, so when
/// using this function directly over other functions such as `get` and `post`
/// take care to ensure you are not providing a body when it is not expected.
///
/// The `default_secret_key_base` constant is used as the secret key base for
/// requests made with this function.
///
pub fn request(
method: http.Method,
path: String,
headers: List(http.Header),
body: BitArray,
) -> Request {
let #(path, query) = case string.split(path, "?") {
[path, query] -> #(path, Some(query))
_ -> #(path, None)
}
request.Request(
method: method,
headers: headers,
body: body,
scheme: http.Https,
host: "localhost",
port: None,
path: path,
query: query,
)
|> request.set_body(wisp.create_canned_connection(
body,
default_secret_key_base,
))
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn get(path: String, headers: List(http.Header)) -> Request {
request(http.Get, path, headers, <<>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn post(path: String, headers: List(http.Header), body: String) -> Request {
request(http.Post, path, headers, <<body:utf8>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The body parameters are encoded as form data and the `content-type` header
/// is set to `application/x-www-form-urlencoded`.
///
pub fn post_form(
path: String,
headers: List(http.Header),
data: List(#(String, String)),
) -> Request {
let body = uri.query_to_string(data)
request(http.Post, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/x-www-form-urlencoded")
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The `content-type` header is set to `application/json`.
///
pub fn post_json(
path: String,
headers: List(http.Header),
data: Json,
) -> Request {
let body = json.to_string(data)
request(http.Post, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/json")
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn head(path: String, headers: List(http.Header)) -> Request {
request(http.Head, path, headers, <<>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn put(path: String, headers: List(http.Header), body: String) -> Request {
request(http.Put, path, headers, <<body:utf8>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The body parameters are encoded as form data and the `content-type` header
/// is set to `application/x-www-form-urlencoded`.
///
pub fn put_form(
path: String,
headers: List(http.Header),
data: List(#(String, String)),
) -> Request {
let body = uri.query_to_string(data)
request(http.Put, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/x-www-form-urlencoded")
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The `content-type` header is set to `application/json`.
///
pub fn put_json(path: String, headers: List(http.Header), data: Json) -> Request {
let body = json.to_string(data)
request(http.Put, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/json")
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn delete(path: String, headers: List(http.Header), body: String) -> Request {
request(http.Delete, path, headers, <<body:utf8>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The body parameters are encoded as form data and the `content-type` header
/// is set to `application/x-www-form-urlencoded`.
///
pub fn delete_form(
path: String,
headers: List(http.Header),
data: List(#(String, String)),
) -> Request {
let body = uri.query_to_string(data)
request(http.Delete, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/x-www-form-urlencoded")
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The `content-type` header is set to `application/json`.
///
pub fn delete_json(
path: String,
headers: List(http.Header),
data: Json,
) -> Request {
let body = json.to_string(data)
request(http.Delete, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/json")
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn trace(path: String, headers: List(http.Header)) -> Request {
request(http.Trace, path, headers, <<>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn connect(path: String, headers: List(http.Header)) -> Request {
request(http.Connect, path, headers, <<>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn options(path: String, headers: List(http.Header)) -> Request {
request(http.Options, path, headers, <<>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
pub fn patch(path: String, headers: List(http.Header), body: String) -> Request {
request(http.Patch, path, headers, <<body:utf8>>)
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The body parameters are encoded as form data and the `content-type` header is set to `application/x-www-form-urlencoded`.
///
pub fn patch_form(
path: String,
headers: List(http.Header),
data: List(#(String, String)),
) -> Request {
let body = uri.query_to_string(data)
request(http.Patch, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/x-www-form-urlencoded")
}
/// Create a test HTTP request that can be used to test your request handler.
///
/// The `content-type` header is set to `application/json`.
///
pub fn patch_json(
path: String,
headers: List(http.Header),
data: Json,
) -> Request {
let body = json.to_string(data)
request(http.Patch, path, headers, <<body:utf8>>)
|> request.set_header("content-type", "application/json")
}
/// Read the body of a response as a string.
///
/// # Panics
///
/// This function will panic if the response body is a file and the file cannot
/// be read, or if it does not contain valid UTF-8.
///
pub fn string_body(response: Response) -> String {
case response.body {
Empty -> ""
Text(builder) -> string_builder.to_string(builder)
Bytes(bytes) -> {
let data = bytes_builder.to_bit_array(bytes)
let assert Ok(string) = bit_array.to_string(data)
string
}
File(path) -> {
let assert Ok(contents) = simplifile.read(path)
contents
}
}
}
/// Read the body of a response as a bit string
///
/// # Panics
///
/// This function will panic if the response body is a file and the file cannot
/// be read.
///
pub fn bit_array_body(response: Response) -> BitArray {
case response.body {
Empty -> <<>>
Bytes(builder) -> bytes_builder.to_bit_array(builder)
Text(builder) ->
bytes_builder.to_bit_array(bytes_builder.from_string_builder(builder))
File(path) -> {
let assert Ok(contents) = simplifile.read_bits(path)
contents
}
}
}
/// Set a header on a request.
///
/// # Examples
///
/// ```gleam
/// let request =
/// test.request(test.Get, "/", [], <<>>)
/// |> test.set_header("content-type", "application/json")
/// request.headers
/// // => [#("content-type", "application/json")]
/// ```
pub const set_header = request.set_header
/// Set a cookie on the request.
///
pub fn set_cookie(
req: Request,
name: String,
value: String,
security: wisp.Security,
) -> Request {
let value = case security {
wisp.PlainText -> bit_array.base64_encode(<<value:utf8>>, False)
wisp.Signed -> wisp.sign_message(req, <<value:utf8>>, crypto.Sha512)
}
request.set_cookie(req, name, value)
}