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
|
module http
// internal tests have access to *everything in the module*
import json
struct HttpbinResponseBody {
args map[string]string
data string
files map[string]string
form map[string]string
headers map[string]string
json map[string]string
origin string
url string
}
fn http_fetch_mock(_methods []string, _config FetchConfig) ?[]Response {
url := 'https://httpbin.org/'
methods := if _methods.len == 0 { ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] } else { _methods }
mut config := _config
mut result := []Response{}
// Note: httpbin doesn't support head
for method in methods {
lmethod := method.to_lower()
config.method = method_from_str(method)
res := fetch(FetchConfig{ ...config, url: url + lmethod }) ?
// TODO
// body := json.decode(HttpbinResponseBody,res.text)?
result << res
}
return result
}
fn test_http_fetch_bare() {
$if !network ? {
return
}
responses := http_fetch_mock([], FetchConfig{}) or { panic(err) }
for response in responses {
assert response.status() == .ok
}
}
fn test_http_fetch_with_data() {
$if !network ? {
return
}
responses := http_fetch_mock(['POST', 'PUT', 'PATCH', 'DELETE'],
data: 'hello world'
) or { panic(err) }
for response in responses {
payload := json.decode(HttpbinResponseBody, response.text) or { panic(err) }
assert payload.data == 'hello world'
}
}
fn test_http_fetch_with_params() {
$if !network ? {
return
}
responses := http_fetch_mock([],
params: {
'a': 'b'
'c': 'd'
}
) or { panic(err) }
for response in responses {
// payload := json.decode(HttpbinResponseBody,response.text) or {
// panic(err)
// }
assert response.status() == .ok
// TODO
// assert payload.args['a'] == 'b'
// assert payload.args['c'] == 'd'
}
}
fn test_http_fetch_with_headers() ? {
$if !network ? {
return
}
mut header := new_header()
header.add_custom('Test-Header', 'hello world') ?
responses := http_fetch_mock([],
header: header
) or { panic(err) }
for response in responses {
// payload := json.decode(HttpbinResponseBody,response.text) or {
// panic(err)
// }
assert response.status() == .ok
// TODO
// assert payload.headers['Test-Header'] == 'hello world'
}
}
|