aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/http/http_httpbin_test.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/vlib/net/http/http_httpbin_test.v
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/vlib/net/http/http_httpbin_test.v')
-rw-r--r--v_windows/v/old/vlib/net/http/http_httpbin_test.v95
1 files changed, 95 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/net/http/http_httpbin_test.v b/v_windows/v/old/vlib/net/http/http_httpbin_test.v
new file mode 100644
index 0000000..2023099
--- /dev/null
+++ b/v_windows/v/old/vlib/net/http/http_httpbin_test.v
@@ -0,0 +1,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(url + lmethod, config) ?
+ // 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: map{
+ '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'
+ }
+}