paul-resourceful 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/Manifest.txt +34 -0
- data/README.markdown +86 -0
- data/Rakefile +14 -0
- data/lib/resourceful.rb +29 -0
- data/lib/resourceful/authentication_manager.rb +107 -0
- data/lib/resourceful/cache_manager.rb +174 -0
- data/lib/resourceful/header.rb +31 -0
- data/lib/resourceful/http_accessor.rb +85 -0
- data/lib/resourceful/net_http_adapter.rb +60 -0
- data/lib/resourceful/options_interpreter.rb +78 -0
- data/lib/resourceful/request.rb +63 -0
- data/lib/resourceful/resource.rb +266 -0
- data/lib/resourceful/response.rb +175 -0
- data/lib/resourceful/stubbed_resource_proxy.rb +47 -0
- data/lib/resourceful/util.rb +6 -0
- data/lib/resourceful/version.rb +1 -0
- data/resourceful.gemspec +30 -0
- data/spec/acceptance_shared_specs.rb +49 -0
- data/spec/acceptance_spec.rb +408 -0
- data/spec/resourceful/authentication_manager_spec.rb +249 -0
- data/spec/resourceful/cache_manager_spec.rb +211 -0
- data/spec/resourceful/header_spec.rb +38 -0
- data/spec/resourceful/http_accessor_spec.rb +125 -0
- data/spec/resourceful/net_http_adapter_spec.rb +96 -0
- data/spec/resourceful/options_interpreter_spec.rb +94 -0
- data/spec/resourceful/request_spec.rb +186 -0
- data/spec/resourceful/resource_spec.rb +600 -0
- data/spec/resourceful/response_spec.rb +238 -0
- data/spec/resourceful/stubbed_resource_proxy_spec.rb +58 -0
- data/spec/simple_http_server_shared_spec.rb +160 -0
- data/spec/simple_http_server_shared_spec_spec.rb +212 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +14 -0
- metadata +98 -0
@@ -0,0 +1,212 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname + 'spec_helper'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'addressable/uri'
|
5
|
+
|
6
|
+
require 'resourceful/net_http_adapter'
|
7
|
+
|
8
|
+
describe 'http server' do
|
9
|
+
it_should_behave_like 'simple http server'
|
10
|
+
|
11
|
+
it 'should have a response code of 200 if the path is /get' do
|
12
|
+
Resourceful::NetHttpAdapter.make_request(:get, 'http://localhost:3000/get')[0].should == 200
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should reply with the posted document in the body if the path is /post' do
|
16
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, 'http://localhost:3000/post', 'Hello from POST!')
|
17
|
+
resp[2].should == 'Hello from POST!'
|
18
|
+
resp[0].should == 201
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should reply with the puted document in the body if the path is /put' do
|
22
|
+
resp = Resourceful::NetHttpAdapter.make_request(:put, 'http://localhost:3000/put', 'Hello from PUT!')
|
23
|
+
resp[2].should == 'Hello from PUT!'
|
24
|
+
resp[0].should == 200
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should reply with "KABOOM!" in the body if the path is /delete' do
|
28
|
+
resp = Resourceful::NetHttpAdapter.make_request(:delete, 'http://localhost:3000/delete')
|
29
|
+
resp[2].should == 'KABOOM!'
|
30
|
+
resp[0].should == 200
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should have a response code of whatever the path is' do
|
34
|
+
Resourceful::NetHttpAdapter.make_request(:get, 'http://localhost:3000/code/304')[0].should == 304
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should redirect to a given url' do
|
38
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, 'http://localhost:3000/redirect/301?http://localhost:3000/get')
|
39
|
+
|
40
|
+
resp[0].should == 301
|
41
|
+
resp[1]['Location'].should == ['http://localhost:3000/get']
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should respond with the request method in the body' do
|
45
|
+
resp = Resourceful::NetHttpAdapter.make_request(:delete, 'http://localhost:3000/method')
|
46
|
+
|
47
|
+
resp[0].should == 200
|
48
|
+
resp[2].should == "DELETE"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should respond with the header set from the query string' do
|
52
|
+
uri = URI.escape('http://localhost:3000/header?{Foo: "bar"}')
|
53
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri)
|
54
|
+
|
55
|
+
resp[1].should have_key('Foo')
|
56
|
+
resp[1]['Foo'].should == ['bar']
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should allow the Date header to be overridden' do
|
60
|
+
uri = URI.escape("http://localhost:3000/header?{Date: \"Thu, 21 Aug 2008 20:00:00 GMT\"}")
|
61
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri)
|
62
|
+
|
63
|
+
resp[1].should have_key('Date')
|
64
|
+
resp[1]['Date'].should == ['Thu, 21 Aug 2008 20:00:00 GMT']
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
it 'should parse escaped uris properly' do
|
69
|
+
uri = URI.escape("http://localhost:3000/header?{Expire: \"#{Time.now.httpdate}\"}")
|
70
|
+
|
71
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri)
|
72
|
+
|
73
|
+
resp[1].should have_key('Expire')
|
74
|
+
resp[1]['Expire'].first.should_not =~ /%/
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should echo the request header in the response body' do
|
78
|
+
uri = URI.escape("http://localhost:3000/echo_header")
|
79
|
+
|
80
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri)
|
81
|
+
|
82
|
+
resp[2].should =~ /HTTP_HOST/
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '/modified' do
|
86
|
+
it 'should be 200 if no I-M-S header' do
|
87
|
+
uri = URI.escape("http://localhost:3000/modified?#{(Time.now + 3600).httpdate}")
|
88
|
+
|
89
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri)
|
90
|
+
|
91
|
+
resp[0].should == 200
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should be 304 if I-M-S header is set' do
|
95
|
+
now = Time.utc(2008,5,29,12,00)
|
96
|
+
uri = URI.escape("http://localhost:3000/modified?#{(now + 3600).httpdate}")
|
97
|
+
|
98
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, uri, nil, {'If-Modified-Since' => now.httpdate})
|
99
|
+
|
100
|
+
resp[0].should == 304
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '/auth' do
|
106
|
+
before do
|
107
|
+
@uri = "http://localhost:3000/auth?basic"
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should return a 401 if no auth info is provided' do
|
111
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
112
|
+
resp[0].should == 401
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'basic' do
|
116
|
+
before do
|
117
|
+
@uri = "http://localhost:3000/auth?basic"
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should return a 401 if no auth info is provided' do
|
121
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
122
|
+
resp[0].should == 401
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should provide a WWW-Authenticate header when 401' do
|
126
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
127
|
+
header = resp[1]
|
128
|
+
header.should have_key('WWW-Authenticate')
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should set the scheme to "Basic"' do
|
132
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
133
|
+
auth = resp[1]['WWW-Authenticate'].first
|
134
|
+
auth.should =~ /^Basic/
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'should set the realm to "Test Auth"' do
|
138
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
139
|
+
auth = resp[1]['WWW-Authenticate'].first
|
140
|
+
auth.should =~ /realm="Test Auth"/
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'should authorize on u/p:admin/secret' do
|
144
|
+
creds = HTTPAuth::Basic.pack_authorization('admin', 'secret')
|
145
|
+
header = {'Authorization' => creds}
|
146
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri, nil, header)
|
147
|
+
resp[0].should == 200
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should authorize if u/p is incorrect' do
|
151
|
+
creds = HTTPAuth::Basic.pack_authorization('admin', 'not secret')
|
152
|
+
header = {'Authorization' => creds}
|
153
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri, nil, header)
|
154
|
+
resp[0].should == 401
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
describe 'digest' do
|
160
|
+
before do
|
161
|
+
@uri = "http://localhost:3000/auth?digest"
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'should return a 401 if no auth info is provided' do
|
165
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
166
|
+
resp[0].should == 401
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should provide a WWW-Authenticate header when 401' do
|
170
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
171
|
+
header = resp[1]
|
172
|
+
header.should have_key('WWW-Authenticate')
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'should set the scheme to "Digest"' do
|
176
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
177
|
+
auth = resp[1]['WWW-Authenticate'].first
|
178
|
+
auth.should =~ /^Digest/
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should set the realm to "Test Auth"' do
|
182
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
183
|
+
auth = resp[1]['WWW-Authenticate'].first
|
184
|
+
auth.should =~ /realm="Test Auth"/
|
185
|
+
end
|
186
|
+
|
187
|
+
def challenge
|
188
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri)
|
189
|
+
HTTPAuth::Digest::Challenge.from_header(resp[1]['WWW-Authenticate'].first)
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'should authorize on u/p:admin/secret' do
|
193
|
+
creds = HTTPAuth::Digest::Credentials.from_challenge(challenge, :username => 'admin', :password => 'secret', :uri => @uri)
|
194
|
+
header = {'Authorization' => creds.to_header}
|
195
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri, nil, header)
|
196
|
+
resp[0].should == 200
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'should not authorize if u/p is incorrect' do
|
200
|
+
pending
|
201
|
+
creds = HTTPAuth::Digest::Credentials.from_challenge(challenge, :username => 'admin', :password => 'not secret', :uri => @uri)
|
202
|
+
header = {'Authorization' => creds.to_header}
|
203
|
+
resp = Resourceful::NetHttpAdapter.make_request(:get, @uri, nil, header)
|
204
|
+
resp[0].should == 401
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
require 'spec'
|
4
|
+
require 'pp'
|
5
|
+
require 'facets'
|
6
|
+
|
7
|
+
$LOAD_PATH << Pathname(__FILE__).dirname + "../lib"
|
8
|
+
require 'resourceful/util'
|
9
|
+
require 'resourceful'
|
10
|
+
require 'resourceful/http_accessor'
|
11
|
+
|
12
|
+
require Pathname(__FILE__).dirname + 'simple_http_server_shared_spec'
|
13
|
+
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paul-resourceful
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Sadauskas
|
8
|
+
- Peter Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-11-22 00:00:00 -08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: hoe
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.2
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- psadauskas@gmail.com
|
28
|
+
- pezra@barelyenough.org
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- Manifest.txt
|
35
|
+
files:
|
36
|
+
- MIT-LICENSE
|
37
|
+
- Manifest.txt
|
38
|
+
- README.markdown
|
39
|
+
- Rakefile
|
40
|
+
- lib/resourceful.rb
|
41
|
+
- lib/resourceful/authentication_manager.rb
|
42
|
+
- lib/resourceful/cache_manager.rb
|
43
|
+
- lib/resourceful/header.rb
|
44
|
+
- lib/resourceful/http_accessor.rb
|
45
|
+
- lib/resourceful/net_http_adapter.rb
|
46
|
+
- lib/resourceful/options_interpreter.rb
|
47
|
+
- lib/resourceful/request.rb
|
48
|
+
- lib/resourceful/resource.rb
|
49
|
+
- lib/resourceful/response.rb
|
50
|
+
- lib/resourceful/stubbed_resource_proxy.rb
|
51
|
+
- lib/resourceful/util.rb
|
52
|
+
- lib/resourceful/version.rb
|
53
|
+
- resourceful.gemspec
|
54
|
+
- spec/acceptance_shared_specs.rb
|
55
|
+
- spec/acceptance_spec.rb
|
56
|
+
- spec/resourceful/authentication_manager_spec.rb
|
57
|
+
- spec/resourceful/cache_manager_spec.rb
|
58
|
+
- spec/resourceful/header_spec.rb
|
59
|
+
- spec/resourceful/http_accessor_spec.rb
|
60
|
+
- spec/resourceful/net_http_adapter_spec.rb
|
61
|
+
- spec/resourceful/options_interpreter_spec.rb
|
62
|
+
- spec/resourceful/request_spec.rb
|
63
|
+
- spec/resourceful/resource_spec.rb
|
64
|
+
- spec/resourceful/response_spec.rb
|
65
|
+
- spec/resourceful/stubbed_resource_proxy_spec.rb
|
66
|
+
- spec/simple_http_server_shared_spec.rb
|
67
|
+
- spec/simple_http_server_shared_spec_spec.rb
|
68
|
+
- spec/spec.opts
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage:
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --main
|
75
|
+
- README.txt
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
version:
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project: resourceful
|
93
|
+
rubygems_version: 1.2.0
|
94
|
+
signing_key:
|
95
|
+
specification_version: 2
|
96
|
+
summary: An HTTP library for Ruby that takes advantage of everything HTTP has to offer.
|
97
|
+
test_files: []
|
98
|
+
|