diff --git a/lib/escher/request.rb b/lib/escher/request.rb index a06a8f2..8d453a0 100644 --- a/lib/escher/request.rb +++ b/lib/escher/request.rb @@ -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 \ No newline at end of file +end diff --git a/lib/escher/request/factory.rb b/lib/escher/request/factory.rb index 30839b4..8edad9b 100644 --- a/lib/escher/request/factory.rb +++ b/lib/escher/request/factory.rb @@ -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) diff --git a/lib/escher/request/net_http_request.rb b/lib/escher/request/net_http_request.rb new file mode 100644 index 0000000..3c0cd6f --- /dev/null +++ b/lib/escher/request/net_http_request.rb @@ -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 diff --git a/spec/escher/request/factory_spec.rb b/spec/escher/request/factory_spec.rb index dd61f9f..97b5c15 100644 --- a/spec/escher/request/factory_spec.rb +++ b/spec/escher/request/factory_spec.rb @@ -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 diff --git a/spec/lib/escher/request/net_http_request_spec.rb b/spec/lib/escher/request/net_http_request_spec.rb new file mode 100644 index 0000000..219d91c --- /dev/null +++ b/spec/lib/escher/request/net_http_request_spec.rb @@ -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