mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
http: refactor and reduce duplication
This commit is contained in:
committed by
Alexander Medvednikov
parent
756c6d2858
commit
84a6c019e8
101
vlib/net/http/http_httpbin_test.v
Normal file
101
vlib/net/http/http_httpbin_test.v
Normal file
@@ -0,0 +1,101 @@
|
||||
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
|
||||
res := fetch(url + lmethod, config) or {
|
||||
return error(err)
|
||||
}
|
||||
// TODO
|
||||
// body := json.decode(HttpbinResponseBody,res.text) or {
|
||||
// return error(err)
|
||||
// }
|
||||
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_code == 200
|
||||
}
|
||||
}
|
||||
|
||||
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_code == 200
|
||||
// TODO
|
||||
// assert payload.args['a'] == 'b'
|
||||
// assert payload.args['c'] == 'd'
|
||||
}
|
||||
}
|
||||
|
||||
fn test_http_fetch_with_headers() {
|
||||
// $if !network ? { return }
|
||||
responses := http_fetch_mock([], {
|
||||
headers: {
|
||||
'Test-Header': 'hello world'
|
||||
}
|
||||
}) or {
|
||||
panic(err)
|
||||
}
|
||||
for response in responses {
|
||||
// payload := json.decode(HttpbinResponseBody,response.text) or {
|
||||
// panic(err)
|
||||
// }
|
||||
assert response.status_code == 200
|
||||
// TODO
|
||||
// assert payload.headers['Test-Header'] == 'hello world'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user