root/trunk/tests/test_httpclient.rb

Revision 786, 5.5 kB (checked in by francis, 10 months ago)

supported optional version string in HTTP client.

  • Property svn:keywords set to Id
Line 
1 # $Id$
2 #
3 # Author:: Francis Cianfrocca (gmail: blackhedd)
4 # Homepage::  http://rubyeventmachine.com
5 # Date:: 8 April 2006
6 #
7 # See EventMachine and EventMachine::Connection for documentation and
8 # usage examples.
9 #
10 #----------------------------------------------------------------------------
11 #
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13 # Gmail: blackhedd
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of either: 1) the GNU General Public License
17 # as published by the Free Software Foundation; either version 2 of the
18 # License, or (at your option) any later version; or 2) Ruby's License.
19 #
20 # See the file COPYING for complete licensing information.
21 #
22 #---------------------------------------------------------------------------
23 #
24 #
25 #
26
27 $:.unshift "../lib"
28 require 'eventmachine'
29 require 'test/unit'
30
31 class TestHttpClient < Test::Unit::TestCase
32
33     Localhost = "127.0.0.1"
34     Localport = 9801
35
36   def setup
37   end
38
39   def teardown
40   end
41
42   #-------------------------------------
43
44   def test_http_client
45     ok = false
46     EventMachine.run {
47       c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
48       c.callback {
49         ok = true
50         EventMachine.stop
51       }
52       c.errback {EventMachine.stop} # necessary, otherwise a failure blocks the test suite forever.
53     }
54     assert ok
55   end
56
57   #-------------------------------------
58
59   def test_http_client_1
60     ok = false
61     EventMachine.run {
62       c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
63       c.callback {ok = true; EventMachine.stop}
64       c.errback {EventMachine.stop}
65     }
66     assert ok
67   end
68
69   #-------------------------------------
70
71   def test_http_client_2
72     ok = false
73     EventMachine.run {
74       c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
75       c.callback {|result|
76         ok = true;
77         EventMachine.stop
78       }
79       c.errback {EventMachine.stop}
80     }
81     assert ok
82   end
83
84
85   #-----------------------------------------
86
87   # Test a server that returns a page with a zero content-length.
88   # This caused an early version of the HTTP client not to generate a response,
89   # causing this test to hang. Observe, there was no problem with responses
90   # lacking a content-length, just when the content-length was zero.
91   #
92   class EmptyContent < EventMachine::Connection
93       def initialize *args
94           super
95       end
96       def receive_data data
97           send_data "HTTP/1.0 404 ...\r\nContent-length: 0\r\n\r\n"
98           close_connection_after_writing
99       end
100   end
101
102   def test_http_empty_content
103       ok = false
104       EventMachine.run {
105           EventMachine.start_server "127.0.0.1", 9701, EmptyContent
106           c = EventMachine::Protocols::HttpClient.send :request, :host => "127.0.0.1", :port => 9701
107           c.callback {|result|
108               ok = true
109               EventMachine.stop
110           }
111       }
112       assert ok
113   end
114
115
116   #---------------------------------------
117
118   class PostContent < EventMachine::Protocols::LineAndTextProtocol
119       def initialize *args
120           super
121           @lines = []
122       end
123       def receive_line line
124           if line.length > 0
125               @lines << line
126           else
127               process_headers
128           end
129       end
130       def receive_binary_data data
131           @post_content = data
132           send_response
133       end
134       def process_headers
135           if @lines.first =~ /\APOST ([^\s]+) HTTP\/1.1\Z/
136               @uri = $1.dup
137           else
138               raise "bad request"
139           end
140           @lines.each {|line|
141               if line =~ /\AContent-length:\s*(\d+)\Z/i
142                   @content_length = $1.dup.to_i
143               elsif line =~ /\AContent-type:\s*(\d+)\Z/i
144                   @content_type = $1.dup
145               end
146           }
147
148           raise "invalid content length" unless @content_length
149           set_binary_mode @content_length
150       end
151       def send_response
152           send_data "HTTP/1.1 200 ...\r\nConnection: close\r\nContent-length: 10\r\nContent-type: text/html\r\n\r\n0123456789"
153           close_connection_after_writing
154       end
155   end
156
157   # TODO, this is WRONG. The handler is asserting an HTTP 1.1 request, but the client
158   # is sending a 1.0 request. Gotta fix the client
159   def test_post
160       response = nil
161       EventMachine.run {
162           EventMachine.start_server Localhost, Localport, PostContent
163           EventMachine.add_timer(2) {raise "timed out"}
164           c = EventMachine::Protocols::HttpClient.request :host=>Localhost,
165               :port=>Localport, :method=>:post, :request=>"/aaa", :content=>"XYZ",
166               :content_type=>"text/plain"
167           c.callback {|r|
168               response = r
169               EventMachine.stop
170           }
171       }
172
173       assert_equal( 200, response[:status] )
174       assert_equal( "0123456789", response[:content] )
175   end
176
177
178   # TODO, need a more intelligent cookie tester.
179   # In fact, this whole test-harness needs a beefier server implementation.
180   def test_cookie
181     ok = false
182     EM.run {
183       c = EM::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80, :cookie=>"aaa=bbb"
184       c.callback {|result|
185         ok = true;
186         EventMachine.stop
187       }
188       c.errback {EventMachine.stop}
189     }
190     assert ok
191   end
192
193
194
195   # We can tell the client to send an HTTP/1.0 request (default is 1.1).
196   # This is useful for suppressing chunked responses until those are working.
197   def test_version_1_0
198     ok = false
199     EM.run {
200       c = EM::P::HttpClient.request :host => "www.bayshorenetworks.com",
201         :port => 80,
202         :version => "1.0"
203       c.callback {|result|
204         ok = true;
205         EventMachine.stop
206       }
207       c.errback {EventMachine.stop}
208     }
209     assert ok
210   end
211
212
213 end
214
215
Note: See TracBrowser for help on using the browser.