sham_rack 1.3.6 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +6 -14
- data/Rakefile +1 -1
- data/lib/sham_rack.rb +7 -0
- data/lib/sham_rack/allowances.rb +24 -0
- data/lib/sham_rack/registration.rb +9 -5
- data/lib/sham_rack/version.rb +1 -1
- data/spec/sham_rack/stub_web_service_spec.rb +24 -24
- data/spec/sham_rack_spec.rb +105 -77
- data/spec/spec_helper.rb +3 -4
- metadata +17 -18
- data/CHANGES.markdown +0 -42
- data/README.markdown +0 -123
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NDI2MTVkYzU3OWI2YTgwYzFkMWEzYzg0NWJlOTM3YzU4NjhmYzk0Mzc1NTUy
|
10
|
-
ZGNhMTY2OGQ1ODA2ODExY2U3OTE3M2VjZjI3MjE0ZjllZDQ5YTE4OGQyNDY3
|
11
|
-
YTEwZTM1OWRjZGZiZjMyM2U2NmVkYjFmN2I2NTA4NzNlNTUxOTc=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZmEyODY4YTVlNGRkNTNmMjE4ODNhZjlhNDUwODU0MjI3ZDFmZGYyYTcwNGJk
|
14
|
-
M2I2NTk0OGU1MDA2NTJiYWUyODczZGZjYTQ2ZjY2YjRmOGYwZDMzZmM4OTU1
|
15
|
-
MDljNjA2OTQ5NWFmNzQxMzAzZWYzZWEzZGZhZDMwZDA1NGI2ZGY=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 887447b8de1ec4acf1dec895fc3f51634cedc2d7
|
4
|
+
data.tar.gz: ad8bbd8c761fc122986386bba9907fef7273762e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4a4bfe421be365279abdb1a5436b04fa08de0c93ca0a524d57767a5fd1fd7f8236b03815f15578c94eb14812de73b93830f4c8d0aad05da2fa08dab2a5112ab8
|
7
|
+
data.tar.gz: 56abd422fc06f9ce5eff13bef96b230883ec49c97ec33bff4bb5aa2847f545e6ac6864d04dce3b8884e911e2f7372aa29cd2d3373258b76df69995c6345d6652
|
data/Rakefile
CHANGED
data/lib/sham_rack.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
module ShamRack
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def network_connections_allowed?
|
6
|
+
@allow_network_connections
|
7
|
+
end
|
8
|
+
|
9
|
+
def allow_network_connections
|
10
|
+
@allow_network_connections = true
|
11
|
+
end
|
12
|
+
|
13
|
+
def prevent_network_connections
|
14
|
+
@allow_network_connections = false
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
class NetworkConnectionPrevented < StandardError
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
ShamRack.allow_network_connections
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require "sham_rack/allowances"
|
2
|
+
|
1
3
|
module ShamRack
|
2
4
|
|
3
5
|
module Registration
|
@@ -18,11 +20,13 @@ module ShamRack
|
|
18
20
|
end
|
19
21
|
|
20
22
|
def application_for(address, port = nil)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
port ||= Net::HTTP.default_port
|
24
|
+
mount_point_for(address, port).app.tap do |app|
|
25
|
+
return app unless app.nil?
|
26
|
+
unless ShamRack.network_connections_allowed?
|
27
|
+
raise NetworkConnectionPrevented, "connection to #{address}:#{port} not allowed"
|
28
|
+
end
|
29
|
+
end
|
26
30
|
end
|
27
31
|
|
28
32
|
private
|
data/lib/sham_rack/version.rb
CHANGED
@@ -3,7 +3,7 @@ require "spec_helper"
|
|
3
3
|
require "sham_rack/stub_web_service"
|
4
4
|
require "rack/test"
|
5
5
|
|
6
|
-
describe ShamRack::StubWebService do
|
6
|
+
RSpec.describe ShamRack::StubWebService do
|
7
7
|
|
8
8
|
include Rack::Test::Methods
|
9
9
|
|
@@ -14,14 +14,14 @@ describe ShamRack::StubWebService do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "#last_request" do
|
17
|
-
|
17
|
+
|
18
18
|
it "returns the last request" do
|
19
19
|
get '/foo/bar'
|
20
|
-
@app.last_request.path_info.
|
20
|
+
expect(@app.last_request.path_info).to eq("/foo/bar")
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
describe "with no handlers registered" do
|
26
26
|
|
27
27
|
describe "any request" do
|
@@ -31,7 +31,7 @@ describe ShamRack::StubWebService do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
it "returns a 404" do
|
34
|
-
last_response.status.
|
34
|
+
expect(last_response.status).to eq(404)
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
@@ -39,9 +39,9 @@ describe ShamRack::StubWebService do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
describe "with two handlers registered" do
|
42
|
-
|
42
|
+
|
43
43
|
before(:each) do
|
44
|
-
|
44
|
+
|
45
45
|
@app.handle do |request|
|
46
46
|
[200, {}, ["response from first handler"]] if request.get?
|
47
47
|
end
|
@@ -49,9 +49,9 @@ describe ShamRack::StubWebService do
|
|
49
49
|
@app.handle do |request|
|
50
50
|
[200, {}, ["response from second handler"]] if request.path_info == "/stuff"
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
describe "a request matching the first handler" do
|
56
56
|
|
57
57
|
before do
|
@@ -59,11 +59,11 @@ describe ShamRack::StubWebService do
|
|
59
59
|
end
|
60
60
|
|
61
61
|
it "receives a response from the first handler" do
|
62
|
-
last_response.body.
|
62
|
+
expect(last_response.body).to eq("response from first handler")
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
describe "a request matching the second handler" do
|
68
68
|
|
69
69
|
before do
|
@@ -71,11 +71,11 @@ describe ShamRack::StubWebService do
|
|
71
71
|
end
|
72
72
|
|
73
73
|
it "receives a response from the second handler" do
|
74
|
-
last_response.body.
|
74
|
+
expect(last_response.body).to eq("response from second handler")
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
describe "a request matching both handlers" do
|
80
80
|
|
81
81
|
before do
|
@@ -83,11 +83,11 @@ describe ShamRack::StubWebService do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
it "receives a response from the second handler" do
|
86
|
-
last_response.body.
|
86
|
+
expect(last_response.body).to eq("response from second handler")
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
end
|
92
92
|
|
93
93
|
describe ".register_resource" do
|
@@ -96,17 +96,17 @@ describe ShamRack::StubWebService do
|
|
96
96
|
@app.register_resource("/stuff?foo=bar", "STUFF", "text/plain", 202)
|
97
97
|
get "/stuff?foo=bar"
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
it "sets body" do
|
101
|
-
last_response.body.
|
101
|
+
expect(last_response.body).to eq("STUFF")
|
102
102
|
end
|
103
103
|
|
104
104
|
it "sets content-type" do
|
105
|
-
last_response.content_type.
|
105
|
+
expect(last_response.content_type).to eq("text/plain")
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
it "sets status code" do
|
109
|
-
last_response.status.
|
109
|
+
expect(last_response.status).to eq(202)
|
110
110
|
end
|
111
111
|
|
112
112
|
end
|
data/spec/sham_rack_spec.rb
CHANGED
@@ -7,61 +7,59 @@ require "restclient"
|
|
7
7
|
require "mechanize"
|
8
8
|
require "rack"
|
9
9
|
|
10
|
-
describe ShamRack do
|
10
|
+
RSpec.describe ShamRack do
|
11
11
|
|
12
12
|
class NetHttpProhibited < StandardError; end
|
13
13
|
|
14
14
|
before do
|
15
|
-
|
16
|
-
|
17
|
-
raise NetHttpProhibited, "real network calls are not allowed"
|
18
|
-
end
|
15
|
+
allow_any_instance_of(Net::HTTP).to receive(:start) do
|
16
|
+
raise NetHttpProhibited, "real network calls are not allowed"
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
20
|
after(:each) do
|
23
|
-
ShamRack.
|
21
|
+
ShamRack.reset
|
24
22
|
end
|
25
23
|
|
26
24
|
describe "mounted Rack application" do
|
27
25
|
|
28
26
|
before(:each) do
|
29
|
-
ShamRack.
|
27
|
+
ShamRack.at("www.greetings.com").mount(GreetingApp.new)
|
30
28
|
end
|
31
29
|
|
32
30
|
it "can be accessed using Net::HTTP" do
|
33
31
|
response = Net::HTTP.start("www.greetings.com") do |http|
|
34
32
|
http.request(Net::HTTP::Get.new("/"))
|
35
33
|
end
|
36
|
-
response.body.
|
34
|
+
expect(response.body).to eq("Hello, world")
|
37
35
|
end
|
38
36
|
|
39
37
|
it "can be accessed using Net::HTTP#get_response" do
|
40
38
|
response = Net::HTTP.get_response(URI.parse("http://www.greetings.com/"))
|
41
|
-
response.body.
|
39
|
+
expect(response.body).to eq("Hello, world")
|
42
40
|
end
|
43
41
|
|
44
42
|
it "can be accessed using open-uri" do
|
45
43
|
response = open("http://www.greetings.com")
|
46
|
-
response.status.
|
47
|
-
response.read.
|
44
|
+
expect(response.status).to eq(["200", "OK"])
|
45
|
+
expect(response.read).to eq("Hello, world")
|
48
46
|
end
|
49
47
|
|
50
48
|
it "can be accessed using RestClient" do
|
51
49
|
response = RestClient.get("http://www.greetings.com")
|
52
|
-
response.code.
|
53
|
-
response.to_s.
|
50
|
+
expect(response.code).to eq(200)
|
51
|
+
expect(response.to_s).to eq("Hello, world")
|
54
52
|
end
|
55
53
|
|
56
54
|
it "can be accessed using Mechanize" do
|
57
55
|
response = Mechanize.new.get("http://www.greetings.com")
|
58
|
-
response.body.
|
56
|
+
expect(response.body).to eq("Hello, world")
|
59
57
|
end
|
60
58
|
|
61
59
|
it "can be accessed using Patron" do
|
62
60
|
patron = Patron::Session.new
|
63
61
|
response = patron.get("http://www.greetings.com/foo/bar")
|
64
|
-
response.body.
|
62
|
+
expect(response.body).to eq("Hello, world")
|
65
63
|
end
|
66
64
|
|
67
65
|
end
|
@@ -76,7 +74,7 @@ describe ShamRack do
|
|
76
74
|
["200 OK", { "Content-type" => "text/plain" }, ["Easy, huh?"]]
|
77
75
|
end
|
78
76
|
|
79
|
-
open("http://simple.xyz").read.
|
77
|
+
expect(open("http://simple.xyz").read).to eq("Easy, huh?")
|
80
78
|
|
81
79
|
end
|
82
80
|
|
@@ -85,9 +83,9 @@ describe ShamRack do
|
|
85
83
|
context "with a URL" do
|
86
84
|
|
87
85
|
it "raises an ArgumentError" do
|
88
|
-
|
86
|
+
expect do
|
89
87
|
ShamRack.at("http://www.greetings.com")
|
90
|
-
end.
|
88
|
+
end.to raise_error(ArgumentError, "invalid address")
|
91
89
|
end
|
92
90
|
|
93
91
|
end
|
@@ -98,7 +96,7 @@ describe ShamRack do
|
|
98
96
|
|
99
97
|
ShamRack.at("hello.xyz").mount(GreetingApp.new)
|
100
98
|
|
101
|
-
open("http://hello.xyz").read.
|
99
|
+
expect(open("http://hello.xyz").read).to eq("Hello, world")
|
102
100
|
|
103
101
|
end
|
104
102
|
|
@@ -111,9 +109,9 @@ describe ShamRack do
|
|
111
109
|
ShamRack.at("gone.xyz").mount(GreetingApp.new)
|
112
110
|
ShamRack.at("gone.xyz").unmount
|
113
111
|
|
114
|
-
|
112
|
+
expect do
|
115
113
|
open("http://gone.xyz").read
|
116
|
-
end.
|
114
|
+
end.to raise_error(NetHttpProhibited)
|
117
115
|
|
118
116
|
end
|
119
117
|
|
@@ -129,11 +127,11 @@ describe ShamRack do
|
|
129
127
|
end
|
130
128
|
|
131
129
|
it "mounts an app created using Rack::Builder" do
|
132
|
-
open("http://rackup.xyz").read.
|
130
|
+
expect(open("http://rackup.xyz").read).to eq("HELLO, WORLD")
|
133
131
|
end
|
134
132
|
|
135
133
|
it "returns the app" do
|
136
|
-
@return_value.
|
134
|
+
expect(@return_value).to respond_to(:call)
|
137
135
|
end
|
138
136
|
|
139
137
|
end
|
@@ -149,11 +147,11 @@ describe ShamRack do
|
|
149
147
|
end
|
150
148
|
|
151
149
|
it "mounts associated block as a Sinatra app" do
|
152
|
-
open("http://sinatra.xyz/hello/stranger").read.
|
150
|
+
expect(open("http://sinatra.xyz/hello/stranger").read).to eq("Hello, stranger")
|
153
151
|
end
|
154
152
|
|
155
153
|
it "returns the app" do
|
156
|
-
@return_value.
|
154
|
+
expect(@return_value).to respond_to(:call)
|
157
155
|
end
|
158
156
|
|
159
157
|
end
|
@@ -165,29 +163,17 @@ describe ShamRack do
|
|
165
163
|
end
|
166
164
|
|
167
165
|
it "mounts a StubWebService" do
|
168
|
-
ShamRack.application_for("stubbed.xyz").
|
166
|
+
expect(ShamRack.application_for("stubbed.xyz")).to be_kind_of(ShamRack::StubWebService)
|
169
167
|
end
|
170
168
|
|
171
169
|
it "returns the StubWebService" do
|
172
|
-
@return_value.
|
170
|
+
expect(@return_value).to eq(ShamRack.application_for("stubbed.xyz"))
|
173
171
|
end
|
174
172
|
|
175
173
|
end
|
176
174
|
|
177
175
|
end
|
178
176
|
|
179
|
-
describe ".mount" do
|
180
|
-
|
181
|
-
it "is deprecated, but still works" do
|
182
|
-
|
183
|
-
ShamRack.mount(GreetingApp.new, "hello.xyz")
|
184
|
-
|
185
|
-
open("http://hello.xyz").read.should == "Hello, world"
|
186
|
-
|
187
|
-
end
|
188
|
-
|
189
|
-
end
|
190
|
-
|
191
177
|
describe "response" do
|
192
178
|
|
193
179
|
before(:each) do
|
@@ -203,23 +189,23 @@ describe ShamRack do
|
|
203
189
|
let(:response) { Net::HTTP.get_response(URI.parse("http://www.greetings.com/")) }
|
204
190
|
|
205
191
|
it "has status returned by app" do
|
206
|
-
response.code.
|
192
|
+
expect(response.code).to eq("456")
|
207
193
|
end
|
208
194
|
|
209
195
|
it "has status message returned by app" do
|
210
|
-
response.message.
|
196
|
+
expect(response.message).to eq("Foo Bar")
|
211
197
|
end
|
212
198
|
|
213
199
|
it "has body returned by app" do
|
214
|
-
response.body.
|
200
|
+
expect(response.body).to eq("BODY")
|
215
201
|
end
|
216
202
|
|
217
203
|
it "has Content-Type returned by app" do
|
218
|
-
response.content_type.
|
204
|
+
expect(response.content_type).to eq("text/plain")
|
219
205
|
end
|
220
206
|
|
221
207
|
it "has other headers returned by app" do
|
222
|
-
response["x-foo"].
|
208
|
+
expect(response["x-foo"]).to eq("bar")
|
223
209
|
end
|
224
210
|
|
225
211
|
context "when the app returns a numeric status" do
|
@@ -236,11 +222,53 @@ describe ShamRack do
|
|
236
222
|
end
|
237
223
|
|
238
224
|
it "has status returned by app" do
|
239
|
-
response.code.
|
225
|
+
expect(response.code).to eq("201")
|
240
226
|
end
|
241
227
|
|
242
228
|
it "derives a status message" do
|
243
|
-
response.message.
|
229
|
+
expect(response.message).to eq("Created")
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
describe ".allow_network_connections" do
|
237
|
+
|
238
|
+
context "when false" do
|
239
|
+
|
240
|
+
before do
|
241
|
+
ShamRack.prevent_network_connections
|
242
|
+
end
|
243
|
+
|
244
|
+
after do
|
245
|
+
ShamRack.allow_network_connections
|
246
|
+
end
|
247
|
+
|
248
|
+
it "prevents Net::HTTP requests" do
|
249
|
+
expect {
|
250
|
+
Net::HTTP.get_response(URI.parse("http://www.example.com/"))
|
251
|
+
}.to raise_error(ShamRack::NetworkConnectionPrevented)
|
252
|
+
end
|
253
|
+
|
254
|
+
it "prevents Patron requests" do
|
255
|
+
expect {
|
256
|
+
Patron::Session.new.get("http://www.example.com/")
|
257
|
+
}.to raise_error(ShamRack::NetworkConnectionPrevented)
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
context "when true" do
|
263
|
+
|
264
|
+
before do
|
265
|
+
ShamRack.allow_network_connections
|
266
|
+
end
|
267
|
+
|
268
|
+
it "allows Net::HTTP requests" do
|
269
|
+
expect {
|
270
|
+
Net::HTTP.get_response(URI.parse("http://www.example.com/"))
|
271
|
+
}.to raise_error(NetHttpProhibited)
|
244
272
|
end
|
245
273
|
|
246
274
|
end
|
@@ -265,19 +293,19 @@ describe ShamRack do
|
|
265
293
|
|
266
294
|
open("http://env.xyz/blah?q=abc")
|
267
295
|
|
268
|
-
env["REQUEST_METHOD"].
|
269
|
-
env["SCRIPT_NAME"].
|
270
|
-
env["PATH_INFO"].
|
271
|
-
env["QUERY_STRING"].
|
272
|
-
env["SERVER_NAME"].
|
273
|
-
env["SERVER_PORT"].
|
296
|
+
expect(env["REQUEST_METHOD"]).to eq("GET")
|
297
|
+
expect(env["SCRIPT_NAME"]).to eq("")
|
298
|
+
expect(env["PATH_INFO"]).to eq("/blah")
|
299
|
+
expect(env["QUERY_STRING"]).to eq("q=abc")
|
300
|
+
expect(env["SERVER_NAME"]).to eq("env.xyz")
|
301
|
+
expect(env["SERVER_PORT"]).to eq("80")
|
274
302
|
|
275
|
-
env["rack.version"].
|
276
|
-
env["rack.url_scheme"].
|
303
|
+
expect(env["rack.version"]).to be_kind_of(Array)
|
304
|
+
expect(env["rack.url_scheme"]).to eq("http")
|
277
305
|
|
278
|
-
env["rack.multithread"].
|
279
|
-
env["rack.multiprocess"].
|
280
|
-
env["rack.run_once"].
|
306
|
+
expect(env["rack.multithread"]).to eq(true)
|
307
|
+
expect(env["rack.multiprocess"]).to eq(true)
|
308
|
+
expect(env["rack.run_once"]).to eq(false)
|
281
309
|
|
282
310
|
end
|
283
311
|
|
@@ -289,7 +317,7 @@ describe ShamRack do
|
|
289
317
|
http.request(request)
|
290
318
|
end
|
291
319
|
|
292
|
-
env["HTTP_FOO_BAR"].
|
320
|
+
expect(env["HTTP_FOO_BAR"]).to eq("baz")
|
293
321
|
|
294
322
|
end
|
295
323
|
|
@@ -297,9 +325,9 @@ describe ShamRack do
|
|
297
325
|
|
298
326
|
RestClient.post("http://env.xyz/resource", "q" => "rack")
|
299
327
|
|
300
|
-
env["REQUEST_METHOD"].
|
301
|
-
env["CONTENT_TYPE"].
|
302
|
-
env["rack.input"].read.
|
328
|
+
expect(env["REQUEST_METHOD"]).to eq("POST")
|
329
|
+
expect(env["CONTENT_TYPE"]).to eq("application/x-www-form-urlencoded")
|
330
|
+
expect(env["rack.input"].read).to eq("q=rack")
|
303
331
|
|
304
332
|
end
|
305
333
|
|
@@ -309,8 +337,8 @@ describe ShamRack do
|
|
309
337
|
http.post("/resource", "q=rack")
|
310
338
|
end
|
311
339
|
|
312
|
-
env["REQUEST_METHOD"].
|
313
|
-
env["rack.input"].read.
|
340
|
+
expect(env["REQUEST_METHOD"]).to eq("POST")
|
341
|
+
expect(env["rack.input"].read).to eq("q=rack")
|
314
342
|
|
315
343
|
end
|
316
344
|
|
@@ -319,11 +347,11 @@ describe ShamRack do
|
|
319
347
|
patron = Patron::Session.new
|
320
348
|
response = patron.post("http://env.xyz/resource", "<xml/>", "Content-Type" => "application/xml")
|
321
349
|
|
322
|
-
response.status.
|
350
|
+
expect(response.status).to eq(200)
|
323
351
|
|
324
|
-
env["REQUEST_METHOD"].
|
325
|
-
env["rack.input"].read.
|
326
|
-
env["CONTENT_TYPE"].
|
352
|
+
expect(env["REQUEST_METHOD"]).to eq("POST")
|
353
|
+
expect(env["rack.input"].read).to eq("<xml/>")
|
354
|
+
expect(env["CONTENT_TYPE"]).to eq("application/xml")
|
327
355
|
|
328
356
|
end
|
329
357
|
|
@@ -331,9 +359,9 @@ describe ShamRack do
|
|
331
359
|
|
332
360
|
RestClient.put("http://env.xyz/thing1", "stuff", :content_type => "text/plain")
|
333
361
|
|
334
|
-
env["REQUEST_METHOD"].
|
335
|
-
env["CONTENT_TYPE"].
|
336
|
-
env["rack.input"].read.
|
362
|
+
expect(env["REQUEST_METHOD"]).to eq("PUT")
|
363
|
+
expect(env["CONTENT_TYPE"]).to eq("text/plain")
|
364
|
+
expect(env["rack.input"].read).to eq("stuff")
|
337
365
|
|
338
366
|
end
|
339
367
|
|
@@ -342,9 +370,9 @@ describe ShamRack do
|
|
342
370
|
patron = Patron::Session.new
|
343
371
|
response = patron.put("http://env.xyz/resource", "stuff", "Content-Type" => "text/plain")
|
344
372
|
|
345
|
-
env["REQUEST_METHOD"].
|
346
|
-
env["CONTENT_TYPE"].
|
347
|
-
env["rack.input"].read.
|
373
|
+
expect(env["REQUEST_METHOD"]).to eq("PUT")
|
374
|
+
expect(env["CONTENT_TYPE"]).to eq("text/plain")
|
375
|
+
expect(env["rack.input"].read).to eq("stuff")
|
348
376
|
|
349
377
|
end
|
350
378
|
|
@@ -352,8 +380,8 @@ describe ShamRack do
|
|
352
380
|
|
353
381
|
RestClient.delete("http://env.xyz/thing/1")
|
354
382
|
|
355
|
-
env["REQUEST_METHOD"].
|
356
|
-
env["PATH_INFO"].
|
383
|
+
expect(env["REQUEST_METHOD"]).to eq("DELETE")
|
384
|
+
expect(env["PATH_INFO"]).to eq("/thing/1")
|
357
385
|
|
358
386
|
end
|
359
387
|
|
@@ -362,8 +390,8 @@ describe ShamRack do
|
|
362
390
|
patron = Patron::Session.new
|
363
391
|
response = patron.delete("http://env.xyz/resource")
|
364
392
|
|
365
|
-
env["REQUEST_METHOD"].
|
366
|
-
env["PATH_INFO"].
|
393
|
+
expect(env["REQUEST_METHOD"]).to eq("DELETE")
|
394
|
+
expect(env["PATH_INFO"]).to eq("/resource")
|
367
395
|
|
368
396
|
end
|
369
397
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sham_rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
prerelease: false
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ! '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
14
|
name: rack
|
21
15
|
requirement: !ruby/object:Gem::Requirement
|
22
16
|
requirements:
|
23
|
-
- -
|
17
|
+
- - ">="
|
24
18
|
- !ruby/object:Gem::Version
|
25
19
|
version: '0'
|
26
20
|
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
27
|
description: ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP
|
28
28
|
testing.
|
29
29
|
email: mdub@dogbiscuit.org
|
@@ -31,21 +31,20 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- Rakefile
|
35
|
+
- benchmark/benchmark.rb
|
36
|
+
- benchmark/hello_app.rb
|
37
|
+
- lib/sham_rack.rb
|
38
|
+
- lib/sham_rack/allowances.rb
|
34
39
|
- lib/sham_rack/net_http.rb
|
35
40
|
- lib/sham_rack/patron.rb
|
36
41
|
- lib/sham_rack/registration.rb
|
37
42
|
- lib/sham_rack/stub_web_service.rb
|
38
43
|
- lib/sham_rack/version.rb
|
39
|
-
- lib/sham_rack.rb
|
40
|
-
- README.markdown
|
41
|
-
- CHANGES.markdown
|
42
44
|
- spec/sham_rack/stub_web_service_spec.rb
|
43
45
|
- spec/sham_rack_spec.rb
|
44
46
|
- spec/spec_helper.rb
|
45
47
|
- spec/test_apps.rb
|
46
|
-
- Rakefile
|
47
|
-
- benchmark/benchmark.rb
|
48
|
-
- benchmark/hello_app.rb
|
49
48
|
homepage: http://github.com/mdub/sham_rack
|
50
49
|
licenses: []
|
51
50
|
metadata: {}
|
@@ -55,17 +54,17 @@ require_paths:
|
|
55
54
|
- lib
|
56
55
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
56
|
requirements:
|
58
|
-
- -
|
57
|
+
- - ">="
|
59
58
|
- !ruby/object:Gem::Version
|
60
59
|
version: '0'
|
61
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
61
|
requirements:
|
63
|
-
- -
|
62
|
+
- - ">="
|
64
63
|
- !ruby/object:Gem::Version
|
65
64
|
version: '0'
|
66
65
|
requirements: []
|
67
66
|
rubyforge_project: shamrack
|
68
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.6.8
|
69
68
|
signing_key:
|
70
69
|
specification_version: 4
|
71
70
|
summary: Net::HTTP-to-Rack plumbing
|
data/CHANGES.markdown
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
## 1-May-2012 [mdub@dogbiscuit.org]
|
2
|
-
|
3
|
-
* Validate arguments to `ShamRack#mount`.
|
4
|
-
* Update for compatibility with patron-0.4.x.
|
5
|
-
|
6
|
-
## 18-Oct-2010 [mdub@dogbiscuit.org]
|
7
|
-
|
8
|
-
* Add support for Patron.
|
9
|
-
|
10
|
-
## 02-Sep-2010 [mdub@dogbiscuit.org]
|
11
|
-
|
12
|
-
* Fixes to support Ruby-1.9.x.
|
13
|
-
|
14
|
-
## 08-Jul-2010 [jyurek@thoughtbot.com]
|
15
|
-
|
16
|
-
* Add support for Mechanize.
|
17
|
-
|
18
|
-
## 11-Mar-2010 [mdub@dogbiscuit.org]
|
19
|
-
|
20
|
-
* Added generic `StubWebService`.
|
21
|
-
|
22
|
-
## 15-Jan-2010 [jeremy.burks@gmail.com]
|
23
|
-
|
24
|
-
* Fix an incompatibility with rest-client 1.2.0.
|
25
|
-
|
26
|
-
## 27-Nov-2009 [mdub@dogbiscuit.org]
|
27
|
-
|
28
|
-
* Change of approach: extend rather than reimplement `Net:HTTP`. This should improve coverage of all the weird and wonderful ways of using the `Net:HTTP` API.
|
29
|
-
|
30
|
-
## 5-Jun-2009 [mdub@dogbiscuit.org]
|
31
|
-
|
32
|
-
* Add support for `Net::HTTP.get_response`.
|
33
|
-
* Pass back headers provided by Rack app in the `HTTPResponse`.
|
34
|
-
|
35
|
-
## 3-Jun-2009 [mdub@dogbiscuit.org]
|
36
|
-
|
37
|
-
* Introduced `ShamRack#at` to simplify registration of apps.
|
38
|
-
|
39
|
-
## 13-May-2009 [mdub@dogbiscuit.org]
|
40
|
-
|
41
|
-
* Added accessors on HTTP object for address, port and rack_app.
|
42
|
-
* Added accessors to imitate "net/https".
|
data/README.markdown
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
ShamRack
|
2
|
-
========
|
3
|
-
|
4
|
-
ShamRack plumbs HTTP requests into [Rack][rack].
|
5
|
-
|
6
|
-
What's it for, again?
|
7
|
-
---------------------
|
8
|
-
|
9
|
-
Well, it makes it easy to _stub out external (HTTP) services_, which is handy in development and testing environments, or when you want to _test your HTTP client code_.
|
10
|
-
|
11
|
-
You can also use it to _test your Rack application_ (or Sinatra, or Rails, or Merb) using a variety of HTTP client libraries, to check interoperability. For instance, you could test your app using:
|
12
|
-
|
13
|
-
* [`rest-client`][rest-client]
|
14
|
-
* [`httparty`][httparty]
|
15
|
-
* [`oauth`][oauth]
|
16
|
-
|
17
|
-
all without having to boot it in a server.
|
18
|
-
|
19
|
-
Installing it
|
20
|
-
-------------
|
21
|
-
|
22
|
-
gem install sham_rack
|
23
|
-
|
24
|
-
Using it
|
25
|
-
--------
|
26
|
-
|
27
|
-
### A simple inline application
|
28
|
-
|
29
|
-
require 'sham_rack'
|
30
|
-
|
31
|
-
ShamRack.at("www.greetings.com") do |env|
|
32
|
-
["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]
|
33
|
-
end
|
34
|
-
|
35
|
-
require 'open-uri'
|
36
|
-
open("http://www.greetings.com/").read #=> "Hello, world!"
|
37
|
-
|
38
|
-
### Sinatra integration
|
39
|
-
|
40
|
-
ShamRack.at("sinatra.xyz").sinatra do
|
41
|
-
get "/hello/:subject" do
|
42
|
-
"Hello, #{params[:subject]}"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
open("http://sinatra.xyz/hello/stranger").read #=> "Hello, stranger"
|
47
|
-
|
48
|
-
### Rackup support
|
49
|
-
|
50
|
-
ShamRack.at("rackup.xyz").rackup do
|
51
|
-
use Some::Middleware
|
52
|
-
use Some::Other::Middleware
|
53
|
-
run MyApp.new
|
54
|
-
end
|
55
|
-
|
56
|
-
### Any old Rack app
|
57
|
-
|
58
|
-
ShamRack.at("google.com").mount(my_google_stub)
|
59
|
-
|
60
|
-
### General-purpose stubbing
|
61
|
-
|
62
|
-
@stub_app = ShamRack.at("stubbed.com").stub
|
63
|
-
@stub_app.register_resource("/greeting", "Hello, world!", "text/plain")
|
64
|
-
|
65
|
-
open("http://stubbed.com/greeting").read #=> "Hello, world!"
|
66
|
-
@stub_app.last_request.path #=> "/greeting"
|
67
|
-
|
68
|
-
Or, just use Sinatra, as described above ... it's almost as succinct, and heaps more powerful.
|
69
|
-
|
70
|
-
### When you're done testing
|
71
|
-
|
72
|
-
ShamRack.unmount_all
|
73
|
-
|
74
|
-
open("http://stubbed.com/greeting").read #=> OpenURI::HTTPError
|
75
|
-
|
76
|
-
Supported HTTP client libraries
|
77
|
-
-------------------------------
|
78
|
-
|
79
|
-
### Net::HTTP and friends
|
80
|
-
|
81
|
-
ShamRack supports requests made using Net::HTTP, or any of the numerous APIs built on top of it:
|
82
|
-
|
83
|
-
uri = URI.parse("http://www.greetings.com/")
|
84
|
-
Net::HTTP.get_response(uri).body #=> "Hello, world!"
|
85
|
-
|
86
|
-
require 'open-uri'
|
87
|
-
open("http://www.greetings.com/").read #=> "Hello, world!"
|
88
|
-
|
89
|
-
require 'restclient'
|
90
|
-
RestClient.get("http://www.greetings.com/").to_s #=> "Hello, world!"
|
91
|
-
|
92
|
-
require 'mechanize'
|
93
|
-
Mechanize.new.get("http://www.greetings.com/").body #=> "Hello, world!"
|
94
|
-
|
95
|
-
### Patron (experimental)
|
96
|
-
|
97
|
-
We've recently added support for [Patron][patron]:
|
98
|
-
|
99
|
-
require 'sham_rack/patron'
|
100
|
-
|
101
|
-
patron = Patron::Session.new
|
102
|
-
patron.get("http://www.greetings.com/").body #=> "Hello, world!"
|
103
|
-
|
104
|
-
What's the catch?
|
105
|
-
-----------------
|
106
|
-
|
107
|
-
* Your Rack request-handling code runs in the same Ruby VM, in fact the same Thread, as your request.
|
108
|
-
|
109
|
-
Thanks to
|
110
|
-
---------
|
111
|
-
|
112
|
-
* Blaine Cook for [FakeWeb][fakeweb], which was an inspiration for ShamRack.
|
113
|
-
* Perryn Fowler for his efforts plumbing Net::HTTP into ActionController::TestProcess.
|
114
|
-
* Christian Neukirchen et al for the chewy goodness that is [Rack][rack].
|
115
|
-
|
116
|
-
[rack]: http://rack.rubyforge.org/
|
117
|
-
[sinatra]: http://www.sinatrarb.com/
|
118
|
-
[rest-client]: http://github.com/adamwiggins/rest-client
|
119
|
-
[httparty]: http://github.com/jnunemaker/httparty
|
120
|
-
[oauth]: http://oauth.rubyforge.org/
|
121
|
-
[fakeweb]: http://fakeweb.rubyforge.org/
|
122
|
-
[mechanize]: http://mechanize.rubyforge.org
|
123
|
-
[patron]: http://github.com/toland/Patron
|