Skip to content

Commit

Permalink
added support for every custom header you may want
Browse files Browse the repository at this point in the history
  • Loading branch information
ilpoldo committed May 6, 2010
1 parent a4358e7 commit 83fc055
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions fredo.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{fredo}
s.version = "0.1.8"
s.version = "0.1.9"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["ilpoldo"]
s.date = %q{2010-05-02}
s.date = %q{2010-05-04}
s.description = %q{Mocks web services by plugging Net::Http straight into Rack}
s.email = %q{ilpoldo@gmail.com}
s.extra_rdoc_files = [
Expand Down
34 changes: 19 additions & 15 deletions lib/fredo/ext/net_http.rb
Expand Up @@ -50,27 +50,31 @@ def request_with_fredo(request, body = nil, &block)
protocol = use_ssl? ? "https" : "http"
full_path = "#{protocol}://#{self.address}:#{self.port}#{request.path}"

rack_env ={'REQUEST_METHOD' => request.method,
'SCRIPT_NAME' => '',
'PATH_INFO' => uri.path,
'QUERY_STRING' => (uri.query || ''),
'SERVER_NAME' => self.address,
'SERVER_PORT' => self.port,
'rack.version' => [1,1],
'rack.url_scheme' => protocol,
'rack.input' => body,
'rack.errors' => $stderr,
'rack.multithread' => true,
'rack.multiprocess' => true,
'rack.run_once' => false}
# It's for testing, we care about all the headers
request_headers = request.instance_variable_get :@header
request_headers = Hash[*request_headers.map {|k,v| [String(k).upcase.gsub(%r{[^_0-9A-Z]},"_"),v]}.flatten]

rack_env =request_headers.merge({'REQUEST_METHOD' => request.method,
'SCRIPT_NAME' => '',
'PATH_INFO' => uri.path,
'QUERY_STRING' => (uri.query || ''),
'SERVER_NAME' => self.address,
'SERVER_PORT' => self.port,
'rack.version' => [1,1],
'rack.url_scheme' => protocol,
'rack.input' => body,
'rack.errors' => $stderr,
'rack.multithread' => true,
'rack.multiprocess' => true,
'rack.run_once' => false})

# @socket = Net::HTTP.socket_type.new
# Perform the request
status, header, body = Fredo.call(rack_env)
status, headers, body = Fredo.call(rack_env)

response = Net::HTTPResponse.send(:response_class, "#{status}").new("1.0", "#{status}", body)
response.instance_variable_set(:@body, body)
header.each { |name, value| response[name] = value }
headers.each { |name, value| response[name] = value }
response.instance_variable_set(:@read, true)

def response.read_body(*args, &block)
Expand Down
13 changes: 12 additions & 1 deletion spec/fredo_spec.rb
Expand Up @@ -126,8 +126,19 @@
end

http = Net::HTTP.new('www.gossip.com')
http.post('/', body, 'content-type' => 'text/plain').body.should eql(body)
http.post('/', body, 'Content-Type' => 'text/plain').body.should eql(body)
end

end

context "headers" do
it "keeps request headers" do
Fredo.post 'http://www.headheavy.com' do
"I got #{request.env['CONTENT_TYPE']}"
end

http = Net::HTTP.new('www.headheavy.com')
http.post('/', '<foo></foo>', 'content-type' => 'application/xml').body.should eql('I got application/xml')
end
end
end

0 comments on commit 83fc055

Please sign in to comment.