Skip to content

Commit

Permalink
Merge pull request #46 from mamantoha/proxy-redirect
Browse files Browse the repository at this point in the history
use proxy on redirects
  • Loading branch information
mamantoha authored Jan 5, 2018
2 parents a9d1627 + d4f34ad commit 08b976a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions spec/integration/proxy_spec.cr
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/integration/redirection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion src/crest/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion src/crest/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 08b976a

Please sign in to comment.