http_stub 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/lib/http_stub.rb +6 -2
  2. data/lib/http_stub/configurer.rb +2 -2
  3. data/lib/http_stub/configurer/request/hash_with_regexpable_values.rb +22 -0
  4. data/lib/http_stub/configurer/request/regexpable_value.rb +18 -0
  5. data/lib/http_stub/configurer/request/stub.rb +26 -0
  6. data/lib/http_stub/configurer/request/stub_activator.rb +18 -0
  7. data/lib/http_stub/models/hash_with_regexpable_values.rb +20 -0
  8. data/lib/http_stub/models/regexpable_value.rb +22 -0
  9. data/lib/http_stub/models/stub_headers.rb +3 -3
  10. data/lib/http_stub/models/stub_parameters.rb +3 -3
  11. data/lib/http_stub/models/stub_uri.rb +3 -4
  12. data/lib/http_stub/version.rb +1 -1
  13. data/spec/lib/http_stub/configurer/request/hash_with_regexpable_values_spec.rb +35 -0
  14. data/spec/lib/http_stub/configurer/request/regexpable_value_spec.rb +55 -0
  15. data/spec/lib/http_stub/configurer/{stub_activator_request_spec.rb → request/stub_activator_spec.rb} +4 -4
  16. data/spec/lib/http_stub/configurer/request/stub_spec.rb +152 -0
  17. data/spec/lib/http_stub/configurer_integration_spec.rb +102 -26
  18. data/spec/lib/http_stub/models/hash_with_regexpable_values_spec.rb +149 -0
  19. data/spec/lib/http_stub/models/regexpable_value_spec.rb +69 -0
  20. data/spec/lib/http_stub/models/stub_headers_spec.rb +34 -86
  21. data/spec/lib/http_stub/models/stub_parameters_spec.rb +22 -11
  22. data/spec/lib/http_stub/models/stub_uri_spec.rb +16 -47
  23. data/spec/spec_helper.rb +1 -1
  24. metadata +21 -9
  25. data/lib/http_stub/configurer/stub_activator_request.rb +0 -16
  26. data/lib/http_stub/configurer/stub_request.rb +0 -24
  27. data/spec/lib/http_stub/configurer/stub_request_spec.rb +0 -129
@@ -224,28 +224,68 @@ describe HttpStub::Configurer, "when the server is running" do
224
224
 
225
225
  describe "that contains headers" do
226
226
 
227
- before(:each) do
228
- configurer.stub_response!("/stub_with_headers", method: :get, headers: { key: "value" },
229
- response: { status: 202, body: "Another stub body" })
230
- end
227
+ describe "whose values are strings" do
228
+
229
+ before(:each) do
230
+ configurer.stub_response!("/stub_with_headers", method: :get, headers: { key: "value" },
231
+ response: { status: 202, body: "Another stub body" })
232
+ end
233
+
234
+ describe "and that request is made" do
231
235
 
232
- describe "and that request is made" do
236
+ let(:response) { HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "value" }) }
233
237
 
234
- let(:response) { HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "value" }) }
238
+ it "should replay the stubbed response" do
239
+ response.code.should eql(202)
240
+ response.body.should eql("Another stub body")
241
+ end
242
+
243
+ end
244
+
245
+ describe "and a request with different headers is made" do
246
+
247
+ let(:response) do
248
+ HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "other_value" })
249
+ end
250
+
251
+ it "should respond with a 404 status code" do
252
+ response.code.should eql(404)
253
+ end
235
254
 
236
- it "should replay the stubbed response" do
237
- response.code.should eql(202)
238
- response.body.should eql("Another stub body")
239
255
  end
240
256
 
241
257
  end
242
258
 
243
- describe "and a request with different headers is made" do
259
+ describe "whose values are regular expressions" do
244
260
 
245
- let(:response) { HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "other_value" }) }
261
+ before(:each) do
262
+ configurer.stub_response!("/stub_with_headers", method: :get, headers: { key: /^match.*/ },
263
+ response: { status: 202, body: "Another stub body" })
264
+ end
265
+
266
+ describe "and a request that matches is made" do
267
+
268
+ let(:response) do
269
+ HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "matching_value" })
270
+ end
271
+
272
+ it "should replay the stubbed response" do
273
+ response.code.should eql(202)
274
+ response.body.should eql("Another stub body")
275
+ end
276
+
277
+ end
278
+
279
+ describe "and a request that does not match is made" do
280
+
281
+ let(:response) do
282
+ HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "does_not_match_value" })
283
+ end
284
+
285
+ it "should respond with a 404 status code" do
286
+ response.code.should eql(404)
287
+ end
246
288
 
247
- it "should respond with a 404 status code" do
248
- response.code.should eql(404)
249
289
  end
250
290
 
251
291
  end
@@ -254,28 +294,64 @@ describe HttpStub::Configurer, "when the server is running" do
254
294
 
255
295
  describe "that contains parameters" do
256
296
 
257
- before(:each) do
258
- configurer.stub_response!("/stub_with_parameters", method: :get, parameters: { key: "value" },
259
- response: { status: 202, body: "Another stub body" })
260
- end
297
+ describe "whose values are strings" do
298
+
299
+ before(:each) do
300
+ configurer.stub_response!("/stub_with_parameters", method: :get, parameters: { key: "value" },
301
+ response: { status: 202, body: "Another stub body" })
302
+ end
303
+
304
+ describe "and that request is made" do
261
305
 
262
- describe "and that request is made" do
306
+ let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=value", 8001) }
263
307
 
264
- let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=value", 8001) }
308
+ it "should replay the stubbed response" do
309
+ response.code.should eql("202")
310
+ response.body.should eql("Another stub body")
311
+ end
312
+
313
+ end
314
+
315
+ describe "and a request with different parameters is made" do
316
+
317
+ let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=another_value", 8001) }
318
+
319
+ it "should respond with a 404 status code" do
320
+ response.code.should eql("404")
321
+ end
265
322
 
266
- it "should replay the stubbed response" do
267
- response.code.should eql("202")
268
- response.body.should eql("Another stub body")
269
323
  end
270
324
 
271
325
  end
272
326
 
273
- describe "and a request with different parameters is made" do
327
+ describe "whose values are regular expressions" do
274
328
 
275
- let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=another_value", 8001) }
329
+ before(:each) do
330
+ configurer.stub_response!("/stub_with_parameters", method: :get, parameters: { key: /^match.*/ },
331
+ response: { status: 202, body: "Another stub body" })
332
+ end
333
+
334
+ describe "and a request that matches is made" do
335
+
336
+ let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=matching_value", 8001) }
337
+
338
+ it "should replay the stubbed response" do
339
+ response.code.should eql("202")
340
+ response.body.should eql("Another stub body")
341
+ end
342
+
343
+ end
344
+
345
+ describe "and a request that does not match is made" do
346
+
347
+ let(:response) do
348
+ Net::HTTP.get_response("localhost", "/stub_with_parameters?key=does_not_match_value", 8001)
349
+ end
350
+
351
+ it "should respond with a 404 status code" do
352
+ response.code.should eql("404")
353
+ end
276
354
 
277
- it "should respond with a 404 status code" do
278
- response.code.should eql("404")
279
355
  end
280
356
 
281
357
  end
@@ -0,0 +1,149 @@
1
+ describe HttpStub::Models::HashWithRegexpableValues do
2
+
3
+ let(:stubbed_hash) do
4
+ (1..3).reduce({}) do |result, i|
5
+ result["key#{i}"] = "value#{i}"
6
+ result
7
+ end
8
+ end
9
+
10
+ let(:regexpable_values) do
11
+ stubbed_hash.values.map { |value| double(HttpStub::Models::RegexpableValue, to_s: value).as_null_object }
12
+ end
13
+
14
+ let(:regexpable_hash) { HttpStub::Models::HashWithRegexpableValues.new(stubbed_hash) }
15
+
16
+ before(:each) do
17
+ regexpable_values.each { |value| HttpStub::Models::RegexpableValue.stub!(:new).with(value.to_s).and_return(value) }
18
+ end
19
+
20
+ it "should be hash" do
21
+ regexpable_hash.should be_a(Hash)
22
+ end
23
+
24
+ describe "constructor" do
25
+
26
+ it "should create a regexpable representation of each value in the hash" do
27
+ regexpable_values.each do |value|
28
+ HttpStub::Models::RegexpableValue.should_receive(:new).with(value.to_s).and_return(value)
29
+ end
30
+
31
+ regexpable_hash.values.should eql(regexpable_values)
32
+ end
33
+
34
+ end
35
+
36
+ describe "#match?" do
37
+
38
+ describe "when the stubbed hash contains multiple entries" do
39
+
40
+ describe "and the provided hash has the same number of entries" do
41
+
42
+ describe "and the keys match" do
43
+
44
+ let(:provided_hash) do
45
+ stubbed_hash.reduce({}) do |result, entry|
46
+ result[entry[0]] = "another #{entry[1]}"
47
+ result
48
+ end
49
+ end
50
+
51
+ describe "and the values match" do
52
+
53
+ before(:each) do
54
+ regexpable_values.each { |value| value.stub!(:match?).with("another #{value}").and_return(true) }
55
+ end
56
+
57
+ it "should return true" do
58
+ regexpable_hash.match?(provided_hash).should be_true
59
+ end
60
+
61
+ end
62
+
63
+ describe "and a value does not match" do
64
+
65
+ before(:each) do
66
+ regexpable_values.each { |value| value.stub!(:match?).with("another #{value}").and_return(true) }
67
+
68
+ non_matching_value = regexpable_values[1]
69
+ non_matching_value.stub!(:match?).with("another #{non_matching_value}").and_return(false)
70
+ end
71
+
72
+ it "should return false" do
73
+ regexpable_hash.match?(provided_hash).should be_false
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+
80
+ describe "and a key does not match" do
81
+
82
+ let(:provided_hash) { { "key1" => "value1", "differentkey2" => "another value2", "key3" => "value3" } }
83
+
84
+ before(:each) do
85
+ regexpable_values.each { |value| value.stub!(:match?).with(value.to_s).and_return(true) }
86
+ end
87
+
88
+ it "should return false" do
89
+ regexpable_hash.match?(provided_hash).should be_false
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
96
+ describe "and the provided hash contains more entries" do
97
+
98
+ let(:provided_hash) do
99
+ (1..5).reduce({}) do |result, i|
100
+ result["key#{i}"] = "another value#{i}"
101
+ result
102
+ end
103
+ end
104
+
105
+ describe "and it has matching keys and values" do
106
+
107
+ before(:each) do
108
+ regexpable_values.each { |value| value.stub!(:match?).with("another #{value}").and_return(true) }
109
+ end
110
+
111
+ it "should return true" do
112
+ regexpable_hash.match?(provided_hash).should be_true
113
+ end
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
121
+ describe "when the stubbed hash is empty" do
122
+
123
+ let(:stubbed_hash) { {} }
124
+
125
+ describe "and the provided hash is not empty" do
126
+
127
+ let(:provided_hash) { { "key" => "value" } }
128
+
129
+ it "should return true" do
130
+ regexpable_hash.match?(provided_hash).should be_true
131
+ end
132
+
133
+ end
134
+
135
+ describe "and the provided hash is empty" do
136
+
137
+ let(:provided_hash) { {} }
138
+
139
+ it "should return true" do
140
+ regexpable_hash.match?(provided_hash).should be_true
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ end
148
+
149
+ end
@@ -0,0 +1,69 @@
1
+ describe HttpStub::Models::RegexpableValue do
2
+
3
+ let(:value) { "a simple string" }
4
+
5
+ let(:regexpable_value) { HttpStub::Models::RegexpableValue.new(value) }
6
+
7
+ describe "#match?" do
8
+
9
+ describe "when the value is a string prefixed with 'regexp:'" do
10
+
11
+ let(:value) { "regexp:^a[0-9]*z$" }
12
+
13
+ describe "and the provided value matches the regular expression" do
14
+
15
+ let(:other_value) { "a789z" }
16
+
17
+ it "should return true" do
18
+ regexpable_value.match?(other_value).should be_true
19
+ end
20
+
21
+ end
22
+
23
+ describe "and the provided value does not match the regular expression" do
24
+
25
+ let(:other_value) { "a789" }
26
+
27
+ it "should return false" do
28
+ regexpable_value.match?(other_value).should be_false
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ describe "when the value is a simple string" do
36
+
37
+ describe "and the provided value is equal to the string" do
38
+
39
+ let(:other_value) { value }
40
+
41
+ it "should return true" do
42
+ regexpable_value.match?(other_value).should be_true
43
+ end
44
+
45
+ end
46
+
47
+ describe "and the provided value is not equal to the string" do
48
+
49
+ let(:other_value) { "a not equal string" }
50
+
51
+ it "should return false" do
52
+ regexpable_value.match?(other_value).should be_false
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
60
+
61
+ describe "#to_s" do
62
+
63
+ it "should return the value provided" do
64
+ regexpable_value.to_s.should eql(value)
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -1,114 +1,62 @@
1
1
  describe HttpStub::Models::StubHeaders do
2
2
 
3
3
  let(:request) { double("HttpRequest") }
4
+ let(:stubbed_headers) { { "stub_key" => "value" } }
4
5
 
5
6
  let(:stub_headers) { HttpStub::Models::StubHeaders.new(stubbed_headers) }
6
7
 
7
- describe "#match?" do
8
-
9
- before(:each) { HttpStub::Models::RequestHeaderParser.stub!(:parse).with(request).and_return(request_headers) }
10
-
11
- describe "when multiple headers are mandatory" do
12
-
13
- let(:stubbed_headers) { { "KEY1" => "value1", "KEY2" => "value2", "KEY3" => "value3" } }
14
-
15
- describe "and the mandatory headers are provided" do
16
-
17
- let(:request_headers) { stubbed_headers }
18
-
19
- describe "and the casing of the header names is identical" do
20
-
21
- it "should return true" do
22
- stub_headers.match?(request).should be_true
23
- end
24
-
25
- end
26
-
27
- describe "and the casing of the header names is different" do
28
-
29
- let(:stubbed_headers) { { "key1" => "value1", "KEY2" => "value2", "key3" => "value3" } }
30
-
31
- it "should return true" do
32
- stub_headers.match?(request).should be_true
33
- end
34
-
35
- end
36
-
37
- describe "and the mandatory request header names have hyphens in place of underscores" do
38
-
39
- let(:stubbed_headers) { { "KEY_1" => "value1", "KEY-2" => "value2", "KEY_3" => "value3" } }
40
- let(:request_headers) { { "KEY-1" => "value1", "KEY_2" => "value2", "KEY-3" => "value3" } }
41
-
42
- it "should return true" do
43
- stub_headers.match?(request).should be_true
44
- end
45
-
46
- end
47
-
48
- end
49
-
50
- describe "and the request headers have different values" do
51
-
52
- let(:request_headers) { { "KEY1" => "value1", "KEY2" => "doesNotMatch", "KEY3" => "value3" } }
8
+ describe "when stubbed headers are provided" do
53
9
 
54
- it "should return false" do
55
- stub_headers.match?(request).should be_false
56
- end
10
+ it "should create a regexpable representation of the stubbed headers whose keys are downcased and underscored" do
11
+ downcased_and_underscored_hash = { "another_stub_key" => "value" }
12
+ stubbed_headers.should_receive(:downcase_and_underscore_keys).and_return(downcased_and_underscored_hash)
13
+ HttpStub::Models::HashWithRegexpableValues.should_receive(:new).with(downcased_and_underscored_hash)
57
14
 
58
- end
59
-
60
- describe "and some mandatory headers are omitted" do
61
-
62
- let(:request_headers) { { "KEY1" => "value1", "KEY3" => "value3" } }
63
-
64
- it "should return false" do
65
- stub_headers.match?(request).should be_false
66
- end
67
-
68
- end
15
+ stub_headers
16
+ end
69
17
 
70
- describe "and all mandatory headers are omitted" do
18
+ end
71
19
 
72
- let(:request_headers) { {} }
20
+ describe "when the stubbed headers are nil" do
73
21
 
74
- it "should return false" do
75
- stub_headers.match?(request).should be_false
76
- end
22
+ let(:stubbed_headers) { nil }
77
23
 
78
- end
24
+ it "should create a regexpable representation of an empty hash" do
25
+ HttpStub::Models::HashWithRegexpableValues.should_receive(:new).with({})
79
26
 
27
+ stub_headers
80
28
  end
81
29
 
82
- describe "when no headers are mandatory" do
83
-
84
- let(:stubbed_headers) { {} }
85
-
86
- describe "and headers are provided" do
87
-
88
- let(:request_headers) { { "KEY" => "value" } }
30
+ end
89
31
 
90
- it "should return true" do
91
- stub_headers.match?(request).should be_true
92
- end
32
+ describe "#match?" do
93
33
 
94
- end
34
+ let(:request_headers) { { "request_key" => "value" } }
35
+ let(:regexpable_stubbed_headers) { double(HttpStub::Models::HashWithRegexpableValues).as_null_object }
95
36
 
37
+ before(:each) do
38
+ HttpStub::Models::HashWithRegexpableValues.stub!(:new).and_return(regexpable_stubbed_headers)
39
+ HttpStub::Models::RequestHeaderParser.stub!(:parse).with(request).and_return(request_headers)
96
40
  end
97
41
 
98
- describe "when the mandatory headers are nil" do
42
+ it "should parse the requests headers into a hash" do
43
+ HttpStub::Models::RequestHeaderParser.should_receive(:parse).with(request).and_return(request_headers)
99
44
 
100
- let(:stubbed_headers) { nil }
101
-
102
- describe "and headers are provided" do
45
+ stub_headers.match?(request)
46
+ end
103
47
 
104
- let(:request_headers) { { "KEY" => "value" } }
48
+ it "should downcase and underscore the keys in the request header hash" do
49
+ request_headers.should_receive(:downcase_and_underscore_keys)
105
50
 
106
- it "should return true" do
107
- stub_headers.match?(request).should be_true
108
- end
51
+ stub_headers.match?(request)
52
+ end
109
53
 
110
- end
54
+ it "should delegate to the regexpable representation of the stubbed headers to determine a match" do
55
+ downcased_and_underscored_hash = { "another_request_key" => "value" }
56
+ request_headers.stub!(:downcase_and_underscore_keys).and_return(downcased_and_underscored_hash)
57
+ regexpable_stubbed_headers.should_receive(:match?).with(downcased_and_underscored_hash).and_return(true)
111
58
 
59
+ stub_headers.match?(request).should be_true
112
60
  end
113
61
 
114
62
  end