2020-01-16 20:16:11 +03:00
|
|
|
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
|
2020-09-12 03:31:06 +03:00
|
|
|
json map[string]string
|
2020-01-16 20:16:11 +03: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 17:25:54 +03:00
|
|
|
mut result := []Response{}
|
2020-01-16 20:16:11 +03:00
|
|
|
// Note: httpbin doesn't support head
|
|
|
|
for method in methods {
|
|
|
|
lmethod := method.to_lower()
|
2020-09-12 03:31:06 +03:00
|
|
|
config.method = method_from_str(method)
|
2020-08-29 02:58:03 +03:00
|
|
|
res := fetch(url + lmethod, config)?
|
2020-01-16 20:16:11 +03:00
|
|
|
// TODO
|
2020-08-29 02:58:03 +03:00
|
|
|
// body := json.decode(HttpbinResponseBody,res.text)?
|
2020-01-16 20:16:11 +03:00
|
|
|
result << res
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_bare() {
|
2020-01-16 21:44:16 +03:00
|
|
|
$if !network ? { return }
|
2020-01-16 20:16:11 +03:00
|
|
|
responses := http_fetch_mock([], FetchConfig{}) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
for response in responses {
|
|
|
|
assert response.status_code == 200
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_with_data() {
|
2020-01-16 21:44:16 +03:00
|
|
|
$if !network ? { return }
|
2020-01-16 20:16:11 +03:00
|
|
|
responses := http_fetch_mock(['POST', 'PUT', 'PATCH', 'DELETE'], {
|
|
|
|
data: 'hello world'
|
|
|
|
}) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
for response in responses {
|
|
|
|
payload := json.decode(HttpbinResponseBody,response.text) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
assert payload.data == 'hello world'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_with_params() {
|
2020-01-16 21:44:16 +03:00
|
|
|
$if !network ? { return }
|
2020-01-16 20:16:11 +03:00
|
|
|
responses := http_fetch_mock([], {
|
|
|
|
params: {
|
|
|
|
'a': 'b',
|
|
|
|
'c': 'd'
|
|
|
|
}
|
|
|
|
}) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
for response in responses {
|
|
|
|
// payload := json.decode(HttpbinResponseBody,response.text) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
// panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
// }
|
|
|
|
assert response.status_code == 200
|
|
|
|
// TODO
|
|
|
|
// assert payload.args['a'] == 'b'
|
|
|
|
// assert payload.args['c'] == 'd'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_fetch_with_headers() {
|
2020-01-16 21:44:16 +03:00
|
|
|
$if !network ? { return }
|
2020-01-16 20:16:11 +03:00
|
|
|
responses := http_fetch_mock([], {
|
|
|
|
headers: {
|
|
|
|
'Test-Header': 'hello world'
|
|
|
|
}
|
|
|
|
}) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
}
|
|
|
|
for response in responses {
|
|
|
|
// payload := json.decode(HttpbinResponseBody,response.text) or {
|
2021-03-01 02:18:14 +03:00
|
|
|
// panic(err)
|
2020-01-16 20:16:11 +03:00
|
|
|
// }
|
|
|
|
assert response.status_code == 200
|
|
|
|
// TODO
|
|
|
|
// assert payload.headers['Test-Header'] == 'hello world'
|
|
|
|
}
|
|
|
|
}
|