webmock 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +15 -0
- data/README.md +80 -68
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/webmock.rb +2 -0
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +1 -4
- data/lib/webmock/http_lib_adapters/httpclient.rb +96 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +4 -7
- data/lib/webmock/request.rb +29 -0
- data/lib/webmock/request_profile.rb +28 -15
- data/lib/webmock/request_registry.rb +12 -2
- data/lib/webmock/request_signature.rb +2 -1
- data/lib/webmock/request_stub.rb +1 -2
- data/lib/webmock/response.rb +18 -6
- data/lib/webmock/util/uri.rb +2 -2
- data/lib/webmock/webmock.rb +2 -2
- data/spec/httpclient_spec.rb +36 -0
- data/spec/httpclient_spec_helper.rb +57 -0
- data/spec/net_http_spec.rb +15 -24
- data/spec/net_http_spec_helper.rb +53 -0
- data/spec/request_execution_verifier_spec.rb +5 -5
- data/spec/request_profile_spec.rb +17 -10
- data/spec/request_registry_spec.rb +15 -15
- data/spec/request_signature_spec.rb +71 -56
- data/spec/request_stub_spec.rb +3 -3
- data/spec/response_spec.rb +7 -1
- data/spec/spec_helper.rb +5 -33
- data/spec/util/uri_spec.rb +67 -40
- data/spec/webmock_spec.rb +179 -164
- data/test/test_webmock.rb +25 -19
- data/webmock.gemspec +14 -3
- metadata +20 -2
@@ -5,43 +5,50 @@ describe RequestProfile do
|
|
5
5
|
describe "initialization" do
|
6
6
|
|
7
7
|
it "should have assigned normalized uri" do
|
8
|
-
WebMock::Util::URI.should_receive(:normalize_uri).and_return("www.
|
9
|
-
profile = RequestProfile.new(:get, "www.
|
10
|
-
profile.uri.should == "www.
|
8
|
+
WebMock::Util::URI.should_receive(:normalize_uri).and_return("www.example.kom")
|
9
|
+
profile = RequestProfile.new(:get, "www.example.com")
|
10
|
+
profile.uri.should == "www.example.kom"
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should have assigned uri without normalization if uri is URI" do
|
14
14
|
WebMock::Util::URI.should_not_receive(:normalize_uri)
|
15
|
-
uri = Addressable::URI.parse("www.
|
15
|
+
uri = Addressable::URI.parse("www.example.com")
|
16
16
|
profile = RequestProfile.new(:get, uri)
|
17
17
|
profile.uri.should == uri
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should have assigned normalized headers" do
|
21
21
|
WebMock::Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
22
|
-
RequestProfile.new(:get, "www.
|
22
|
+
RequestProfile.new(:get, "www.example.com", :headers => {'A' => 'a'}).headers.should == {'B' => 'b'}
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should have assigned body" do
|
26
|
-
RequestProfile.new(:get, "www.
|
26
|
+
RequestProfile.new(:get, "www.example.com", :body => "abc").
|
27
|
+
body.should == RequestProfile::Body.new("abc")
|
27
28
|
end
|
28
29
|
|
29
30
|
end
|
30
31
|
|
31
32
|
it "should report string describing itself" do
|
32
|
-
RequestProfile.new(:get, "www.
|
33
|
-
|
33
|
+
RequestProfile.new(:get, "www.example.com",
|
34
|
+
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
|
35
|
+
"GET http://www.example.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
|
34
36
|
end
|
35
37
|
|
36
38
|
|
37
39
|
describe "with" do
|
38
40
|
before(:each) do
|
39
|
-
@request_profile = RequestProfile.new(:get, "www.
|
41
|
+
@request_profile = RequestProfile.new(:get, "www.example.com")
|
40
42
|
end
|
41
43
|
|
42
44
|
it "should assign body to request profile" do
|
43
45
|
@request_profile.with(:body => "abc")
|
44
|
-
@request_profile.body.should == "abc"
|
46
|
+
@request_profile.body.should == RequestProfile::Body.new("abc")
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should have the same body" do
|
50
|
+
@request_profile.with(:body => "abc")
|
51
|
+
@request_profile.body.should == RequestProfile::Body.new("abc")
|
45
52
|
end
|
46
53
|
|
47
54
|
it "should assign normalized headers to request profile" do
|
@@ -4,9 +4,9 @@ describe RequestRegistry do
|
|
4
4
|
|
5
5
|
before(:each) do
|
6
6
|
RequestRegistry.instance.reset_webmock
|
7
|
-
@request_profile = RequestProfile.new(:get, "www.
|
8
|
-
@request_signature = RequestSignature.new(:get, "www.
|
9
|
-
@request_stub = RequestStub.new(:get, "www.
|
7
|
+
@request_profile = RequestProfile.new(:get, "www.example.com")
|
8
|
+
@request_signature = RequestSignature.new(:get, "www.example.com")
|
9
|
+
@request_stub = RequestStub.new(:get, "www.example.com")
|
10
10
|
end
|
11
11
|
|
12
12
|
describe "reset_webmock" do
|
@@ -60,11 +60,11 @@ describe RequestRegistry do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should always return last registered matching response" do
|
63
|
-
@request_stub1 = RequestStub.new(:get, "www.
|
63
|
+
@request_stub1 = RequestStub.new(:get, "www.example.com")
|
64
64
|
@request_stub1.response = @response1 = Response.new
|
65
|
-
@request_stub2 = RequestStub.new(:get, "www.
|
65
|
+
@request_stub2 = RequestStub.new(:get, "www.example.com")
|
66
66
|
@request_stub2.response = @response2 = Response.new
|
67
|
-
@request_stub3 = RequestStub.new(:get, "www.
|
67
|
+
@request_stub3 = RequestStub.new(:get, "www.example.org")
|
68
68
|
@request_stub3.response = @response3 = Response.new
|
69
69
|
RequestRegistry.instance.register_request_stub(@request_stub1)
|
70
70
|
RequestRegistry.instance.register_request_stub(@request_stub2)
|
@@ -83,24 +83,24 @@ describe RequestRegistry do
|
|
83
83
|
end
|
84
84
|
|
85
85
|
before(:each) do
|
86
|
-
@request_stub1 = RequestStub.new(:get, "www.
|
87
|
-
@request_stub2 = RequestStub.new(:get, "www.
|
88
|
-
@request_stub3 = RequestStub.new(:get, "www.
|
89
|
-
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.
|
90
|
-
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.
|
91
|
-
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.
|
86
|
+
@request_stub1 = RequestStub.new(:get, "www.example.com")
|
87
|
+
@request_stub2 = RequestStub.new(:get, "www.example.net")
|
88
|
+
@request_stub3 = RequestStub.new(:get, "www.example.org")
|
89
|
+
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.example.com"))
|
90
|
+
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.example.com"))
|
91
|
+
RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.example.org"))
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should report 0 if no request matching profile was requested" do
|
95
|
-
RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.
|
95
|
+
RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.example.net")).should == 0
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should report number of times matching profile was requested" do
|
99
|
-
RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.
|
99
|
+
RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.example.com")).should == 2
|
100
100
|
end
|
101
101
|
|
102
102
|
it "should report number of times all matching profile were requested" do
|
103
|
-
RequestRegistry.instance.times_executed(RequestProfile.new(:get, /.*
|
103
|
+
RequestRegistry.instance.times_executed(RequestProfile.new(:get, /.*example.*/)).should == 3
|
104
104
|
end
|
105
105
|
|
106
106
|
|
@@ -5,79 +5,79 @@ describe RequestSignature do
|
|
5
5
|
describe "when matching" do
|
6
6
|
|
7
7
|
it "should match if uri matches and method matches" do
|
8
|
-
RequestSignature.new(:get, "www.
|
9
|
-
should match(RequestProfile.new(:get, "www.
|
8
|
+
RequestSignature.new(:get, "www.example.com").
|
9
|
+
should match(RequestProfile.new(:get, "www.example.com"))
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should match if uri matches and method is any" do
|
13
|
-
RequestSignature.new(:get, "www.
|
14
|
-
should match(RequestProfile.new(:any, "www.
|
13
|
+
RequestSignature.new(:get, "www.example.com").
|
14
|
+
should match(RequestProfile.new(:any, "www.example.com"))
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should not match if other request profile has different method" do
|
18
|
-
RequestSignature.new(:get, "www.
|
19
|
-
should_not match(RequestProfile.new(:post, "www.
|
18
|
+
RequestSignature.new(:get, "www.example.com").
|
19
|
+
should_not match(RequestProfile.new(:post, "www.example.com"))
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should match if uri matches other uri" do
|
23
|
-
RequestSignature.new(:get, "www.
|
24
|
-
should match(RequestProfile.new(:get, "www.
|
23
|
+
RequestSignature.new(:get, "www.example.com").
|
24
|
+
should match(RequestProfile.new(:get, "www.example.com"))
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should match if uri matches other escaped using uri" do
|
28
|
-
RequestSignature.new(:get, "www.
|
29
|
-
should match(RequestProfile.new(:get, "www.
|
28
|
+
RequestSignature.new(:get, "www.example.com/my path").
|
29
|
+
should match(RequestProfile.new(:get, "www.example.com/my%20path"))
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should match if unescaped uri matches other uri" do
|
33
|
-
RequestSignature.new(:get, "www.
|
34
|
-
should match(RequestProfile.new(:get, "www.
|
33
|
+
RequestSignature.new(:get, "www.example.com/my%20path").
|
34
|
+
should match(RequestProfile.new(:get, "www.example.com/my path"))
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should match if unescaped uri matches other regexp uri" do
|
38
|
-
RequestSignature.new(:get, "www.
|
39
|
-
should match(RequestProfile.new(:get, /.*
|
38
|
+
RequestSignature.new(:get, "www.example.com/my%20path").
|
39
|
+
should match(RequestProfile.new(:get, /.*my path.*/))
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should match if uri matches other regex uri" do
|
43
|
-
RequestSignature.new(:get, "www.
|
44
|
-
should match(RequestProfile.new(:get, /.*
|
43
|
+
RequestSignature.new(:get, "www.example.com").
|
44
|
+
should match(RequestProfile.new(:get, /.*example.*/))
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should match for uris with same parameters" do
|
48
|
-
RequestSignature.new(:get, "www.
|
49
|
-
should match(RequestProfile.new(:get, "www.
|
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
50
|
end
|
51
51
|
|
52
52
|
it "should not match for uris with different parameters" do
|
53
|
-
RequestSignature.new(:get, "www.
|
54
|
-
should_not match(RequestProfile.new(:get, "www.
|
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
55
|
end
|
56
56
|
|
57
57
|
it "should match for parameters in different order" do
|
58
|
-
RequestSignature.new(:get, "www.
|
59
|
-
should match(RequestProfile.new(:get, "www.
|
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
60
|
end
|
61
61
|
|
62
62
|
describe "when parameters are escaped" do
|
63
63
|
|
64
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.
|
66
|
-
should match(RequestProfile.new(:get, "www.
|
65
|
+
RequestSignature.new(:get, "www.example.com/?a=a b").
|
66
|
+
should match(RequestProfile.new(:get, "www.example.com/?a=a%20b"))
|
67
67
|
end
|
68
68
|
|
69
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.
|
71
|
-
should match(RequestProfile.new(:get, "www.
|
70
|
+
RequestSignature.new(:get, "www.example.com/?a=a%20b").
|
71
|
+
should match(RequestProfile.new(:get, "www.example.com/?a=a b"))
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should match if other regexp is for non escaped parameters but uri has escaped parameters" do
|
75
|
-
RequestSignature.new(:get, "www.
|
75
|
+
RequestSignature.new(:get, "www.example.com/?a=a%20b").
|
76
76
|
should match(RequestProfile.new(:get, /.*a=a b.*/))
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should match if other regexp is for escaped parameters but uri has non escaped parameters" do
|
80
|
-
RequestSignature.new(:get, "www.
|
80
|
+
RequestSignature.new(:get, "www.example.com/?a=a b").
|
81
81
|
should match(RequestProfile.new(:get, /.*a=a%20b.*/))
|
82
82
|
end
|
83
83
|
|
@@ -86,68 +86,83 @@ describe RequestSignature do
|
|
86
86
|
|
87
87
|
|
88
88
|
it "should match for same bodies" do
|
89
|
-
RequestSignature.new(:get, "www.
|
90
|
-
should match(RequestProfile.new(:get, "www.
|
89
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
90
|
+
should match(RequestProfile.new(:get, "www.example.com", :body => "abc"))
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should not match for different bodies" do
|
94
|
-
RequestSignature.new(:get, "www.
|
95
|
-
should_not match(RequestProfile.new(:get, "www.
|
94
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
95
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :body => "def"))
|
96
96
|
end
|
97
97
|
|
98
|
-
it "should match
|
99
|
-
RequestSignature.new(:get, "www.
|
100
|
-
should match(RequestProfile.new(:get, "www.
|
98
|
+
it "should match if other has not specified body" do
|
99
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
100
|
+
should match(RequestProfile.new(:get, "www.example.com"))
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should not match if other has nil body" do
|
104
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
105
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :body => nil))
|
101
106
|
end
|
102
107
|
|
103
108
|
it "should not match if other has empty body" do
|
104
|
-
RequestSignature.new(:get, "www.
|
105
|
-
should_not match(RequestProfile.new(:get, "www.
|
109
|
+
RequestSignature.new(:get, "www.example.com", :body => "abc").
|
110
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :body => ""))
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should not match if other has body" do
|
114
|
+
RequestSignature.new(:get, "www.example.com").
|
115
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :body => "abc"))
|
106
116
|
end
|
107
117
|
|
108
118
|
it "should match for same headers" do
|
109
|
-
RequestSignature.new(:get, "www.
|
110
|
-
should match(RequestProfile.new(:get, "www.
|
119
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
120
|
+
should match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
111
121
|
end
|
112
122
|
|
113
123
|
it "should not match for different values of the same header" do
|
114
|
-
RequestSignature.new(:get, "www.
|
115
|
-
should_not match(RequestProfile.new(:get, "www.
|
124
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
125
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}))
|
116
126
|
end
|
117
127
|
|
118
128
|
it "should match if request has more headers than other" do
|
119
|
-
RequestSignature.new(:get, "www.
|
120
|
-
should match(RequestProfile.new(:get, "www.
|
129
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
|
130
|
+
should match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
|
121
131
|
end
|
122
132
|
|
123
133
|
it "should not match if request has less headers that the other and all match" do
|
124
|
-
RequestSignature.new(:get, "www.
|
125
|
-
should_not match(RequestProfile.new(:get, "www.
|
134
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
|
135
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
|
126
136
|
end
|
127
137
|
|
128
138
|
it "should match even is header keys or values are in different format" do
|
129
|
-
RequestSignature.new(:get, "www.
|
130
|
-
should match(RequestProfile.new(:get, "www.
|
139
|
+
RequestSignature.new(:get, "www.example.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}).
|
140
|
+
should match(RequestProfile.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}))
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should match is other has not specified" do
|
144
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
145
|
+
should match(RequestProfile.new(:get, "www.example.com"))
|
131
146
|
end
|
132
147
|
|
133
|
-
it "should match is other has nil headers" do
|
134
|
-
RequestSignature.new(:get, "www.
|
135
|
-
should match(RequestProfile.new(:get, "www.
|
148
|
+
it "should not match is other has nil headers" do
|
149
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
150
|
+
should match(RequestProfile.new(:get, "www.example.com", :headers => nil))
|
136
151
|
end
|
137
152
|
|
138
153
|
it "should not match if other has empty headers" do
|
139
|
-
RequestSignature.new(:get, "www.
|
140
|
-
should_not match(RequestProfile.new(:get, "www.
|
154
|
+
RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}).
|
155
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {}))
|
141
156
|
end
|
142
157
|
|
143
158
|
it "should not match if profile has no headers but other has headers" do
|
144
|
-
RequestSignature.new(:get, "www.
|
145
|
-
should_not match(RequestProfile.new(:get, "www.
|
159
|
+
RequestSignature.new(:get, "www.example.com").
|
160
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'A'=>'a'}))
|
146
161
|
end
|
147
162
|
|
148
163
|
it "should not match if profile has empty headers but other has headers" do
|
149
|
-
RequestSignature.new(:get, "www.
|
150
|
-
should_not match(RequestProfile.new(:get, "www.
|
164
|
+
RequestSignature.new(:get, "www.example.com", :headers => {}).
|
165
|
+
should_not match(RequestProfile.new(:get, "www.example.com", :headers => {'A'=>'a'}))
|
151
166
|
end
|
152
167
|
|
153
168
|
end
|
data/spec/request_stub_spec.rb
CHANGED
@@ -3,12 +3,12 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
describe RequestStub do
|
4
4
|
|
5
5
|
before(:each) do
|
6
|
-
@request_stub = RequestStub.new(:get, "www.
|
6
|
+
@request_stub = RequestStub.new(:get, "www.example.com")
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should have request profile with method and uri" do
|
10
10
|
@request_stub.request_profile.method.should == :get
|
11
|
-
@request_stub.request_profile.uri.host.should == "www.
|
11
|
+
@request_stub.request_profile.uri.host.should == "www.example.com"
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should have response" do
|
@@ -19,7 +19,7 @@ describe RequestStub do
|
|
19
19
|
|
20
20
|
it "should assign body to request profile" do
|
21
21
|
@request_stub.with(:body => "abc")
|
22
|
-
@request_stub.request_profile.body.should == "abc"
|
22
|
+
@request_stub.request_profile.body.should == RequestProfile::Body.new("abc")
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should assign normalized headers to request profile" do
|
data/spec/response_spec.rb
CHANGED
@@ -7,6 +7,7 @@ describe Response do
|
|
7
7
|
|
8
8
|
it "should report normalized headers" do
|
9
9
|
Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
|
10
|
+
@response = Response.new(:headers => {'A' => 'a'})
|
10
11
|
@response.headers.should == {'B' => 'b'}
|
11
12
|
end
|
12
13
|
|
@@ -49,8 +50,13 @@ describe Response do
|
|
49
50
|
@response.body.should == "abc"
|
50
51
|
end
|
51
52
|
|
52
|
-
it "should report
|
53
|
+
it "should report string even if existing file path was provided" do
|
53
54
|
@response = Response.new(:body => __FILE__)
|
55
|
+
@response.body.should == __FILE__
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should report content of a IO object if provided" do
|
59
|
+
@response = Response.new(:body => File.new(__FILE__))
|
54
60
|
@response.body.should == File.new(__FILE__).read
|
55
61
|
end
|
56
62
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'httpclient'
|
1
3
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
4
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
5
|
require 'spec'
|
4
6
|
require 'spec/autorun'
|
5
|
-
require 'rubygems'
|
6
7
|
|
7
8
|
require 'webmock/rspec'
|
8
9
|
|
@@ -13,7 +14,7 @@ def fail()
|
|
13
14
|
end
|
14
15
|
|
15
16
|
def fail_with(message)
|
16
|
-
raise_error(Spec::Expectations::ExpectationNotMetError,message)
|
17
|
+
raise_error(Spec::Expectations::ExpectationNotMetError, message)
|
17
18
|
end
|
18
19
|
|
19
20
|
class Proc
|
@@ -22,37 +23,8 @@ class Proc
|
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
# that a request isn't handled by WebMock
|
28
|
-
#This solution is copied from FakeWeb project
|
29
|
-
def setup_expectations_for_real_request(options = {})
|
30
|
-
# Socket handling
|
31
|
-
if options[:port] == 443
|
32
|
-
socket = mock("SSLSocket")
|
33
|
-
OpenSSL::SSL::SSLSocket.should_receive(:===).with(socket).at_least(:once).and_return(true)
|
34
|
-
OpenSSL::SSL::SSLSocket.should_receive(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).at_least(:once).and_return(socket)
|
35
|
-
socket.stub!(:sync_close=).and_return(true)
|
36
|
-
socket.should_receive(:connect).at_least(:once).with()
|
37
|
-
else
|
38
|
-
socket = mock("TCPSocket")
|
39
|
-
Socket.should_receive(:===).with(socket).at_least(:once).and_return(true)
|
40
|
-
end
|
41
|
-
|
42
|
-
TCPSocket.should_receive(:open).with(options[:host], options[:port]).at_least(:once).and_return(socket)
|
43
|
-
socket.stub!(:closed?).and_return(false)
|
44
|
-
socket.stub!(:close).and_return(true)
|
45
|
-
|
46
|
-
# Request/response handling
|
47
|
-
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
48
|
-
socket.should_receive(:write).with(/#{request_parts[0]}.*#{request_parts[1]}.*/m).and_return(100)
|
49
|
-
|
50
|
-
socket.should_receive(:sysread).once.and_return("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}")
|
51
|
-
socket.should_receive(:sysread).any_number_of_times.and_raise(EOFError)
|
52
|
-
end
|
53
|
-
|
54
|
-
def setup_expectations_for_real_google_request(options = {})
|
55
|
-
defaults = { :host => "www.google.com", :port => 80, :method => "GET",
|
26
|
+
def setup_expectations_for_real_example_com_request(options = {})
|
27
|
+
defaults = { :host => "www.example.com", :port => 80, :method => "GET",
|
56
28
|
:path => "/",
|
57
29
|
:response_code => 200, :response_message => "OK",
|
58
30
|
:response_body => "<title>Google fake response</title>" }
|