Skip to content

Commit

Permalink
fixes integration & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevprice83 committed Apr 19, 2022
1 parent dceafc7 commit 9b6d442
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 87 deletions.
46 changes: 2 additions & 44 deletions spec/resty/http/proxy_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,55 +50,13 @@ describe('resty.http.proxy', function()
it('connects to the #http_proxy', function()
_M:reset({ http_proxy = 'http://127.0.0.1:1984' })

local request = { url = 'http://127.0.0.1:1984/request', method = 'GET' }
local request = { url = 'http://upstream:8091/request', method = 'GET' }
local proxy = assert(_M.new(request))

local res = assert(proxy:request(request))

assert.same(200, res.status)
assert.match('GET http://127.0.0.1:1984/request HTTP/1.1', res:read_body())
end)

-- Regression test. Ref: https://issues.jboss.org/browse/THREESCALE-2205
context('when different subdomains resolve to the same IP', function()
local request_domain_1 = { url = 'http://test.example.com/', method = 'GET' }
local request_domain_2 = { url = 'http://prod.example.com/', method = 'GET' }

before_each(function()
-- Make everything resolve to the same IP
local resty_resolver = require 'resty.resolver.http'
stub(resty_resolver, 'resolve', function() return "1.1.1.1", 80 end)
end)

context('and it uses a http proxy', function()
before_each(function()
_M:reset({ http_proxy = 'http://127.0.0.1:1984' })
end)

it('does not reuse the connection', function()
local proxy = _M.new(request_domain_1)
proxy:request(request_domain_1):read_body()
proxy:set_keepalive()

proxy = _M.new(request_domain_2)
assert.same(0, proxy:get_reused_times())
end)
end)

context('and it does not use an http proxy', function()
before_each(function()
_M:reset({})
end)

it('does not reuse the connection', function()
local proxy = _M.new(request_domain_1)
proxy:request(request_domain_1):read_body()
proxy:set_keepalive()

proxy = _M.new(request_domain_2)
assert.same(0, proxy:get_reused_times())
end)
end)
assert.match('GET http://upstream:8091/request HTTP/1.1', res:read_body())
end)
end)
end)
74 changes: 31 additions & 43 deletions t/http-proxy.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ __DATA__
It connects to backened and forwards request to the upstream.
--- env eval
(
'no_proxy' => '127.0.0.1,localhost,test-upstream.lvh.me',
'no_proxy' => '127.0.0.1,localhost',
)
--- configuration
{
Expand All @@ -37,7 +37,7 @@ It connects to backened and forwards request to the upstream.
"backend_authentication_type": "service_token",
"backend_authentication_value": "token-value",
"proxy": {
"api_backend": "http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/",
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{ "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 2 }
]
Expand All @@ -53,14 +53,13 @@ It connects to backened and forwards request to the upstream.
}
}
--- upstream
server_name test-upstream.lvh.me;
location / {
echo 'yay, api backend: $http_host';
}
--- request
GET /?user_key=value
--- response_body env
yay, api backend: test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT
yay, api backend: test:$TEST_NGINX_SERVER_PORT
--- error_code: 200
--- no_error_log
Expand Down Expand Up @@ -567,7 +566,7 @@ proxy request: CONNECT test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT HTTP/1.1
--- user_files fixture=tls.pl eval
=== TEST 11: Upstream Policy connection uses proxy
--- env eval
--- env random_port eval
("http_proxy" => $ENV{TEST_NGINX_HTTP_PROXY})
--- configuration
{
Expand Down Expand Up @@ -596,18 +595,14 @@ server_name test-upstream.lvh.me;
--- request
GET /test?user_key=test3
--- more_headers
GET /test?user_key=test3
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
User-Agent: Test::APIcast::Blackbox
ETag: foobar
--- response_body env
GET /test?user_key=test3 HTTP/1.1
X-Real-IP: 127.0.0.1
Host: test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT
User-Agent: Test::APIcast::Blackbox
ETag: foobar
--- expected_response_body_like_multiple eval
[[
qr{GET \/test\?user_key=test3 HTTP\/1\.1},
qr{ETag\: foobar},
qr{Connection\: close},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+}
]]
--- error_code: 200
--- error_log env
proxy request: GET http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/test?user_key=test3 HTTP/1.1
Expand Down Expand Up @@ -656,14 +651,14 @@ location /test {
--- request
GET /test?user_key=test3
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
User-Agent: Test::APIcast::Blackbox
ETag: foobar
--- expected_response_body_like_multiple eval
[[
qr{GET \/test\?user_key=test3 HTTP\/1\.1},
qr{ETag\: foobar},
qr{Connection\: close},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+}
]]
--- error_code: 200
Expand All @@ -674,7 +669,7 @@ proxy request: CONNECT test-upstream.lvh.me:$TEST_NGINX_RANDOM_PORT HTTP/1.1
--- user_files fixture=tls.pl eval
=== TEST 13: Upstream Policy connection uses proxy
--- ONLY
--- env eval
("http_proxy" => $ENV{TEST_NGINX_HTTP_PROXY})
--- configuration
{
Expand Down Expand Up @@ -707,17 +702,15 @@ server_name test-upstream.lvh.me;
POST /test?user_key=test3
this-is-some-request-body
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
ETag: foobar
--- expected_response_body_like_multiple eval
[[
qr{POST \/test\?user_key=test3 HTTP\/1\.1},
qr{ETag\: foobar},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+},
qr{Content\-Length\: 25},
qr{this\-is\-some\-request\-body},
]]
User-Agent: Test::APIcast::Blackbox
--- response_body env
POST /test?user_key=test3 HTTP/1.1
X-Real-IP: 127.0.0.1
Host: test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT
Content-Length: 25
User-Agent: Test::APIcast::Blackbox
this-is-some-request-body
--- error_code: 200
--- error_log env
proxy request: POST http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/test?user_key=test3 HTTP/1.1
Expand Down Expand Up @@ -770,12 +763,12 @@ location /test {
POST /test?user_key=test3
this-is-some-request-body
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
User-Agent: Test::APIcast::Blackbox
--- expected_response_body_like_multiple eval
[[
qr{POST \/test\?user_key=test3 HTTP\/1\.1},
qr{Connection\: close},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+},
qr{Content\-Length\: 25},
qr{this\-is\-some\-request\-body},
Expand Down Expand Up @@ -959,15 +952,10 @@ server_name test-upstream.lvh.me;
}
--- request
GET /test
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
--- expected_response_body_like_multiple eval
[[
qr{GET \/test HTTP\/1\.1},
qr{Connection\: close},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+},
]]
--- response_body env
GET /test HTTP/1.1
X-Real-IP: 127.0.0.1
Host: test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT
--- error_code: 200
--- error_log env
proxy request: GET http://test-upstream.lvh.me:$TEST_NGINX_SERVER_PORT/test HTTP/1.1
Expand Down Expand Up @@ -1017,14 +1005,14 @@ location /test {
--- request
GET /test
--- more_headers
User-Agent: test.lvh.me::APIcast::Blackbox
User-Agent: Test::APIcast::Blackbox
ETag: foobar
--- expected_response_body_like_multiple eval
[[
qr{GET \/test HTTP\/1\.1},
qr{ETag\: foobar},
qr{Connection\: close},
qr{User\-Agent\: test.lvh.me\:\:APIcast\:\:Blackbox},
qr{User\-Agent\: Test\:\:APIcast\:\:Blackbox},
qr{Host\: test-upstream\.lvh\.me\:\d+}
]]
--- error_code: 200
Expand Down

0 comments on commit 9b6d442

Please sign in to comment.