2021-06-14 09:08:41 +02:00
|
|
|
module http
|
2020-01-16 18:16:11 +01:00
|
|
|
|
2021-06-14 09:08:41 +02:00
|
|
|
// internal tests have access to *everything in the module*
|
2020-01-16 18:16:11 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
struct HttpbinResponseBody {
|
|
|
|
args map[string]string
|
|
|
|
data string
|
|
|
|
files map[string]string
|
|
|
|
form map[string]string
|
|
|
|
headers map[string]string
|
2020-09-12 02:31:06 +02:00
|
|
|
json map[string]string
|
2020-01-16 18:16:11 +01:00
|
|
|
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
|
2020-04-26 16:25:54 +02:00
|
|
|
mut result := []Response{}
|
2020-01-16 18:16:11 +01:00
|
|
|
// Note: httpbin doesn't support head
|
|
|
|
for method in methods {
|
|
|
|
lmethod := method.to_lower()
|
2020-09-12 02:31:06 +02:00
|
|
|
config.method = method_from_str(method)
|
2022-05-13 05:56:21 +02:00
|
|
|
res := fetch(FetchConfig{ ...config, url: url + lmethod })?
|
2020-01-16 18:16:11 +01:00
|
|
|
// TODO
|
2022-05-29 19:27:18 +02:00
|
|
|
// body := json.decode(HttpbinResponseBody,res.body)?
|
2020-01-16 18:16:11 +01:00
|
|
|
result << res
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_bare() {
|
2021-06-14 09:08:41 +02:00
|
|
|
$if !network ? {
|
|
|
|
return
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
2021-06-14 09:08:41 +02:00
|
|
|
responses := http_fetch_mock([], FetchConfig{}) or { panic(err) }
|
2020-01-16 18:16:11 +01:00
|
|
|
for response in responses {
|
2021-07-24 19:47:45 +02:00
|
|
|
assert response.status() == .ok
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_with_data() {
|
2021-06-14 09:08:41 +02:00
|
|
|
$if !network ? {
|
|
|
|
return
|
2020-01-16 18:16:11 +01:00
|
|
|
}
|
2021-06-14 09:08:41 +02:00
|
|
|
responses := http_fetch_mock(['POST', 'PUT', 'PATCH', 'DELETE'],
|
|
|
|
data: 'hello world'
|
|
|
|
) or { panic(err) }
|
2020-01-16 18:16:11 +01:00
|
|
|
for response in responses {
|
2022-05-29 19:27:18 +02:00
|
|
|
payload := json.decode(HttpbinResponseBody, response.body) or { panic(err) }
|
2020-01-16 18:16:11 +01:00
|
|
|
assert payload.data == 'hello world'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_with_params() {
|
2021-06-14 09:08:41 +02:00
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
responses := http_fetch_mock([],
|
2021-08-04 11:44:41 +02:00
|
|
|
params: {
|
2021-06-14 09:08:41 +02:00
|
|
|
'a': 'b'
|
2020-01-16 18:16:11 +01:00
|
|
|
'c': 'd'
|
|
|
|
}
|
2021-06-14 09:08:41 +02:00
|
|
|
) or { panic(err) }
|
2020-01-16 18:16:11 +01:00
|
|
|
for response in responses {
|
2022-05-29 19:27:18 +02:00
|
|
|
// payload := json.decode(HttpbinResponseBody,response.body) or {
|
2021-03-01 00:18:14 +01:00
|
|
|
// panic(err)
|
2020-01-16 18:16:11 +01:00
|
|
|
// }
|
2021-07-24 19:47:45 +02:00
|
|
|
assert response.status() == .ok
|
2020-01-16 18:16:11 +01:00
|
|
|
// TODO
|
|
|
|
// assert payload.args['a'] == 'b'
|
|
|
|
// assert payload.args['c'] == 'd'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:17:33 +02:00
|
|
|
fn test_http_fetch_with_headers() ? {
|
2021-06-14 09:08:41 +02:00
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
2021-04-09 18:17:33 +02:00
|
|
|
mut header := new_header()
|
2022-05-13 05:56:21 +02:00
|
|
|
header.add_custom('Test-Header', 'hello world')?
|
2021-06-14 09:08:41 +02:00
|
|
|
responses := http_fetch_mock([],
|
2021-04-09 18:17:33 +02:00
|
|
|
header: header
|
2021-06-14 09:08:41 +02:00
|
|
|
) or { panic(err) }
|
2020-01-16 18:16:11 +01:00
|
|
|
for response in responses {
|
2022-05-29 19:27:18 +02:00
|
|
|
// payload := json.decode(HttpbinResponseBody,response.body) or {
|
2021-03-01 00:18:14 +01:00
|
|
|
// panic(err)
|
2020-01-16 18:16:11 +01:00
|
|
|
// }
|
2021-07-24 19:47:45 +02:00
|
|
|
assert response.status() == .ok
|
2020-01-16 18:16:11 +01:00
|
|
|
// TODO
|
|
|
|
// assert payload.headers['Test-Header'] == 'hello world'
|
|
|
|
}
|
|
|
|
}
|