Permalink
Browse files

completed clean up with functional request lib

  • Loading branch information...
1 parent a2a6450 commit d38052552b9680d82bcee0e34307fac64104b9c5 @hukl committed Aug 15, 2010
Showing with 69 additions and 15 deletions.
  1. +21 −1 lib/rig/http.rb
  2. +3 −3 lib/rig/http_header.rb
  3. +12 −2 lib/rig/http_response.rb
  4. +33 −9 test/test_http.rb
View
@@ -16,7 +16,27 @@ class HTTP
def initialize *options
@options = normalize_options( options )
@body = HTTPBody.new( @options )
- @header = HTTPHeader.new( @options, @body )
+
+ @options.merge!(
+ :content_type => @body.content_type,
+ :content_length => @body.content_length
+ )
+
+ @header = HTTPHeader.new( @options )
+ end
+
+ def send
+ begin
+ tcp_socket = TCPSocket.new( @options[:host], @options[:port] )
+ tcp_socket.write( @header.to_s)# + @body.to_s )
+ response = tcp_socket.read
+ rescue => exception
+ puts exception.message
+ ensure
+ tcp_socket.close
+ end
+
+ HTTPResponse.new( response ) || exception.message
end
def with_body?
@@ -1,16 +1,16 @@
module Rig
class HTTPHeader < Hash
- def initialize options, body
+ def initialize options
http_method = options[:http_method]
path = options[:path]
header = {
"" => "#{http_method} #{path} HTTP/1.1",
"Host" => options[:host],
"Origin" => "localhost",
- "Content-Length" => body.content_length,
- "Content-Type" => body.content_type
+ "Content-Length" => options[:content_length],
+ "Content-Type" => options[:content_type]
}.merge(
(options[:custom_header] || {})
).merge(
@@ -1,10 +1,14 @@
+require 'ruby-debug'
module Rig
class HTTPResponse
attr_reader :header, :body
def initialize response
- @header, @body = response.split(CRLF + CRLF)
+ parts = response.split(CRLF + CRLF)
+ @header = parts.delete_at( 0 )
+ @status = @header.match(/HTTP\/\d.\d\s(\d\d\d)/)[1]
+ @body = parts.join
parse_header
@@ -17,8 +21,14 @@ def initialize response
end
end
+ def status
+ @status ? @status.to_i : 666
+ end
+
def parse_header
- @header = @header.gsub(/^HTTP\/\d\.\d\s\d\d\d.+\r\n/, "")
+ status_line_regexp = /HTTP\/\d\.\d\s\d\d\d.+\r\n/
+ status_line = @header[status_line_regexp]
+ @header = @header.gsub(status_line_regexp, "Status: #{@status}")
@header = @header.split(CRLF)
@header = @header.map { |element| element.split(": ") }
@header = @header.inject({}) do |result, element|
View
@@ -21,7 +21,9 @@ class TestHttp < Test::Unit::TestCase
:port=>80,
:path=>"/",
:http_method=>"GET",
- :query => nil
+ :query => nil,
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -33,7 +35,9 @@ class TestHttp < Test::Unit::TestCase
:port=>80,
:path=>"/",
:http_method=>"POST",
- :query => nil
+ :query => nil,
+ :content_type=>"application/x-www-form-urlencoded; charset=UTF-8",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -45,7 +49,9 @@ class TestHttp < Test::Unit::TestCase
:port=>80,
:path=>"/",
:http_method=>"PUT",
- :query => nil
+ :query => nil,
+ :content_type=>"application/x-www-form-urlencoded; charset=UTF-8",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -57,7 +63,9 @@ class TestHttp < Test::Unit::TestCase
:port=>80,
:path=>"/",
:http_method=>"DELETE",
- :query => nil
+ :query => nil,
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -69,7 +77,9 @@ class TestHttp < Test::Unit::TestCase
:port=>80,
:path=>"/",
:http_method=>"GET",
- :query => nil
+ :query => nil,
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
@@ -86,7 +96,9 @@ class TestHttp < Test::Unit::TestCase
:port=>3000,
:path=>"/",
:http_method=>"GET",
- :query => nil
+ :query => nil,
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -98,7 +110,9 @@ class TestHttp < Test::Unit::TestCase
:port=>3000,
:path=>"/",
:http_method=>"GET",
- :query => "foo=bar"
+ :query => "foo=bar",
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -110,7 +124,9 @@ class TestHttp < Test::Unit::TestCase
:port=>3000,
:path=>"/",
:http_method=>"GET",
- :query => "foo=bar"
+ :query => "foo=bar",
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -122,7 +138,9 @@ class TestHttp < Test::Unit::TestCase
:port=>3000,
:path=>"/",
:http_method=>"GET",
- :query => "foo=bar"
+ :query => "foo=bar",
+ :content_type=>"text/plain",
+ :content_length=>0
}
assert_equal expected, request.options
end
@@ -184,6 +202,12 @@ class TestHttp < Test::Unit::TestCase
assert_equal expected, get.header.to_s
end
+ test "actual get request" do
+ request = HTTP.get( "http://www.spiegel.de" )
+ response = request.send
+ assert_equal 200, response.status
+ end
+
#test "multipart body gets properly created" do
# post = HTTP.new(
# :host => "localhost",

0 comments on commit d380525

Please sign in to comment.