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

Handle Net::HTTP requests #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/escher/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ module Escher::Request
require 'escher/request/base'
require 'escher/request/hash_request'
require 'escher/request/rack_request'
require 'escher/request/net_http_request'
require 'escher/request/legacy_request'

require 'escher/request/factory'

end
end
3 changes: 2 additions & 1 deletion lib/escher/request/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def self.from_request(request)

when defined?(Rack::Request) && Rack::Request
RackRequest.new(request)

when Net::HTTPRequest
NetHttpRequest.new(request)
when Hash
HashRequest.new(request)

Expand Down
37 changes: 37 additions & 0 deletions lib/escher/request/net_http_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Escher
module Request
class NetHttpRequest < Base
def path
URI(request.path).path
end

def headers
request.each_header.to_a
end

def has_header?(header_key)
!!request[header_key]
end

def header(header_key)
request[header_key]
end

def method
request.method
end

def body
request.body || ''
end

def query_values
Addressable::URI.parse(request.path).query_values(Array) || []
end

def set_header(key, value)
request.add_field(key, value)
end
end
end
end
16 changes: 14 additions & 2 deletions spec/escher/request/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
require 'rack'
require 'rack/request'
require 'action_dispatch'
require 'net/http'

describe Escher::Request::Factory do

describe ".from_request" do
request_env = {Rack::PATH_INFO.to_s => "request-path"}
ruby_request_classes = [
Net::HTTP::Get, Net::HTTP::Head, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete,
Net::HTTP::Options, Net::HTTP::Trace, Net::HTTP::Patch, Net::HTTP::Propfind, Net::HTTP::Proppatch,
Net::HTTP::Mkcol, Net::HTTP::Copy, Net::HTTP::Move, Net::HTTP::Lock, Net::HTTP::Unlock
]

{
request_types = {

{uri: "request-path"} => Escher::Request::HashRequest,
Struct.new("Request", :uri).new("request-path") => Escher::Request::LegacyRequest,
Rack::Request.new(request_env) => Escher::Request::RackRequest,
ActionDispatch::Request.new(request_env) => Escher::Request::RackRequest

}.each do |request, expected_class|
}

ruby_request_classes.each do |net_http_request_type|
request_types[net_http_request_type.new("request-path")] = Escher::Request::NetHttpRequest
end

request_types.each do |request, expected_class|

context "the request to be wrapped is a #{request.class.name}" do

Expand Down
88 changes: 88 additions & 0 deletions spec/lib/escher/request/net_http_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'spec_helper'
require 'net/http'

describe Escher::Request::NetHttpRequest do
let(:default_headers) do
[
["accept-encoding", "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
["accept", "*/*"],
["user-agent", "Ruby"]
]
end
let(:default_header_key) { 'accept' }
let(:default_header_value) { '*/*' }
let(:path) { '/path?a=10&b=20' }
let(:request) { Net::HTTP::Post.new(path) }
let(:inexisting_header_key) { 'Inexisting-Header' }
subject(:net_http_request) { described_class.new(request) }

describe "#request" do
it 'returns the underlying request object' do
expect(net_http_request.request).to eq(request)
end
end

describe "#headers" do
it 'returns request headers' do
expect(net_http_request.headers).to eq(default_headers)
end
end

describe "#has_header?" do
it 'returns true if request has specified header' do
expect(net_http_request.has_header?(default_header_key)).to be true
end

it "returns false if request doesn't have specified header" do
expect(net_http_request.has_header?(inexisting_header_key)).to be false
end
end

describe "#header" do
it 'returns the value for the requested header' do
expect(net_http_request.header(default_header_key)).to eq(default_header_value)
end

it 'returns nil if header is not present' do
expect(net_http_request.header(inexisting_header_key)).to eq nil
end
end

describe "#method" do
it 'returns request method' do
expect(net_http_request.method).to eq('POST')
end
end

describe "#body" do
it 'returns request body' do
request.body = 'body'

expect(net_http_request.body).to eq('body')
end

it 'returns empty string if no body' do
expect(net_http_request.body).to eq('')
end
end

describe "#path" do
it 'returns request path' do
expect(net_http_request.path).to eq(URI(request.path).path)
end
end

describe "#query_values" do
it 'returns query values' do
expect(net_http_request.query_values).to eq([["a", "10"], ["b", "20"]])
end
end

describe "#set_header" do
it 'sets the header' do
net_http_request.set_header('test-header', 'test value')

expect(net_http_request.header('test-header')).to eq('test value')
end
end
end