Class SimpleHttpTest
In: test/http_test.rb
Parent: Test::Unit::TestCase

These tests communicate with a corresponding http server (TestServer), which is implemented in `http_test_server.rb`

Methods

Public Class methods

[Source]

    # File test/http_test.rb, line 12
12:         def initialize *args
13:                 super *args
14:                 # create webbrick server in a separate thread
15:                 Thread.new {
16:                         TestServer.new.start
17:                 }
18:         end

Public Instance methods

[Source]

    # File test/http_test.rb, line 20
20:         def teardown
21:                 #@t.shutdown # webrick blocks in a weird way, can't
22:                 #commnicate with it in a different thread. this, of
23:                 #course, is really ugly.
24:         end

[Source]

     # File test/http_test.rb, line 156
156:         def test_basic_auth
157: 
158:                 # string url
159:                 ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/basic_auth"
160:                 assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth get class 1 test failed.");
161:                 
162:                 http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
163:                 http.basic_authentication "test", "pwd"
164:                 ret = http.get 
165:                 assert_equal(ret, TestServer::SUCCESS_TEXT_1, "basic auth get instance 2 test failed.");
166:                 #string with query as hash    
167:                 ret = SimpleHttp.post "http://test:pwd@127.0.0.1:12345/basic_auth", "query" => "query_test"
168:                 assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth post class 3 test failed.");
169:                 
170:                 http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
171:                 http.basic_authentication "test", "pwd"
172:                 ret = http.post TestServer::POST_DATA, "misc/test-data"
173:                 assert_equal(TestServer::SUCCESS_TEXT_1, ret, "Post data test")
174:         end

Tests for http GET calls implemented using instantiated SimpleHttp objects.

[Source]

    # File test/http_test.rb, line 56
56:         def test_get_class_instance_method
57:                 http = SimpleHttp.new "http://127.0.0.1:12345/test1"
58:                 ret = http.get 
59:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance GET 1 test failed.");
60: 
61:                 http = SimpleHttp.new "http://127.0.0.1:12345/test2?query=query_test"
62:                 ret = http.get
63:                 assert_equal("query_test", ret, "instance GET 2 test (query)");
64: 
65:                 http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
66:                 ret = http.get "query2"=>"query2_test"
67:                 assert_equal("query2_test", ret, "instance GET 2.1 test (add to existing query")
68: 
69:                 http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
70:                 ret = http.get "query2" => "query_test"
71:                 assert_equal("query_test", ret, "basic GET (query) 3 test failed.")
72: 
73:                 http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
74:                 ret = http.get "something"=>"or the other", "query2" => "query_test"
75:                 assert_equal("query_test", ret, "basic GET (query) 4 test failed.")
76: 
77:                 # GET request with a custom request_header
78:                 http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
79:                 http.request_headers= {'x-special-http-header'=>'my-value'}
80:                 ret = http.get
81:                 assert_equal("my-value", ret, "GET header test 4")
82: 
83:                 # GET test with custom response_headers
84:                 http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
85:                 http.request_headers= {'x-special-http-header'=>'resp-header-test'}
86:                 http.get
87:                 assert_equal("resp-header-test", http.response_headers['x-special-response'], "GET response header test 5")
88: 
89:                 # GET test with custom request & response header and redirect
90:                 # test that request headers we set remain intact during redirects.
91: 
92:                 http = SimpleHttp.new "http://127.0.0.1:12345/redirect_special_header"
93:                 http.request_headers["x-special-http-header"]='redirect_test'
94:                 http.get
95:                 assert_equal("redirect_test", http.response_headers['x-special-response'], "GET response header redirect test. 6")
96:                                 
97:         end

Tests of SimpleHttp class method behaviour.

[Source]

    # File test/http_test.rb, line 28
28:         def test_get_class_method
29:                 # string url
30:                 ret = SimpleHttp.get "http://127.0.0.1:12345/test1"
31:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 1 test failed.");
32:                 
33:                 # string url, with query      
34:                 ret = SimpleHttp.get "http://127.0.0.1:12345/test1?query=true"
35:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 2 test failed.");
36:                 
37:                 # uri
38:                 uri = URI.parse "http://127.0.0.1:12345/test1?query=true"
39:                 ret = SimpleHttp.get uri 
40:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic GET 3 test failed.");
41: 
42:                 #string with query as hash    
43:                 ret = SimpleHttp.get "http://127.0.0.1:12345/test2", "query" => "query_test"
44:                 assert_equal("query_test", ret, "basic GET (query) 4 test failed.");
45:                 
46:                 #uri with existing query + query as hash.
47:                 uri = URI.parse "http://127.0.0.1:12345/test2?query2=true"
48:                 ret = SimpleHttp.get uri, "query" => "query_test"
49:                 assert_equal("query_test", ret, "basic GET (query) 4.1 test failed.");
50: 
51: 
52:         end

http POST calls using class methods.

[Source]

     # File test/http_test.rb, line 101
101:         def test_post_class_method
102: 
103:                 # string url
104:                 ret = SimpleHttp.post "http://127.0.0.1:12345/test1"
105:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "basic POST 1 test failed.");
106:                 
107: 
108:                 #string with query as hash    
109:                 ret = SimpleHttp.post "http://127.0.0.1:12345/test2", "query" => "query_test"
110:                 assert_equal("query_test", ret, "basic POST (query) 2 test failed.");
111:                 
112:                 #uri with existing query + query as hash.
113:                 uri = URI.parse "http://127.0.0.1:12345/test2"
114:                 ret = SimpleHttp.post uri, "query" => "query_test"
115:                 assert_equal("query_test", ret, "basic POST (query) 2.1 test failed.");
116:                 
117:                 # post something other than a form.
118:                 ret = SimpleHttp.post "http://127.0.0.1:12345/post_data_test", TestServer::POST_DATA, "misc/test-data"
119:                 assert_equal(TestServer::POST_DATA, ret, "class Post data test")
120: 
121: 
122:         end

[Source]

     # File test/http_test.rb, line 125
125:         def test_post_instance_method
126:                 http = SimpleHttp.new "http://127.0.0.1:12345/test1"
127:                 ret = http.post 
128:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "instance POST 1 test failed.");
129: 
130:                 http = SimpleHttp.new "http://127.0.0.1:12345/test2"
131:                 ret = http.post "query" => "query_test"
132:                 assert_equal("query_test", ret, "instance POST 2 test (query)");
133: 
134:                 http = SimpleHttp.new "http://127.0.0.1:12345/query_test2?query=query_test"
135:                 ret = http.post "query2"=>"query2_test"
136:                 assert_equal("query2_test", ret, "instance POST 2.1 test (add to existing query")
137: 
138:                 http = SimpleHttp.new(URI.parse("http://127.0.0.1:12345/query_test2?bla=true"))
139:                 ret = http.post "query2" => "query_test"
140:                 assert_equal("query_test", ret, "basic POST (query) 3 test failed.")
141: 
142:                 # POST requst with a custom request_header
143:                 http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
144:                 http.request_headers= {'x-special-http-header'=>'my-value'}
145:                 ret = http.post
146:                 assert_equal("my-value", ret, "POST header test 4")
147:                 
148:                 # POST request with a custom data type (not posting a form.)
149:                 http = SimpleHttp.new "http://127.0.0.1:12345/post_data_test"
150:                 ret = http.post TestServer::POST_DATA, "misc/test-data"
151:                 assert_equal(TestServer::POST_DATA, ret, "Post data test")
152: 
153: 
154:         end

[Source]

     # File test/http_test.rb, line 197
197:         def test_redirect 
198:                 assert_raise(RuntimeError) {  
199:                         http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
200:                         ret = http.get
201:                 }
202:                 
203:                         
204:                 ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/redirect"
205:                 assert_equal(ret, TestServer::SUCCESS_TEXT_0, "redirect test 2");
206:         end

[Source]

     # File test/http_test.rb, line 176
176:         def test_response_handler
177:                 assert_raise(RuntimeError) {  
178:                         ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/non_existant"
179:                 }
180:                 
181:                 http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/non_existant"
182:                 http.register_response_handler(Net::HTTPNotFound) { |req, res, http|
183:                         res.code
184:                 }
185:                 ret = http.get
186:                 assert_equal("404", ret, "response handler test 2")
187:                 
188:                 http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
189:                 http.register_response_handler(Net::HTTPRedirection) { |req, res, http|
190:                         res['location']
191:                 }
192:                 ret = http.get
193:                 assert_equal("http://127.0.0.1:12345/redirect2", ret, "response handler test 3") 
194: 
195:         end

[Validate]