Class: TestHttpClient

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb

Defined Under Namespace

Classes: EmptyContent, PostContent

Constant Summary collapse

Localhost =
"127.0.0.1"
Localport =
9801

Instance Method Summary collapse

Instance Method Details

#setupObject



36
37
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 36

def setup
end

#teardownObject



39
40
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 39

def teardown
end

TODO, need a more intelligent cookie tester. In fact, this whole test-harness needs a beefier server implementation.



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 186

def test_cookie
  ok = false
  EM.run {
    c = EM::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80, :cookie=>"aaa=bbb"
    c.callback {|result|
      ok = true;
      EventMachine.stop
    }
    c.errback {EventMachine.stop}
  }
  assert ok
end

#test_http_clientObject




44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 44

def test_http_client
  ok = false
  EventMachine.run {
    c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
    c.callback {
      ok = true
      EventMachine.stop
    }
    c.errback {EventMachine.stop} # necessary, otherwise a failure blocks the test suite forever.
  }
  assert ok
end

#test_http_client_1Object




59
60
61
62
63
64
65
66
67
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 59

def test_http_client_1
  ok = false
  EventMachine.run {
    c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
    c.callback {ok = true; EventMachine.stop}
    c.errback {EventMachine.stop}
  }
  assert ok
end

#test_http_client_2Object




71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 71

def test_http_client_2
  ok = false
  EventMachine.run {
    c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
    c.callback {|result|
      ok = true;
      EventMachine.stop
    }
    c.errback {EventMachine.stop}
  }
  assert ok
end

#test_http_empty_contentObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 102

def test_http_empty_content
    ok = false
    EventMachine.run {
      EventMachine.start_server "127.0.0.1", 9701, EmptyContent
      c = EventMachine::Protocols::HttpClient.send :request, :host => "127.0.0.1", :port => 9701
      c.callback {|result|
        ok = true
        EventMachine.stop
      }
    }
    assert ok
end

#test_postObject

TODO, this is WRONG. The handler is asserting an HTTP 1.1 request, but the client is sending a 1.0 request. Gotta fix the client



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 160

def test_post
    response = nil
    EventMachine.run {
      EventMachine.start_server Localhost, Localport, PostContent
      EventMachine.add_timer(2) {raise "timed out"}
      c = EventMachine::Protocols::HttpClient.request(
        :host=>Localhost,
        :port=>Localport,
        :method=>:post,
        :request=>"/aaa",
        :content=>"XYZ",
        :content_type=>"text/plain"
      )
      c.callback {|r|
        response = r
        EventMachine.stop
      }
    }

    assert_equal( 200, response[:status] )
    assert_equal( "0123456789", response[:content] )
end

#test_version_1_0Object

We can tell the client to send an HTTP/1.0 request (default is 1.1). This is useful for suppressing chunked responses until those are working.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb', line 201

def test_version_1_0
  ok = false
  EM.run {
    c = EM::P::HttpClient.request(
      :host => "www.bayshorenetworks.com",
      :port => 80,
      :version => "1.0"
    )
    c.callback {|result|
      ok = true;
      EventMachine.stop
    }
    c.errback {EventMachine.stop}
  }
  assert ok
end