mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
Changed test to use httpbin.org
This commit is contained in:
parent
6b1cee2fe9
commit
c2c16dc3fa
1 changed files with 21 additions and 72 deletions
|
@ -1,8 +1,8 @@
|
||||||
module http
|
module http
|
||||||
|
|
||||||
import encoding.base64
|
import encoding.base64
|
||||||
import net
|
|
||||||
import net.urllib
|
import net.urllib
|
||||||
|
import os
|
||||||
|
|
||||||
const sample_proxy_url = 'https://localhost'
|
const sample_proxy_url = 'https://localhost'
|
||||||
const sample_auth_proxy_url = 'http://user:pass@localhost:8888'
|
const sample_auth_proxy_url = 'http://user:pass@localhost:8888'
|
||||||
|
@ -49,79 +49,28 @@ fn test_proxy_headers_authenticated() ? {
|
||||||
'Proxy-Connection: Keep-Alive\r\nProxy-Authorization: Basic ${auth_token}\r\n\r\n'
|
'Proxy-Connection: Keep-Alive\r\nProxy-Authorization: Basic ${auth_token}\r\n\r\n'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_http_do() {
|
fn test_http_proxy_do() {
|
||||||
// params
|
env := os.environ()
|
||||||
host := urllib.URL{
|
mut env_proxy := ''
|
||||||
scheme: 'http'
|
|
||||||
host: 'httpbin.org'
|
|
||||||
}
|
|
||||||
path := '/get'
|
|
||||||
req := &Request{
|
|
||||||
method: Method.get
|
|
||||||
url: 'http://httpbin.org/get'
|
|
||||||
}
|
|
||||||
|
|
||||||
assert host.scheme == 'http'
|
for envvar in ['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'] {
|
||||||
assert host.host == 'httpbin.org'
|
prox_val := env[envvar] or { continue }
|
||||||
|
if prox_val != '' {
|
||||||
host_name, port := net.split_address(host.hostname())!
|
env_proxy = env[envvar]
|
||||||
|
|
||||||
assert host_name == 'httpbin.org'
|
|
||||||
assert port == 80 || port == 0
|
|
||||||
|
|
||||||
full_url := if host.scheme == 'http' || host.scheme == 'https' {
|
|
||||||
port_part := if port == 80 || port == 0 { '' } else { ':${port}' }
|
|
||||||
'${host.scheme}://${host.host}${port_part}${path}'
|
|
||||||
} else {
|
|
||||||
'${host.scheme}://${host.host}${path}'
|
|
||||||
}
|
|
||||||
|
|
||||||
assert full_url.starts_with('http') || full_url.starts_with('https')
|
|
||||||
|| full_url.starts_with('/')
|
|
||||||
|
|
||||||
s_proxy := new_http_proxy('http://8.8.8.8:8080')!
|
|
||||||
assert s_proxy.host == '8.8.8.8:8080'
|
|
||||||
assert s_proxy.hostname == '8.8.8.8'
|
|
||||||
assert s_proxy.port == 8080
|
|
||||||
assert s_proxy.url == 'http://8.8.8.8:8080'
|
|
||||||
|
|
||||||
s_fetch := prepare(FetchConfig{
|
|
||||||
url: 'http://httpbin.org/get'
|
|
||||||
proxy: s_proxy
|
|
||||||
})!
|
|
||||||
|
|
||||||
str := req.build_request_headers(req.method, host_name, port, full_url)
|
|
||||||
|
|
||||||
// Test the request headers string was built correctly
|
|
||||||
println(s_fetch)
|
|
||||||
println(str)
|
|
||||||
assert str.len > 0
|
|
||||||
assert str.contains('GET') && str.contains('HTTP/1.1')
|
|
||||||
assert str.contains(s_fetch.url)
|
|
||||||
assert str.contains('Host: httpbin.org')
|
|
||||||
assert str.contains('User-Agent:')
|
|
||||||
|
|
||||||
if host.scheme == 'http' || host.scheme == 'https' {
|
|
||||||
$if windows {
|
|
||||||
println('Running on Windows. Test would skip SSL connection.')
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
mock_response_text := 'HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 32\r\n\r\n{"origin": "127.0.0.1", "url": "http://httpbin.org/get"}'
|
if env_proxy != '' {
|
||||||
mock_result := parse_response(mock_response_text)!
|
println('Has usable proxy env vars')
|
||||||
|
proxy := new_http_proxy(env_proxy)!
|
||||||
assert mock_result.status_code == 200
|
mut header := new_header(key: .user_agent, value: 'vlib')
|
||||||
assert mock_result.body.contains('"origin"')
|
header.add_custom('X-Vlang-Test', 'proxied')!
|
||||||
assert str.starts_with('GET')
|
res := proxy.http_do(urllib.parse('http://httpbin.org/headers')!, Method.get,
|
||||||
assert str.ends_with('\r\n\r\n')
|
'/headers', &Request{ proxy: proxy, header: header })!
|
||||||
|
println(res.status_code)
|
||||||
assert req.read_timeout > 0
|
println('he4aders ${res.header}')
|
||||||
assert req.write_timeout > 0
|
assert res.status_code == 200
|
||||||
|
// assert res.header.data['X-Vlang-Test'] == 'proxied'
|
||||||
// req.on_finish(req, u64(mock_response_text.len)) or { panic('on_finish callback failed') }
|
|
||||||
|
|
||||||
assert mock_result.body.len > 0
|
|
||||||
} else {
|
} else {
|
||||||
assert false, 'Should not reach here. Need to add more test cases'
|
println('Proxy env vars (HTTP_PROXY or HTTPS_PROXY) not set. Skipping test.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue