diff --git a/vlib/net/http/http_proxy_test.v b/vlib/net/http/http_proxy_test.v index f66000f228..af47bf3b82 100644 --- a/vlib/net/http/http_proxy_test.v +++ b/vlib/net/http/http_proxy_test.v @@ -1,8 +1,8 @@ module http import encoding.base64 -import net import net.urllib +import os const sample_proxy_url = 'https://localhost' 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' } -fn test_http_do() { - // params - host := urllib.URL{ - scheme: 'http' - host: 'httpbin.org' - } - path := '/get' - req := &Request{ - method: Method.get - url: 'http://httpbin.org/get' - } +fn test_http_proxy_do() { + env := os.environ() + mut env_proxy := '' - assert host.scheme == 'http' - assert host.host == 'httpbin.org' - - host_name, port := net.split_address(host.hostname())! - - 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 + for envvar in ['http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'] { + prox_val := env[envvar] or { continue } + if prox_val != '' { + env_proxy = env[envvar] } - - 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"}' - mock_result := parse_response(mock_response_text)! - - assert mock_result.status_code == 200 - assert mock_result.body.contains('"origin"') - assert str.starts_with('GET') - assert str.ends_with('\r\n\r\n') - - assert req.read_timeout > 0 - assert req.write_timeout > 0 - - // req.on_finish(req, u64(mock_response_text.len)) or { panic('on_finish callback failed') } - - assert mock_result.body.len > 0 + } + if env_proxy != '' { + println('Has usable proxy env vars') + proxy := new_http_proxy(env_proxy)! + mut header := new_header(key: .user_agent, value: 'vlib') + header.add_custom('X-Vlang-Test', 'proxied')! + res := proxy.http_do(urllib.parse('http://httpbin.org/headers')!, Method.get, + '/headers', &Request{ proxy: proxy, header: header })! + println(res.status_code) + println('he4aders ${res.header}') + assert res.status_code == 200 + // assert res.header.data['X-Vlang-Test'] == 'proxied' } 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.') } }