diff --git a/shard.yml b/shard.yml index e96790be..1579714a 100644 --- a/shard.yml +++ b/shard.yml @@ -13,5 +13,8 @@ development_dependencies: kemal: github: sdogruyol/kemal version: ~> 0.22.0 + http_proxy: + github: mamantoha/http_proxy + version: ~> 0.3.1 license: MIT diff --git a/spec/integration/proxy_spec.cr b/spec/integration/proxy_spec.cr new file mode 100644 index 00000000..f19bfdf1 --- /dev/null +++ b/spec/integration/proxy_spec.cr @@ -0,0 +1,33 @@ +require "../spec_helper" + +describe Crest do + describe "With proxy server" do + it "should make request" do + with_proxy_server do |host, port, wants_close| + response = Crest.get("#{TEST_SERVER_URL}/", p_addr: host, p_port: port) + (response.status_code).should eq(200) + (response.body).should contain("Hello World!") + (response.request.p_addr).should eq("127.0.0.1") + (response.request.p_port).should eq(8080) + ensure + wants_close.send(nil) + end + end + + it "should redirect with proxy" do + with_proxy_server do |host, port, wants_close| + response = Crest.get("#{TEST_SERVER_URL}/redirect", p_addr: "127.0.0.1", p_port: 8080) + (response.status_code).should eq(200) + (response.body).should contain("Hello World!") + (response.url).should eq("#{TEST_SERVER_URL}/") + (response.history.size).should eq(1) + (response.history.first.url).should eq("#{TEST_SERVER_URL}/redirect") + (response.history.first.status_code).should eq(302) + (response.request.p_addr).should eq("127.0.0.1") + (response.request.p_port).should eq(8080) + ensure + wants_close.send(nil) + end + end + end +end diff --git a/spec/integration/redirection_spec.cr b/spec/integration/redirection_spec.cr index 4c025c7c..742aa586 100644 --- a/spec/integration/redirection_spec.cr +++ b/spec/integration/redirection_spec.cr @@ -12,6 +12,12 @@ describe Crest do (response.history.first.status_code).should eq(302) end + it "should redirect with logger" do + response = Crest.get("#{TEST_SERVER_URL}/redirect", logging: true) + (response.request.logging).should eq(true) + (response.request.logger).should be_a(Crest::Logger) + end + it "should raise error when too many redirects" do expect_raises Crest::RequestFailed, "HTTP status code 302" do Crest.get("#{TEST_SERVER_URL}/redirect/circle1") diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 522e21c9..2ab05801 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,5 +1,24 @@ require "spec" require "json" +require "http_proxy" require "../src/crest" TEST_SERVER_URL = "http://127.0.0.1:4567" + +def with_proxy_server(host = "127.0.0.1", port = 8080) + wants_close = Channel(Nil).new + server = HTTP::Proxy::Server.new(host, port) + + spawn do + server.listen + end + + spawn do + wants_close.receive + server.close + end + + Fiber.yield + + yield host, port, wants_close +end diff --git a/src/crest/request.cr b/src/crest/request.cr index c8c5dbba..8ecab903 100644 --- a/src/crest/request.cr +++ b/src/crest/request.cr @@ -17,7 +17,9 @@ module Crest @logger : Crest::Logger @logging : Bool - getter method, url, payload, headers, cookies, max_redirects, user, password, proxy, logging, logger + getter method, url, payload, headers, cookies, max_redirects, user, password, + proxy, logging, logger, p_addr, p_port, p_user, p_pass + # An array of previous redirection responses property redirection_history diff --git a/src/crest/response.cr b/src/crest/response.cr index 5448bf0c..44130314 100644 --- a/src/crest/response.cr +++ b/src/crest/response.cr @@ -45,7 +45,11 @@ module Crest max_redirects: max_redirects, cookies: cookies, logging: @request.logging, - logger: @request.logger + logger: @request.logger, + p_addr: @request.p_addr, + p_port: @request.p_port, + p_user: @request.p_user, + p_pass: @request.p_pass ) # append self to redirection history