webmock 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +47 -2
- data/README.md +68 -6
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/webmock.rb +2 -2
- data/lib/webmock/adapters/rspec.rb +1 -1
- data/lib/webmock/adapters/rspec/matchers.rb +2 -2
- data/lib/webmock/adapters/rspec/{request_profile_matcher.rb → request_pattern_matcher.rb} +5 -5
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +2 -2
- data/lib/webmock/config.rb +2 -1
- data/lib/webmock/http_lib_adapters/httpclient.rb +5 -4
- data/lib/webmock/http_lib_adapters/net_http.rb +5 -3
- data/lib/webmock/http_lib_adapters/patron.rb +82 -0
- data/lib/webmock/request_execution_verifier.rb +8 -8
- data/lib/webmock/request_pattern.rb +130 -0
- data/lib/webmock/request_registry.rb +4 -9
- data/lib/webmock/request_signature.rb +18 -37
- data/lib/webmock/request_stub.rb +17 -6
- data/lib/webmock/response.rb +87 -31
- data/lib/webmock/util/headers.rb +5 -0
- data/lib/webmock/webmock.rb +10 -6
- data/spec/httpclient_spec.rb +0 -1
- data/spec/httpclient_spec_helper.rb +11 -1
- data/spec/net_http_spec.rb +8 -1
- data/spec/net_http_spec_helper.rb +11 -1
- data/spec/patron_spec.rb +83 -0
- data/spec/patron_spec_helper.rb +44 -0
- data/spec/request_execution_verifier_spec.rb +8 -8
- data/spec/request_pattern_spec.rb +243 -0
- data/spec/request_registry_spec.rb +34 -19
- data/spec/request_signature_spec.rb +23 -191
- data/spec/request_stub_spec.rb +32 -11
- data/spec/response_spec.rb +98 -5
- data/spec/spec_helper.rb +8 -16
- data/spec/webmock_spec.rb +154 -49
- data/webmock.gemspec +14 -7
- metadata +21 -7
- data/lib/webmock/request.rb +0 -29
- data/lib/webmock/request_profile.rb +0 -50
- data/spec/request_profile_spec.rb +0 -68
@@ -4,7 +4,7 @@ describe RequestRegistry do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
RequestRegistry.instance.reset_webmock
|
7
|
-
@
|
7
|
+
@request_pattern = RequestPattern.new(:get, "www.example.com")
|
8
8
|
@request_signature = RequestSignature.new(:get, "www.example.com")
|
9
9
|
@request_stub = RequestStub.new(:get, "www.example.com")
|
10
10
|
end
|
@@ -22,9 +22,9 @@ describe RequestRegistry do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should clean list of executed requests" do
|
25
|
-
RequestRegistry.instance.times_executed(@
|
25
|
+
RequestRegistry.instance.times_executed(@request_pattern).should == 1
|
26
26
|
RequestRegistry.instance.reset_webmock
|
27
|
-
RequestRegistry.instance.times_executed(@
|
27
|
+
RequestRegistry.instance.times_executed(@request_pattern).should == 0
|
28
28
|
end
|
29
29
|
|
30
30
|
end
|
@@ -49,10 +49,25 @@ describe RequestRegistry do
|
|
49
49
|
|
50
50
|
describe "response for request" do
|
51
51
|
|
52
|
-
it "should registered response for request
|
53
|
-
@request_stub.
|
52
|
+
it "should report registered evaluated response for request pattern" do
|
53
|
+
@request_stub.to_return(:body => "abc")
|
54
54
|
RequestRegistry.instance.register_request_stub(@request_stub)
|
55
|
-
RequestRegistry.instance.response_for_request(@request_signature).should ==
|
55
|
+
RequestRegistry.instance.response_for_request(@request_signature).should == Response.new(:body => "abc")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should report evaluated response" do
|
59
|
+
@request_stub.to_return {|request| {:body => request.method.to_s} }
|
60
|
+
RequestRegistry.instance.register_request_stub(@request_stub)
|
61
|
+
response1 = RequestRegistry.instance.response_for_request(@request_signature)
|
62
|
+
response1.should == Response.new(:body => "get")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should report clone of theresponse" do
|
66
|
+
@request_stub.to_return {|request| {:body => request.method.to_s} }
|
67
|
+
RequestRegistry.instance.register_request_stub(@request_stub)
|
68
|
+
response1 = RequestRegistry.instance.response_for_request(@request_signature)
|
69
|
+
response2 = RequestRegistry.instance.response_for_request(@request_signature)
|
70
|
+
response1.should_not be(response2)
|
56
71
|
end
|
57
72
|
|
58
73
|
it "should report nothing if no response for request is registered" do
|
@@ -61,24 +76,24 @@ describe RequestRegistry do
|
|
61
76
|
|
62
77
|
it "should always return last registered matching response" do
|
63
78
|
@request_stub1 = RequestStub.new(:get, "www.example.com")
|
64
|
-
@request_stub1.
|
79
|
+
@request_stub1.to_return(:body => "abc")
|
65
80
|
@request_stub2 = RequestStub.new(:get, "www.example.com")
|
66
|
-
@request_stub2.
|
81
|
+
@request_stub2.to_return(:body => "def")
|
67
82
|
@request_stub3 = RequestStub.new(:get, "www.example.org")
|
68
|
-
@request_stub3.
|
83
|
+
@request_stub3.to_return(:body => "ghj")
|
69
84
|
RequestRegistry.instance.register_request_stub(@request_stub1)
|
70
85
|
RequestRegistry.instance.register_request_stub(@request_stub2)
|
71
86
|
RequestRegistry.instance.register_request_stub(@request_stub3)
|
72
|
-
RequestRegistry.instance.response_for_request(@request_signature).should ==
|
87
|
+
RequestRegistry.instance.response_for_request(@request_signature).should == Response.new(:body => "def")
|
73
88
|
end
|
74
89
|
|
75
90
|
end
|
76
91
|
|
77
92
|
describe "times executed" do
|
78
93
|
|
79
|
-
def times_executed(
|
80
|
-
self.requested.hash.select { |
|
81
|
-
|
94
|
+
def times_executed(request_pattern)
|
95
|
+
self.requested.hash.select { |executed_request_pattern, times_executed|
|
96
|
+
executed_request_pattern.match(request_pattern)
|
82
97
|
}.inject(0) {|sum, (_, times_executed)| sum =+ times_executed }
|
83
98
|
end
|
84
99
|
|
@@ -91,16 +106,16 @@ describe RequestRegistry do
|
|
91
106
|
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.example.org"))
|
92
107
|
end
|
93
108
|
|
94
|
-
it "should report 0 if no request matching
|
95
|
-
RequestRegistry.instance.times_executed(
|
109
|
+
it "should report 0 if no request matching pattern was requested" do
|
110
|
+
RequestRegistry.instance.times_executed(RequestPattern.new(:get, "www.example.net")).should == 0
|
96
111
|
end
|
97
112
|
|
98
|
-
it "should report number of times matching
|
99
|
-
RequestRegistry.instance.times_executed(
|
113
|
+
it "should report number of times matching pattern was requested" do
|
114
|
+
RequestRegistry.instance.times_executed(RequestPattern.new(:get, "www.example.com")).should == 2
|
100
115
|
end
|
101
116
|
|
102
|
-
it "should report number of times all matching
|
103
|
-
RequestRegistry.instance.times_executed(
|
117
|
+
it "should report number of times all matching pattern were requested" do
|
118
|
+
RequestRegistry.instance.times_executed(RequestPattern.new(:get, /.*example.*/)).should == 3
|
104
119
|
end
|
105
120
|
|
106
121
|
|
@@ -1,205 +1,37 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe RequestSignature do
|
4
|
+
|
5
|
+
describe "initialization" do
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
should match(RequestProfile.new(:get, "www.example.com"))
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should match if uri matches and method is any" do
|
13
|
-
RequestSignature.new(:get, "www.example.com").
|
14
|
-
should match(RequestProfile.new(:any, "www.example.com"))
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should not match if other request profile has different method" do
|
18
|
-
RequestSignature.new(:get, "www.example.com").
|
19
|
-
should_not match(RequestProfile.new(:post, "www.example.com"))
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should match if uri matches other uri" do
|
23
|
-
RequestSignature.new(:get, "www.example.com").
|
24
|
-
should match(RequestProfile.new(:get, "www.example.com"))
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should match if uri matches other escaped using uri" do
|
28
|
-
RequestSignature.new(:get, "www.example.com/my path").
|
29
|
-
should match(RequestProfile.new(:get, "www.example.com/my%20path"))
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should match if unescaped uri matches other uri" do
|
33
|
-
RequestSignature.new(:get, "www.example.com/my%20path").
|
34
|
-
should match(RequestProfile.new(:get, "www.example.com/my path"))
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should match if unescaped uri matches other regexp uri" do
|
38
|
-
RequestSignature.new(:get, "www.example.com/my%20path").
|
39
|
-
should match(RequestProfile.new(:get, /.*my path.*/))
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should match if uri matches other regex uri" do
|
43
|
-
RequestSignature.new(:get, "www.example.com").
|
44
|
-
should match(RequestProfile.new(:get, /.*example.*/))
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should match for uris with same parameters" do
|
48
|
-
RequestSignature.new(:get, "www.example.com?a=1&b=2").
|
49
|
-
should match(RequestProfile.new(:get, "www.example.com?a=1&b=2"))
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should not match for uris with different parameters" do
|
53
|
-
RequestSignature.new(:get, "www.example.com?a=2&b=1").
|
54
|
-
should_not match(RequestProfile.new(:get, "www.example.com?a=1&b=2"))
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should match for parameters in different order" do
|
58
|
-
RequestSignature.new(:get, "www.example.com?a=1&b=2").
|
59
|
-
should match(RequestProfile.new(:get, "www.example.com?b=2&a=1"))
|
60
|
-
end
|
61
|
-
|
62
|
-
describe "when parameters are escaped" do
|
63
|
-
|
64
|
-
it "should match if uri with non escaped parameters is the same as other uri with escaped parameters" do
|
65
|
-
RequestSignature.new(:get, "www.example.com/?a=a b").
|
66
|
-
should match(RequestProfile.new(:get, "www.example.com/?a=a%20b"))
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should match if uri with escaped parameters is the same as other uri with non escaped parameters" do
|
70
|
-
RequestSignature.new(:get, "www.example.com/?a=a%20b").
|
71
|
-
should match(RequestProfile.new(:get, "www.example.com/?a=a b"))
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should match if other regexp is for non escaped parameters but uri has escaped parameters" do
|
75
|
-
RequestSignature.new(:get, "www.example.com/?a=a%20b").
|
76
|
-
should match(RequestProfile.new(:get, /.*a=a b.*/))
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should match if other regexp is for escaped parameters but uri has non escaped parameters" do
|
80
|
-
RequestSignature.new(:get, "www.example.com/?a=a b").
|
81
|
-
should match(RequestProfile.new(:get, /.*a=a%20b.*/))
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
it "should match for same bodies" do
|
89
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
90
|
-
should match(RequestProfile.new(:get, "www.example.com", :body => "abc"))
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should match for body matching regexp" do
|
94
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
95
|
-
should match(RequestProfile.new(:get, "www.example.com", :body => /^abc$/))
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should not match for different bodies" do
|
99
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
100
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :body => "def"))
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should not match for body not matching regexp" do
|
104
|
-
RequestSignature.new(:get, "www.example.com", :body => "xabc").
|
105
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :body => /^abc$/))
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should match if other has not specified body" do
|
109
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
110
|
-
should match(RequestProfile.new(:get, "www.example.com"))
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should not match if other has nil body" do
|
114
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
115
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :body => nil))
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should not match if other has empty body" do
|
119
|
-
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
120
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :body => ""))
|
121
|
-
end
|
122
|
-
|
123
|
-
it "should not match if other has body" do
|
124
|
-
RequestSignature.new(:get, "www.example.com").
|
125
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :body => "abc"))
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should match for same headers" do
|
129
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
130
|
-
should match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
131
|
-
end
|
132
|
-
|
133
|
-
it "should match for header values matching regexp" do
|
134
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
135
|
-
should match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}))
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should not match for different values of the same header" do
|
139
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
140
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}))
|
141
|
-
end
|
142
|
-
|
143
|
-
it "should not match for header values not matching regexp" do
|
144
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpegx'}).
|
145
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}))
|
146
|
-
end
|
147
|
-
|
148
|
-
it "should match if request has more headers than other" do
|
149
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
|
150
|
-
should match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
151
|
-
end
|
152
|
-
|
153
|
-
it "should not match if request has less headers that the other and all match" do
|
154
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
155
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should match even is header keys or values are in different format" do
|
159
|
-
RequestSignature.new(:get, "www.example.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}).
|
160
|
-
should match(RequestProfile.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}))
|
161
|
-
end
|
162
|
-
|
163
|
-
it "should match is other has not specified" do
|
164
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
165
|
-
should match(RequestProfile.new(:get, "www.example.com"))
|
166
|
-
end
|
167
|
-
|
168
|
-
it "should not match is other has nil headers" do
|
169
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
170
|
-
should match(RequestProfile.new(:get, "www.example.com", :headers => nil))
|
171
|
-
end
|
172
|
-
|
173
|
-
it "should not match if other has empty headers" do
|
174
|
-
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
175
|
-
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {}))
|
7
|
+
it "should have assigned normalized uri" do
|
8
|
+
WebMock::Util::URI.should_receive(:normalize_uri).and_return("www.example.kom")
|
9
|
+
signature = RequestSignature.new(:get, "www.example.com")
|
10
|
+
signature.uri.should == "www.example.kom"
|
176
11
|
end
|
177
12
|
|
178
|
-
it "should
|
179
|
-
|
180
|
-
|
13
|
+
it "should have assigned uri without normalization if uri is URI" do
|
14
|
+
WebMock::Util::URI.should_not_receive(:normalize_uri)
|
15
|
+
uri = Addressable::URI.parse("www.example.com")
|
16
|
+
signature = RequestSignature.new(:get, uri)
|
17
|
+
signature.uri.should == uri
|
181
18
|
end
|
182
19
|
|
183
|
-
it "should
|
184
|
-
|
185
|
-
|
20
|
+
it "should have assigned normalized headers" do
|
21
|
+
WebMock::Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
22
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).headers.should == {'B' => 'b'}
|
186
23
|
end
|
187
24
|
|
188
|
-
it "should
|
189
|
-
RequestSignature.new(:get, "www.example.com").
|
190
|
-
should match(RequestProfile.new(:get, "www.example.com").with { |request| true } )
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should not match if block given to profile evaluates on request to false" do
|
194
|
-
RequestSignature.new(:get, "www.example.com").
|
195
|
-
should_not match( RequestProfile.new(:get, "www.example.com").with { |request| false } )
|
196
|
-
end
|
197
|
-
|
198
|
-
it "should pass self to the block when matching" do
|
199
|
-
signature = RequestSignature.new(:get, "www.example.com")
|
200
|
-
signature.should match(RequestProfile.new(:get, "www.example.com").with { |request| request == signature } )
|
25
|
+
it "should have assigned body" do
|
26
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").body.should == "abc"
|
201
27
|
end
|
202
28
|
|
203
29
|
end
|
204
30
|
|
205
|
-
|
31
|
+
it "should report string describing itself" do
|
32
|
+
RequestSignature.new(:get, "www.example.com",
|
33
|
+
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
|
34
|
+
"GET http://www.example.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/spec/request_stub_spec.rb
CHANGED
@@ -6,9 +6,8 @@ describe RequestStub do
|
|
6
6
|
@request_stub = RequestStub.new(:get, "www.example.com")
|
7
7
|
end
|
8
8
|
|
9
|
-
it "should have request
|
10
|
-
@request_stub.
|
11
|
-
@request_stub.request_profile.uri.host.should == "www.example.com"
|
9
|
+
it "should have request pattern with method and uri" do
|
10
|
+
@request_stub.request_pattern.to_s.should == "GET http://www.example.com/"
|
12
11
|
end
|
13
12
|
|
14
13
|
it "should have response" do
|
@@ -17,20 +16,20 @@ describe RequestStub do
|
|
17
16
|
|
18
17
|
describe "with" do
|
19
18
|
|
20
|
-
it "should assign body to request
|
19
|
+
it "should assign body to request pattern" do
|
21
20
|
@request_stub.with(:body => "abc")
|
22
|
-
@request_stub.
|
21
|
+
@request_stub.request_pattern.to_s.should == RequestPattern.new(:get, "www.example.com", :body => "abc").to_s
|
23
22
|
end
|
24
23
|
|
25
|
-
it "should assign normalized headers to request
|
26
|
-
Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
24
|
+
it "should assign normalized headers to request pattern" do
|
27
25
|
@request_stub.with(:headers => {'A' => 'a'})
|
28
|
-
@request_stub.
|
26
|
+
@request_stub.request_pattern.to_s.should ==
|
27
|
+
RequestPattern.new(:get, "www.example.com", :headers => {'A' => 'a'}).to_s
|
29
28
|
end
|
30
29
|
|
31
30
|
it "should assign given block to request profile" do
|
32
|
-
@request_stub.with { |req|
|
33
|
-
@request_stub.
|
31
|
+
@request_stub.with { |req| req.body == "abc" }
|
32
|
+
@request_stub.request_pattern.matches?(RequestSignature.new(:get, "www.example.com", :body => "abc")).should be_true
|
34
33
|
end
|
35
34
|
|
36
35
|
end
|
@@ -40,7 +39,7 @@ describe RequestStub do
|
|
40
39
|
it "should assign response with provided options" do
|
41
40
|
@request_stub.to_return(:body => "abc", :status => 500)
|
42
41
|
@request_stub.response.body.should == "abc"
|
43
|
-
@request_stub.response.status.should == 500
|
42
|
+
@request_stub.response.status.should == [500, ""]
|
44
43
|
end
|
45
44
|
|
46
45
|
it "should assign responses with provided options" do
|
@@ -124,6 +123,28 @@ describe RequestStub do
|
|
124
123
|
|
125
124
|
end
|
126
125
|
|
126
|
+
describe "to_timeout" do
|
127
|
+
|
128
|
+
it "should assign response with timeout" do
|
129
|
+
@request_stub.to_timeout
|
130
|
+
@request_stub.response.should_timeout.should be_true
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should assign sequence of responses with response with timeout" do
|
134
|
+
@request_stub.to_return(:body => "abc").then.to_timeout
|
135
|
+
@request_stub.response.body.should == "abc"
|
136
|
+
@request_stub.response.should_timeout.should be_true
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should allow multiple timeouts to be declared" do
|
140
|
+
@request_stub.to_timeout.then.to_timeout.then.to_return(:body => "abc")
|
141
|
+
@request_stub.response.should_timeout.should be_true
|
142
|
+
@request_stub.response.should_timeout.should be_true
|
143
|
+
@request_stub.response.body.should == "abc"
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
127
148
|
|
128
149
|
describe "times" do
|
129
150
|
|
data/spec/response_spec.rb
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
+
describe ResponseFactory do
|
4
|
+
|
5
|
+
describe "response_for" do
|
6
|
+
|
7
|
+
it "should create response with options passed as arguments" do
|
8
|
+
options = {:body => "abc", :headers => {:a => :b}}
|
9
|
+
Response.should_receive(:new).with(options).and_return(@response = mock(Response))
|
10
|
+
ResponseFactory.response_for(options).should == @response
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
it "should create dynamic response for argument responding to call" do
|
15
|
+
callable = mock(:call => {:body => "abc"})
|
16
|
+
DynamicResponse.should_receive(:new).with(callable).and_return(@response = mock(Response))
|
17
|
+
ResponseFactory.response_for(callable).should == @response
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
3
24
|
describe Response do
|
4
25
|
before(:each) do
|
5
26
|
@response = Response.new(:headers => {'A' => 'a'})
|
@@ -13,13 +34,18 @@ describe Response do
|
|
13
34
|
|
14
35
|
describe "status" do
|
15
36
|
|
16
|
-
it "should
|
17
|
-
@response.status.should == 200
|
37
|
+
it "should have 200 code and empty message by default" do
|
38
|
+
@response.status.should == [200, ""]
|
18
39
|
end
|
19
40
|
|
20
41
|
it "should return assigned status" do
|
21
42
|
@response = Response.new(:status => 500)
|
22
|
-
@response.status.should == 500
|
43
|
+
@response.status.should == [500, ""]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return assigned message" do
|
47
|
+
@response = Response.new(:status => [500, "Internal Server Error"])
|
48
|
+
@response.status.should == [500, "Internal Server Error"]
|
23
49
|
end
|
24
50
|
|
25
51
|
end
|
@@ -38,6 +64,20 @@ describe Response do
|
|
38
64
|
end
|
39
65
|
|
40
66
|
end
|
67
|
+
|
68
|
+
describe "timeout" do
|
69
|
+
|
70
|
+
it "should know if it should timeout" do
|
71
|
+
@response = Response.new(:should_timeout => true)
|
72
|
+
@response.should_timeout.should be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should not timeout by default" do
|
76
|
+
@response = Response.new
|
77
|
+
@response.should_timeout.should be_false
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
41
81
|
|
42
82
|
describe "body" do
|
43
83
|
|
@@ -78,7 +118,7 @@ describe Response do
|
|
78
118
|
|
79
119
|
|
80
120
|
it "should read status" do
|
81
|
-
@response.status.should == 202
|
121
|
+
@response.status.should == [202, "OK"]
|
82
122
|
end
|
83
123
|
|
84
124
|
it "should read headers" do
|
@@ -107,7 +147,7 @@ describe Response do
|
|
107
147
|
end
|
108
148
|
|
109
149
|
it "should read status" do
|
110
|
-
@response.status.should == 202
|
150
|
+
@response.status.should == [202, "OK"]
|
111
151
|
end
|
112
152
|
|
113
153
|
it "should read headers" do
|
@@ -131,6 +171,59 @@ describe Response do
|
|
131
171
|
|
132
172
|
end
|
133
173
|
|
174
|
+
describe "with dynamically evaluated options" do
|
175
|
+
|
176
|
+
before(:each) do
|
177
|
+
@request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should have evaluated body" do
|
181
|
+
@response = Response.new(:body => lambda {|request| request.body})
|
182
|
+
@response.evaluate!(@request_signature).body.should == "abc"
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should have evaluated headers" do
|
186
|
+
@response = Response.new(:headers => lambda {|request| request.headers})
|
187
|
+
@response.evaluate!(@request_signature).headers.should == {'A' => 'a'}
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should have evaluated status" do
|
191
|
+
@response = Response.new(:status => lambda {|request| 302})
|
192
|
+
@response.evaluate!(@request_signature).status.should == [302, ""]
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
describe DynamicResponse do
|
200
|
+
|
201
|
+
describe "evaluating response options" do
|
202
|
+
|
203
|
+
it "should have evaluated options" do
|
204
|
+
request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
|
205
|
+
response = DynamicResponse.new(lambda {|request|
|
206
|
+
{
|
207
|
+
:body => request.body,
|
208
|
+
:headers => request.headers,
|
209
|
+
:status => 302
|
210
|
+
}
|
211
|
+
})
|
212
|
+
response.evaluate!(request_signature)
|
213
|
+
response.body.should == "abc"
|
214
|
+
response.headers.should == {'A' => 'a'}
|
215
|
+
response.status.should == [302, ""]
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should be equal to static response after evaluation" do
|
219
|
+
request_signature = RequestSignature.new(:post, "www.example.com", :body => "abc")
|
220
|
+
response = DynamicResponse.new(lambda {|request| {:body => request.body}})
|
221
|
+
response.evaluate!(request_signature)
|
222
|
+
response.should == Response.new(:body => "abc")
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
134
227
|
end
|
135
228
|
|
136
229
|
end
|