|
| 1 | +require_relative "../test_helper" |
| 2 | + |
| 3 | +# Tests to see how different HTTP client version request get translated into |
| 4 | +# HTTP requests for the API backend proxied requests. |
| 5 | +# |
| 6 | +# Currently, these all get translated to HTTP 1.1 requests (since nginx doesn't |
| 7 | +# currently support 2.0 upstream requests: |
| 8 | +# https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version), |
| 9 | +# but if this changes in the behavior, we can update these tests (this is |
| 10 | +# mostly just about documenting current behavior). |
| 11 | +# |
| 12 | +# This uses Caddy as an API backend server, since it supports HTTP versions |
| 13 | +# 1-3. The current version of curl inside our image doesn't yet support HTTP |
| 14 | +# v3, though, so we don't have tests for that yet. |
| 15 | +class Test::Proxy::TestBackendHttpVersions < Minitest::Test |
| 16 | + include ApiUmbrellaTestHelpers::Setup |
| 17 | + |
| 18 | + parallelize_me! |
| 19 | + |
| 20 | + def setup |
| 21 | + super |
| 22 | + setup_server |
| 23 | + once_per_class_setup do |
| 24 | + prepend_api_backends([ |
| 25 | + { |
| 26 | + :frontend_host => "127.0.0.1", |
| 27 | + :backend_host => "localhost", |
| 28 | + :servers => [{ :host => "127.0.0.1", :port => $config["caddy"]["http_port"] }], |
| 29 | + :url_matches => [{ :frontend_prefix => "/#{unique_test_class_id}/http/", :backend_prefix => "/" }], |
| 30 | + }, |
| 31 | + { |
| 32 | + :frontend_host => "127.0.0.1", |
| 33 | + :backend_host => "localhost", |
| 34 | + :backend_protocol => "https", |
| 35 | + :servers => [{ :host => "127.0.0.1", :port => $config["caddy"]["https_port"] }], |
| 36 | + :url_matches => [{ :frontend_prefix => "/#{unique_test_class_id}/https/", :backend_prefix => "/" }], |
| 37 | + }, |
| 38 | + ]) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def test_httpv1_0_client |
| 43 | + # Make to HTTP server port, since current version of curl doesn't seem to |
| 44 | + # work with HTTP 1.0 without `--no-alpn` option, which Typhoeus doesn't |
| 45 | + # currently support. |
| 46 | + http_opts = http_options.merge(ssl_verifypeer: false, http_version: :httpv1_0) |
| 47 | + response = Typhoeus.get("http://localhost:#{$config["caddy"]["http_port"]}/", http_opts) |
| 48 | + assert_response_code(200, response) |
| 49 | + assert_match("HTTP/1.0 200 OK", response.response_headers) |
| 50 | + data = MultiJson.load(response.body) |
| 51 | + assert_equal("HTTP/1.0", data.fetch("http.request.proto")) |
| 52 | + |
| 53 | + response = Typhoeus.get("http://127.0.0.1:9080/#{unique_test_class_id}/http/", http_opts) |
| 54 | + assert_response_code(200, response) |
| 55 | + assert_match("HTTP/1.1 200 OK", response.response_headers) |
| 56 | + data = MultiJson.load(response.body) |
| 57 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 58 | + end |
| 59 | + |
| 60 | + def test_httpv1_1_client |
| 61 | + http_opts = http_options.merge(ssl_verifypeer: false, http_version: :httpv1_1) |
| 62 | + response = Typhoeus.get("https://localhost:#{$config["caddy"]["https_port"]}/", http_opts) |
| 63 | + assert_response_code(200, response) |
| 64 | + assert_match("HTTP/1.1 200 OK", response.response_headers) |
| 65 | + data = MultiJson.load(response.body) |
| 66 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 67 | + |
| 68 | + response = Typhoeus.get("http://127.0.0.1:9080/#{unique_test_class_id}/https/", http_opts) |
| 69 | + assert_response_code(200, response) |
| 70 | + assert_match("HTTP/1.1 200 OK", response.response_headers) |
| 71 | + data = MultiJson.load(response.body) |
| 72 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 73 | + end |
| 74 | + |
| 75 | + def test_httpv2_0_client |
| 76 | + http_opts = http_options.merge(ssl_verifypeer: false, http_version: :httpv2_0) |
| 77 | + response = Typhoeus.get("https://localhost:#{$config["caddy"]["https_port"]}/", http_opts) |
| 78 | + assert_response_code(200, response) |
| 79 | + assert_match("HTTP/2 200", response.response_headers) |
| 80 | + data = MultiJson.load(response.body) |
| 81 | + assert_equal("HTTP/2.0", data.fetch("http.request.proto")) |
| 82 | + |
| 83 | + response = Typhoeus.get("http://127.0.0.1:9080/#{unique_test_class_id}/https/", http_opts) |
| 84 | + assert_response_code(200, response) |
| 85 | + assert_match("HTTP/1.1 200", response.response_headers) |
| 86 | + data = MultiJson.load(response.body) |
| 87 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 88 | + end |
| 89 | + |
| 90 | + def test_httpv2_tls_client |
| 91 | + http_opts = http_options.merge(ssl_verifypeer: false, http_version: :httpv2_tls) |
| 92 | + response = Typhoeus.get("https://localhost:#{$config["caddy"]["https_port"]}/", http_opts) |
| 93 | + assert_response_code(200, response) |
| 94 | + assert_match("HTTP/2 200", response.response_headers) |
| 95 | + data = MultiJson.load(response.body) |
| 96 | + assert_equal("HTTP/2.0", data.fetch("http.request.proto")) |
| 97 | + |
| 98 | + response = Typhoeus.get("http://127.0.0.1:9080/#{unique_test_class_id}/https/", http_opts) |
| 99 | + assert_response_code(200, response) |
| 100 | + assert_match("HTTP/1.1 200", response.response_headers) |
| 101 | + data = MultiJson.load(response.body) |
| 102 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 103 | + end |
| 104 | + |
| 105 | + def test_httpv2_prior_knowledge_client |
| 106 | + http_opts = http_options.merge(ssl_verifypeer: false, http_version: :httpv2_prior_knowledge) |
| 107 | + response = Typhoeus.get("https://localhost:#{$config["caddy"]["https_port"]}/", http_opts) |
| 108 | + assert_response_code(200, response) |
| 109 | + assert_match("HTTP/2 200", response.response_headers) |
| 110 | + data = MultiJson.load(response.body) |
| 111 | + assert_equal("HTTP/2.0", data.fetch("http.request.proto")) |
| 112 | + |
| 113 | + response = Typhoeus.get("http://127.0.0.1:9080/#{unique_test_class_id}/https/", http_opts) |
| 114 | + assert_response_code(200, response) |
| 115 | + assert_match("HTTP/1.1 200", response.response_headers) |
| 116 | + data = MultiJson.load(response.body) |
| 117 | + assert_equal("HTTP/1.1", data.fetch("http.request.proto")) |
| 118 | + end |
| 119 | +end |
0 commit comments