Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test proxy client with auth #28

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions samples/with_proxy_server.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "../src/http_proxy"

def with_proxy_server(host = "127.0.0.1", port = 8080, &)
def with_proxy_server(host = "127.0.0.1", port = 8080, username = "user", password = "passwd", &)
wants_close = Channel(Nil).new

server = HTTP::Proxy::Server.new(handlers: [
HTTP::LogHandler.new,
HTTP::Proxy::Server::BasicAuth.new("user", "passwd"),
HTTP::Proxy::Server::BasicAuth.new(username, password),
]) do |context|
context.request.headers.add("X-Forwarded-For", host)
end
Expand All @@ -27,7 +27,7 @@ def with_proxy_server(host = "127.0.0.1", port = 8080, &)
yield host, port, wants_close
end

with_proxy_server do |_host, _port, wants_close|
with_proxy_server do |_host, _port, _username, _password, wants_close|
puts "start proxy client"

puts "HTTP Request:"
Expand Down
79 changes: 73 additions & 6 deletions spec/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe HTTP::Proxy::Client do

describe "HTTP::Client#proxy=" do
context HTTP::Client do
it "should make HTTP request with proxy" do
with_proxy_server do |host, port, wants_close|
it "should make HTTP request" do
with_proxy_server do |host, port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port)

uri = URI.parse("http://httpbin.org")
Expand All @@ -34,8 +34,8 @@ describe HTTP::Proxy::Client do
end
end

it "should make HTTPS request with proxy" do
with_proxy_server do |host, port, wants_close|
it "should make HTTPS request" do
with_proxy_server do |host, port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port)

uri = URI.parse("https://httpbin.org")
Expand All @@ -49,11 +49,78 @@ describe HTTP::Proxy::Client do
wants_close.send(nil)
end
end

it "fails if the proxy server is not reachable" do
with_proxy_server do |host, _port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, 8081)

uri = URI.parse("https://httpbin.org")
client = HTTP::Client.new(uri)

expect_raises IO::Error, "Failed to open TCP connection to httpbin.org:443 (Error connecting to '127.0.0.1:8081': Connection refused)" do
client.proxy = proxy_client
end
ensure
wants_close.send(nil)
end
end

context "with authentication" do
it "should make HTTP request" do
with_proxy_server(username: "user", password: "passwd") do |host, port, username, password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port, username: username, password: password)

uri = URI.parse("http://httpbin.org")
client = HTTP::Client.new(uri)
client.proxy = proxy_client
response = client.get("/get")

(client.proxy?).should eq(true)
(response.status_code).should eq(200)
ensure
wants_close.send(nil)
end
end

it "should make HTTPS request" do
with_proxy_server(username: "user", password: "passwd") do |host, port, username, password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port, username: username, password: password)

uri = URI.parse("https://httpbin.org")
client = HTTP::Client.new(uri)
client.proxy = proxy_client
response = client.get("/get")

(client.proxy?).should eq(true)
(response.status_code).should eq(200)
ensure
wants_close.send(nil)
end
end

it "should not be success without credentials" do
with_proxy_server(username: "user", password: "passwd") do |host, port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port, username: "invalid", password: "invalid")

uri = URI.parse("http://httpbin.org")
client = HTTP::Client.new(uri)
client.proxy = proxy_client
response = client.get("/get")

(client.proxy?).should eq(true)
(response.status_code).should eq(407) # 407 Proxy Authentication Required


ensure
wants_close.send(nil)
end
end
end
end

context HTTP::Request do
it "should make HTTP::Request request with proxy" do
with_proxy_server do |host, port, wants_close|
with_proxy_server do |host, port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port)

uri = URI.parse("http://httpbin.org")
Expand All @@ -74,7 +141,7 @@ describe HTTP::Proxy::Client do
describe "HTTP::Client#set_proxy" do
context HTTP::Client do
it "should make HTTP request with proxy" do
with_proxy_server do |host, port, wants_close|
with_proxy_server do |host, port, _username, _password, wants_close|
proxy_client = HTTP::Proxy::Client.new(host, port)

uri = URI.parse("http://httpbin.org")
Expand Down
12 changes: 9 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ describe HTTP::Proxy do
end
end

def with_proxy_server(host = "127.0.0.1", port = 8080, &)
def with_proxy_server(host = "127.0.0.1", port = 8080, username : String? = nil, password : String? = nil, &)
wants_close = Channel(Nil).new
server = HTTP::Proxy::Server.new

server =
if username && password
HTTP::Proxy::Server.new(handlers: [HTTP::Proxy::Server::BasicAuth.new(username, password)])
else
HTTP::Proxy::Server.new
end

spawn do
server.bind_tcp(host, port)
Expand All @@ -23,5 +29,5 @@ def with_proxy_server(host = "127.0.0.1", port = 8080, &)

Fiber.yield

yield host, port, wants_close
yield host, port, username, password, wants_close
end
2 changes: 1 addition & 1 deletion src/ext/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module HTTP

# Configures this client to perform proxy basic authentication in every
# request.
private def proxy_basic_auth(username : String?, password : String?)
private def proxy_basic_auth(username : String?, password : String?) : Nil
header = "Basic #{Base64.strict_encode("#{username}:#{password}")}"
before_request do |request|
request.headers["Proxy-Authorization"] = header
Expand Down