fresh_simplehttp 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simplehttp}
8
+ s.version = "0.1.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Josh Nichols", "Wilker Lucio"]
12
+ s.date = %q{2010-04-22}
13
+ s.description = %q{simple_http: Simple Http client lib.}
14
+ s.email = %q{josh@technicalpickles.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README",
18
+ "TODO"
19
+ ]
20
+ s.files = [
21
+ "AUTHORS",
22
+ "CHANGELOG",
23
+ "LICENSE",
24
+ "README",
25
+ "Rakefile",
26
+ "TODO",
27
+ "VERSION",
28
+ "lib/simple_http.rb",
29
+ "lib/simplehttp.rb",
30
+ "setup.rb",
31
+ "simplehttp.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/wilkerlucio/simplehttp}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{simple_http: Simple Http client lib.}
38
+ s.test_files = [
39
+ "test/http_test.rb",
40
+ "test/http_test_server.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
53
+
data/test/http_test.rb ADDED
@@ -0,0 +1,214 @@
1
+ require 'simple_http.rb'
2
+ require 'http_test_server.rb'
3
+
4
+ require 'test/unit'
5
+ require 'uri'
6
+ require 'base64'
7
+
8
+ # These tests communicate with a corresponding http server (TestServer), which is
9
+ # implemented in `http_test_server.rb`
10
+
11
+ class SimpleHttpTest < Test::Unit::TestCase
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
19
+
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
25
+
26
+ # Tests of SimpleHttp class method behaviour.
27
+
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
53
+
54
+ # Tests for http GET calls implemented using instantiated SimpleHttp objects.
55
+
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
98
+
99
+ # http POST calls using class methods.
100
+ #
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
123
+
124
+
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
+ http = SimpleHttp.new "http://127.0.0.1:12345/post_test_uri_query?query=query_test"
143
+ ret = http.post "query2"=>"query2_test"
144
+ assert_equal("query=query_test", ret, "instance POST 3.1 test (query on url for post query.)")
145
+
146
+ # POST requst with a custom request_header
147
+ http = SimpleHttp.new "http://127.0.0.1:12345/header_test"
148
+ http.request_headers= {'x-special-http-header'=>'my-value'}
149
+ ret = http.post
150
+ assert_equal("my-value", ret, "POST header test 4")
151
+
152
+ # POST request with a custom data type (not posting a form.)
153
+ http = SimpleHttp.new "http://127.0.0.1:12345/post_data_test"
154
+ ret = http.post TestServer::POST_DATA, "misc/test-data"
155
+ assert_equal(TestServer::POST_DATA, ret, "Post data test")
156
+
157
+
158
+ end
159
+
160
+ def test_basic_auth
161
+
162
+ # string url
163
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/basic_auth"
164
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth get class 1 test failed.");
165
+
166
+ http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
167
+ http.basic_authentication "test", "pwd"
168
+ ret = http.get
169
+ assert_equal(ret, TestServer::SUCCESS_TEXT_1, "basic auth get instance 2 test failed.");
170
+ #string with query as hash
171
+ ret = SimpleHttp.post "http://test:pwd@127.0.0.1:12345/basic_auth", "query" => "query_test"
172
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "basic auth post class 3 test failed.");
173
+
174
+ http = SimpleHttp.new "http://127.0.0.1:12345/basic_auth"
175
+ http.basic_authentication "test", "pwd"
176
+ ret = http.post TestServer::POST_DATA, "misc/test-data"
177
+ assert_equal(TestServer::SUCCESS_TEXT_1, ret, "Post data test")
178
+ end
179
+
180
+ def test_response_handler
181
+ assert_raise(RuntimeError) {
182
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/non_existant"
183
+ }
184
+
185
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/non_existant"
186
+ http.register_response_handler(Net::HTTPNotFound) { |req, res, http|
187
+ res.code
188
+ }
189
+ ret = http.get
190
+ assert_equal("404", ret, "response handler test 2")
191
+
192
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
193
+ http.register_response_handler(Net::HTTPRedirection) { |req, res, http|
194
+ res['location']
195
+ }
196
+ ret = http.get
197
+ assert_equal("http://127.0.0.1:12345/redirect2", ret, "response handler test 3")
198
+
199
+ end
200
+
201
+ def test_redirect
202
+ assert_raise(RuntimeError) {
203
+ http = SimpleHttp.new "http://test:pwd@127.0.0.1:12345/redirect1"
204
+ ret = http.get
205
+ }
206
+
207
+ ret = SimpleHttp.get "http://test:pwd@127.0.0.1:12345/redirect"
208
+ assert_equal(ret, TestServer::SUCCESS_TEXT_0, "redirect test 2");
209
+ end
210
+
211
+
212
+
213
+ end
214
+
@@ -0,0 +1,102 @@
1
+ require 'webrick'
2
+ #require 'constants'
3
+
4
+ include WEBrick
5
+ class TestServer
6
+ SUCCESS_TEXT_0 = "success_0"
7
+ SUCCESS_TEXT_1 = "success_1"
8
+ SUCCESS_TEXT_2 = "success_2"
9
+ SUCCESS_TEXT_3 = "success_3"
10
+ SUCCESS_TEXT_4 = "success_4"
11
+ SUCCESS_TEXT_5 = "success_5"
12
+ SUCCESS_TEXT_6 = "success_6"
13
+ SUCCESS_TEXT_7 = "success_7"
14
+ SUCCESS_TEXT_8 = "success_8"
15
+ SUCCESS_TEXT_9 = "success_9"
16
+
17
+ POST_DATA = [0xbe, 0x00, 0x12, 0x23, 0x45, 0x67, 0x89, 0xAB].join
18
+
19
+ def initialize
20
+ @server = HTTPServer.new(
21
+ :Port => 12345
22
+ )
23
+
24
+ add_tests
25
+ end
26
+
27
+ def start
28
+ @server.start
29
+ end
30
+
31
+ def shutdown
32
+ @server.shutdown
33
+ end
34
+
35
+ def dbg str
36
+ puts "!!!!!!!!! #{str}"
37
+ end
38
+ def add_tests
39
+ # return a text
40
+ @server.mount_proc("/test1"){|req, res|
41
+ res.body=SUCCESS_TEXT_0
42
+ }
43
+ #return value of query param named "query"
44
+ @server.mount_proc("/test2"){|req, res|
45
+ res.body=req.query["query"]
46
+ }
47
+
48
+ # return value of query param named "query2"
49
+ @server.mount_proc("/query_test2"){|req, res|
50
+ res.body=req.query["query2"]
51
+ }
52
+
53
+ # return value of request header named "X-Special-Http-Header"
54
+ @server.mount_proc("/header_test"){|req, res|
55
+ res.body=req.header["x-special-http-header"][0]
56
+ res['x-special-response']=req.header["x-special-http-header"][0]
57
+ }
58
+
59
+ # return value of request header named "X-Special-Http-Header"
60
+ @server.mount_proc("/post_data_test"){|req, res|
61
+ ctype = req.header['content-type'][0]
62
+ if "misc/test-data" == ctype
63
+ res.body=req.body
64
+ else
65
+ res.body="failed, content-type: #{ctype}"
66
+ end
67
+ }
68
+
69
+ # Test that query in uri gets transmitted in post reqs as well.
70
+ @server.mount_proc("/post_test_uri_query") { |req, res|
71
+ res.body = req.query_string
72
+ }
73
+
74
+ @server.mount_proc("/basic_auth") {|req, res|
75
+
76
+ auth = Base64.decode64(req.header['authorization'][0].split[1])
77
+ usr, pwd = auth.split(':')
78
+ if ('test'==usr && 'pwd'==pwd)
79
+ res.body=SUCCESS_TEXT_1
80
+ else
81
+ res.status=403
82
+ end
83
+ }
84
+
85
+ 1.upto(6) {|i|
86
+ @server.mount_proc("/redirect#{i}") { |req, res|
87
+ res.status=301
88
+ res.header['location']="http://127.0.0.1:12345/redirect#{i+1}"
89
+ }
90
+ }
91
+
92
+ @server.mount_proc("/redirect") { |req, res|
93
+ res.status=301
94
+ res.header['location']="http://127.0.0.1:12345/test1"
95
+ }
96
+
97
+ @server.mount_proc("/redirect_special_header") { |req, res|
98
+ res.status=301
99
+ res.header['location']="http://127.0.0.1:12345/header_test"
100
+ }
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fresh_simplehttp
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 7
9
+ version: 0.1.7
10
+ platform: ruby
11
+ authors:
12
+ - Wilker Lucio
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-22 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: "simple_http: Simple Http client lib. Use to do http requests without noise."
22
+ email: wilkerlucio@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README
30
+ - TODO
31
+ files:
32
+ - AUTHORS
33
+ - CHANGELOG
34
+ - LICENSE
35
+ - README
36
+ - Rakefile
37
+ - TODO
38
+ - VERSION
39
+ - fresh_simplehttp.gemspec
40
+ - lib/fresh_simplehttp.rb
41
+ - lib/simple_http.rb
42
+ - lib/simplehttp.rb
43
+ - setup.rb
44
+ - simplehttp.gemspec
45
+ has_rdoc: true
46
+ homepage: http://github.com/wilkerlucio/simplehttp
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: "simple_http: Simple Http client lib."
75
+ test_files:
76
+ - test/http_test.rb
77
+ - test/http_test_server.rb