Last active
December 21, 2015 00:58
-
-
Save Porta/6224038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Poxy | |
| class RequestFactory | |
| def initialize(target, rack_request) | |
| @rp = Poxy::RequestProcessor.new(rack_request) | |
| method, headers, query_string, params, body = @rp.parsed | |
| url = fix_url(target, query_string) | |
| headers = fix_headers(headers) | |
| HTTPI.adapter = :curb | |
| @request = HTTPI::Request.new( | |
| headers: headers, | |
| url: url, | |
| body: params | |
| ) | |
| begin | |
| @response = HTTPI.send(method.downcase.to_sym, @request) | |
| rescue Curl::Err::ConnectionFailedError | |
| @response = HTTPI::Response.new(503, {}, ["No response from server"]) | |
| end | |
| end | |
| def response | |
| @response | |
| end | |
| def fix_headers(headers) | |
| copy = headers | |
| headers = {} | |
| copy.select {|k,v| k.downcase.start_with?("x_")}.collect { |pair| headers[pair[0]] = pair[1] } | |
| headers['X-VIA'] = "Poxy 1.0, http://poxy.ws" | |
| headers['X-Forwarded-For'] = copy['ORIGIN'] | |
| headers | |
| end | |
| def fix_url(target, query_string) | |
| if query_string.present? | |
| target + "?" + query_string | |
| else | |
| target | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Poxy | |
| class RequestFactory | |
| attr_accessor :url, :method, :headers, :query_string, :body | |
| def initialize(target, opts = {}) | |
| @method = opts[:method].downcase.to_sym | |
| @url = target | |
| @headers = add_via(opts[:headers]) | |
| @query_string = opts[:query_string] | |
| #TODO: Add uploads from original request? | |
| @body = opts[:body] | |
| @timeout = opts[:open_timeout] || 30 | |
| HTTPI.adapter = :curb | |
| @request = HTTPI::Request.new( | |
| url: @url, | |
| query: @query_string, | |
| headers: @headers, | |
| body: @body, | |
| open_timeout: @timeout | |
| ) | |
| end | |
| def response | |
| begin | |
| @response = HTTPI.send(@method, @request) | |
| rescue Curl::Err::ConnectionFailedError | |
| @response = HTTPI::Response.new(503, {}, ["No response from server"]) | |
| rescue Curl::Err::GotNothingError | |
| @response = HTTPI::Response.new(404, {}, ["Not found"]) | |
| rescue NoMethodError | |
| @response = HTTPI::Response.new(405, {}, ["Method not allowed"]) | |
| end | |
| @response | |
| end | |
| def add_via(headers) | |
| headers['X-VIA'] = "Poxy 1.0, http://poxy.ws" | |
| headers['X-Forwarded-For'] = headers['ORIGIN'] | |
| headers | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #poxy_request_factory | |
| setup do | |
| @req_opts = { method: "GET", headers: {}, query_string: "foo=bar" , body: "", open_timeout: 1} | |
| @req = Poxy::RequestFactory.new('http://example.com:8080', @req_opts) | |
| end | |
| test "it should response a 503" do || | |
| assert_equal @req.response.code, HTTPI::Response.new(503, {}, ["No response from server"]).code | |
| end | |
| test "url should match " do | |
| req = Poxy::RequestFactory.new('http://example.com:8080', @req_opts) | |
| assert_equal req.url, 'http://example.com:8080' | |
| end | |
| test "query string should match" do | |
| query_string = "foo=bar" | |
| @req_opts[:query_string] = query_string | |
| req = Poxy::RequestFactory.new('http://example.com:8080', @req_opts) | |
| assert_equal req.query_string, query_string | |
| end | |
| test "it should be a :get request" do | |
| assert_equal @req.method, @req_opts[:method].downcase.to_sym | |
| end | |
| test "body should be present" do | |
| body = { user_id: 123, active: false } | |
| @req_opts[:body] = body | |
| req = Poxy::RequestFactory.new('http://example.com:8080', @req_opts) | |
| assert_equal req.body, body | |
| end | |
| test "X-VIA should be present in request headers" do | |
| assert_equal @req.headers["X-VIA"], "Poxy 1.0, http://poxy.ws" | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment