webmock 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.google.kom")
9
- profile = RequestProfile.new(:get, "www.google.com")
10
- profile.uri.should == "www.google.kom"
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.google.com")
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.google.com", nil, 'A' => 'a').headers.should == {'B' => 'b'}
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.google.com", "abc").body.should == "abc"
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.google.com", "abc", {'A' => 'a', 'B' => 'b'}).to_s.should ==
33
- "GET http://www.google.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
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.google.com")
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.google.com")
8
- @request_signature = RequestSignature.new(:get, "www.google.com")
9
- @request_stub = RequestStub.new(:get, "www.google.com")
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.google.com")
63
+ @request_stub1 = RequestStub.new(:get, "www.example.com")
64
64
  @request_stub1.response = @response1 = Response.new
65
- @request_stub2 = RequestStub.new(:get, "www.google.com")
65
+ @request_stub2 = RequestStub.new(:get, "www.example.com")
66
66
  @request_stub2.response = @response2 = Response.new
67
- @request_stub3 = RequestStub.new(:get, "www.google.org")
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.google.com")
87
- @request_stub2 = RequestStub.new(:get, "www.google.net")
88
- @request_stub3 = RequestStub.new(:get, "www.google.org")
89
- RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.google.com"))
90
- RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.google.com"))
91
- RequestRegistry.instance.requested_signatures.put(RequestSignature.new(:get, "www.google.org"))
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.google.net")).should == 0
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.google.com")).should == 2
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, /.*google.*/)).should == 3
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.google.com").
9
- should match(RequestProfile.new(:get, "www.google.com"))
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.google.com").
14
- should match(RequestProfile.new(:any, "www.google.com"))
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.google.com").
19
- should_not match(RequestProfile.new(:post, "www.google.com"))
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.google.com").
24
- should match(RequestProfile.new(:get, "www.google.com"))
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.google.com/big image.jpg").
29
- should match(RequestProfile.new(:get, "www.google.com/big%20image.jpg"))
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.google.com/big%20image.jpg").
34
- should match(RequestProfile.new(:get, "www.google.com/big image.jpg"))
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.google.com/big%20image.jpg").
39
- should match(RequestProfile.new(:get, /.*big image.jpg.*/))
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.google.com").
44
- should match(RequestProfile.new(:get, /.*google.*/))
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.google.com?a=1&b=2").
49
- should match(RequestProfile.new(:get, "www.google.com?a=1&b=2"))
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.google.com?a=2&b=1").
54
- should_not match(RequestProfile.new(:get, "www.google.com?a=1&b=2"))
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.google.com?a=1&b=2").
59
- should match(RequestProfile.new(:get, "www.google.com?b=2&a=1"))
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.google.com/?a=a b").
66
- should match(RequestProfile.new(:get, "www.google.com/?a=a%20b"))
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.google.com/?a=a%20b").
71
- should match(RequestProfile.new(:get, "www.google.com/?a=a b"))
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.google.com/?a=a%20b").
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.google.com/?a=a b").
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.google.com", "abc").
90
- should match(RequestProfile.new(:get, "www.google.com", "abc"))
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.google.com", "abc").
95
- should_not match(RequestProfile.new(:get, "www.google.com", "def"))
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 is other has nil body" do
99
- RequestSignature.new(:get, "www.google.com", "abc").
100
- should match(RequestProfile.new(:get, "www.google.com", nil))
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.google.com", "abc").
105
- should_not match(RequestProfile.new(:get, "www.google.com", ""))
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.google.com", nil, 'Content-Type' => 'image/jpeg').
110
- should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
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.google.com", nil, 'Content-Type' => 'image/jpeg').
115
- should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/png'))
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.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888').
120
- should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
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.google.com", nil, 'Content-Type' => 'image/jpeg').
125
- should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'))
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.google.com", nil, :ContentLength => 8888, 'content_type' => 'image/png').
130
- should match(RequestProfile.new(:get, "www.google.com", nil, 'ContentLength' => '8888', 'Content-type' => 'image/png'))
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.google.com", nil, 'A' => 'a').
135
- should match(RequestProfile.new(:get, "www.google.com", nil, nil))
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.google.com", nil, 'A' => 'a').
140
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {}))
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.google.com", nil, nil).
145
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
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.google.com", nil, {}).
150
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
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
@@ -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.google.com")
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.google.com"
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
@@ -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 content of a file as body if provided" do
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
- # Sets several expectations that a real HTTP request makes it
26
- # past WebMock to the socket layer. You can use this when you need to check
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>" }