Skip to content

Commit

Permalink
Wrap the mongrel adapters request body to be string-like.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwgoldman committed Feb 9, 2012
1 parent 07f65d0 commit 8bf7d1c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/webmachine/adapters/mongrel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def process(wreq, wres)
request = Webmachine::Request.new(wreq.params["REQUEST_METHOD"],
URI.parse(wreq.params["REQUEST_URI"]),
header,
wreq.body || StringIO.new(''))
RequestBody.new(wreq))

response = Webmachine::Response.new
@dispatcher.dispatch(request, response)
Expand Down Expand Up @@ -81,6 +81,23 @@ def process(wreq, wres)
end
end # class Handler

class RequestBody
attr_reader :request

def initialize(request)
@request = request
end

def to_s
request.body.rewind
request.body.read
end

def each(&block)
request.body.each(&block)
end
end # class RequestBody

end # module Mongrel
end # module Adapters
end # module Webmachine
30 changes: 30 additions & 0 deletions spec/webmachine/adapters/mongrel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@
it "implements #run" do
adapter.should respond_to(:run)
end

describe "request handler" do
let(:request_params) do
{
"REQUEST_METHOD" => "GET",
"REQUEST_URI" => "http://www.example.com/test?query=string"
}
end
let(:request_body) { StringIO.new("Hello, World!") }
let(:mongrel_request) { stub(:params => request_params, :body => request_body) }
let(:mongrel_response) { stub.as_null_object }

subject { Webmachine::Adapters::Mongrel::Handler.new(dispatcher) }

it "should build a string-like request body" do
dispatcher.should_receive(:dispatch) do |request, response|
request.body.to_s.should eq("Hello, World!")
end
subject.process(mongrel_request, mongrel_response)
end

it "should build an enumerable request body" do
chunks = []
dispatcher.should_receive(:dispatch) do |request, response|
request.body.each { |chunk| chunks << chunk }
end
subject.process(mongrel_request, mongrel_response)
chunks.join.should eq("Hello, World!")
end
end
end
rescue LoadError
warn "Platform is #{RUBY_PLATFORM}: skipping mongrel adapter spec."
Expand Down

0 comments on commit 8bf7d1c

Please sign in to comment.