root/trunk/tests/test_hc.rb

Revision 788, 5.9 kB (checked in by raggi, 8 months ago)

Merge of branches/raggi
Most notable work and patches by Aman Gupta, Roger Pack, and James Tucker.
Patches / Tickets also submitted by: Jeremy Evans, aanand, darix, mmmurf,
danielaquino, macournoyer.

  • Moved docs into docs/ dir
  • Major refactor of rakefile, added generic rakefile helpers in tasks
  • Added example CPP build rakefile in tasks/cpp.rake
  • Moved rake tests out to tasks/tests.rake
  • Added svn ignores where appropriate
  • Fixed jruby build on older java platforms
  • Gem now builds from Rakefile rather than directly via extconf
  • Gem unified for jruby, C++ and pure ruby.
  • Correction for pure C++ build, removing ruby dependency
  • Fix for CYGWIN builds on ipv6
  • Major refactor for extconf.rb
  • Working mingw builds
  • extconf optionally uses pkg_config over manual configuration
  • extconf builds for 1.9 on any system that has 1.9
  • extconf no longer links pthread explicitly
  • looks for kqueue on all *nix systems
  • better error output on std::runtime_error, now says where it came from
  • Fixed some tests on jruby
  • Added test for general send_data flaw, required for a bugfix in jruby build
  • Added timeout to epoll tests
  • Added fixes for java reactor ruby api
  • Small addition of some docs in httpclient.rb and httpcli2.rb
  • Some refactor and fixes in smtpserver.rb
  • Added parenthesis where possible to avoid excess ruby warnings
  • Refactor of $eventmachine_library logic for accuracy and maintenance, jruby
  • EM::start_server now supports unix sockets
  • EM::connect now supports unix sockets
  • EM::defer @threadqueue now handled more gracefully
  • Added better messages on exceptions raised
  • Fix edge case in timer fires
  • Explicitly require buftok.rb
  • Add protocols to autoload, rather than require them all immediately
  • Fix a bug in pr_eventmachine for outbound_q
  • Refactors to take some of the use of defer out of tests.
  • Fixes in EM.defer under start/stop conditions. Reduced scope of threads.
  • 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 TestHeaderAndContentProtocol < Test::Unit::TestCase
32
33   TestHost = "127.0.0.1"
34   TestPort = 8905
35
36   class SimpleTest < EventMachine::Protocols::HeaderAndContentProtocol
37     attr_reader :first_header, :my_headers, :request
38
39     def receive_first_header_line hdr
40       @first_header ||= []
41       @first_header << hdr
42     end
43     def receive_headers hdrs
44       @my_headers ||= []
45       @my_headers << hdrs
46     end
47     def receive_request hdrs, content
48       @request ||= []
49       @request << [hdrs, content]
50     end
51   end
52
53   def test_no_content
54     the_connection = nil
55     EventMachine.run {
56       EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
57         the_connection = conn
58       end
59       EventMachine.add_timer(4) {raise "test timed out"}
60
61       client = Module.new do
62         def unbind
63           EM.add_timer(0.1) { EM.stop }
64         end
65
66         def post_init
67           send_data [ "aaa\n", "bbb\r\n", "ccc\n", "\n" ].join
68           close_connection_after_writing
69         end
70       end
71
72       EventMachine.connect( TestHost, TestPort, client )
73     }
74     assert_equal( ["aaa"], the_connection.first_header )
75     assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers )
76     assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request )
77   end
78
79   def test_content
80     the_connection = nil
81     content = "A" * 50
82     headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
83     EventMachine.run {
84       EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
85         the_connection = conn
86       end
87       EventMachine.add_timer(4) { assert(false, 'test timeout'); EM.stop }
88
89       client = Module.new do
90         define_method(:headers) { headers }
91         define_method(:content) { content }
92
93         def unbind
94           EM.add_timer(0.1) { EM.stop }
95         end
96
97         def post_init
98           headers.each { |h| send_data "#{h}\r\n" }
99           send_data "\n"
100           send_data content
101           close_connection_after_writing
102         end
103       end
104
105       EventMachine.connect( TestHost, TestPort, client )
106
107     }
108     assert_equal( ["aaa"], the_connection.first_header )
109     assert_equal( [headers], the_connection.my_headers )
110     assert_equal( [[headers, content]], the_connection.request )
111   end
112
113   def test_several_requests
114     the_connection = nil
115     content = "A" * 50
116     headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
117     EventMachine.run {
118       EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
119         the_connection = conn
120       end
121       EventMachine.add_timer(4) { assert(false, 'test timeout'); EM.stop }
122
123       client = Module.new do
124         define_method(:headers) { headers }
125         define_method(:content) { content }
126
127         def unbind
128           EM.add_timer(0.1) { EM.stop }
129         end
130
131         def post_init
132           5.times do
133             headers.each { |h| send_data "#{h}\r\n" }
134             send_data "\n"
135             send_data content
136           end
137           close_connection_after_writing
138         end
139       end
140
141       EventMachine.connect( TestHost, TestPort, client )
142     }
143     assert_equal( ["aaa"] * 5, the_connection.first_header )
144     assert_equal( [headers] * 5, the_connection.my_headers )
145     assert_equal( [[headers, content]] * 5, the_connection.request )
146   end
147
148
149   # def x_test_multiple_content_length_headers
150   #   # This is supposed to throw a RuntimeError but it throws a C++ exception instead.
151   #   the_connection = nil
152   #   content = "A" * 50
153   #   headers = ["aaa", "bbb", ["Content-length: #{content.length}"]*2, "ccc"].flatten
154   #   EventMachine.run {
155   #     EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
156   #       the_connection = conn
157   #     end
158   #     EventMachine.add_timer(4) {raise "test timed out"}
159   #     test_proc = proc {
160   #       t = TCPSocket.new TestHost, TestPort
161   #       headers.each {|h| t.write "#{h}\r\n" }
162   #       t.write "\n"
163   #       t.write content
164   #       t.close
165   #     }
166   #     EventMachine.defer test_proc, proc {
167   #       EventMachine.stop
168   #     }
169   #   }
170   # end
171
172   def test_interpret_headers
173     the_connection = nil
174     content = "A" * 50
175     headers = [
176       "GET / HTTP/1.0",
177       "Accept: aaa",
178       "User-Agent: bbb",
179       "Host:        ccc",
180       "x-tempest-header:ddd"
181     ]
182
183     EventMachine.run {
184       EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
185         the_connection = conn
186       end
187       EventMachine.add_timer(4) {raise "test timed out"}
188
189       client = Module.new do
190         define_method(:headers) { headers }
191         define_method(:content) { content }
192
193         def unbind
194           EM.add_timer(0.1) { EM.stop }
195         end
196
197         def post_init
198           headers.each { |h| send_data "#{h}\r\n" }
199           send_data "\n"
200           send_data content
201           close_connection_after_writing
202         end
203       end
204
205       EventMachine.connect( TestHost, TestPort, client )
206     }
207
208     hsh = the_connection.headers_2_hash( the_connection.my_headers.shift )
209     expect = {
210       :accept => "aaa",
211       :user_agent => "bbb",
212       :host => "ccc",
213       :x_tempest_header => "ddd"
214     }
215     assert_equal(expect, hsh)
216   end
217
218 end
Note: See TracBrowser for help on using the browser.