webmock 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ Features
10
10
  * Matching requests based on method, url, headers and body
11
11
  * Support for Test::Unit and RSpec (and can be easily extended to other frameworks)
12
12
  * Support for Net::Http and other http libraries based on Net::Http
13
- * Architecture allows adding other http library adapters easily
13
+ * Adding other http library adapters is easy
14
14
 
15
15
 
16
16
  Installation
@@ -32,7 +32,7 @@ Now you are ready to write your tests/specs with stubbed HTTP calls.
32
32
 
33
33
  stub_request(:any, "www.google.com")
34
34
 
35
- Net::HTTP.get('www.google.com', '/')
35
+ Net::HTTP.get('www.google.com', '/') # ===> Success
36
36
 
37
37
  ### Stubbing requests based on method, url, body and headers
38
38
 
@@ -43,49 +43,68 @@ Now you are ready to write your tests/specs with stubbed HTTP calls.
43
43
  req['Content-Length'] = 3
44
44
  res = Net::HTTP.start(url.host, url.port) {|http|
45
45
  http.request(req, 'abc')
46
- }
46
+ } # ===> Success
47
+
48
+ ### Matching custom request headers
49
+
50
+ stub_request(:any, "www.google.com").with(:headers=>{'Header-Name'=>"Header-Value"}).to_return(:body => "abc", :status => 200)
51
+
52
+ url = URI.parse('http://www.google.com/')
53
+ req = Net::HTTP::Post.new(url.path)
54
+ req['Header-Name'] = "Header-Value"
55
+ res = Net::HTTP.start(url.host, url.port) {|http|
56
+ http.request(req, 'abc')
57
+ } # ===> Success
47
58
 
48
59
  ### Custom response
49
60
 
50
61
  stub_request(:any, "www.google.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 })
51
62
 
52
- Net::HTTP.get('www.google.com', '/').body ===> "abc"
63
+ Net::HTTP.get('www.google.com', '/') # ===> "abc"
64
+
65
+ ### Custom response with body as file path
66
+
67
+ File.open('/tmp/response_body.txt', 'w') { |f| f.puts 'abc' }
68
+
69
+ stub_request(:any, "www.google.com").to_return(:body => "/tmp/response_body.txt", :status => 200)
70
+
71
+ Net::HTTP.get('www.google.com', '/') # ===> "abc\n"
53
72
 
54
73
  ### Request with basic authentication
55
74
 
56
75
  stub_request(:any, "john:smith@www.google.com")
57
76
 
58
- Net::HTTP.get(URI.parse('http://john:smith@www.google.com'))
77
+ Net::HTTP.get(URI.parse('http://john:smith@www.google.com')) # ===> Success
59
78
 
60
79
  ### Matching urls using regular expressions
61
80
 
62
81
  stub_request(:any, /.*google.*/)
63
82
 
64
- Net::HTTP.get('www.google.com', '/')
83
+ Net::HTTP.get('www.google.com', '/') # ===> Success
65
84
 
66
- ### Real requests to network can be allowed or disables
85
+ ### Real requests to network can be allowed or disabled
67
86
 
68
87
  WebMock.allow_net_connect!
69
88
 
70
89
  stub_request(:any, "www.google.com").to_return(:body => "abc")
71
90
 
72
- Net::HTTP.get('www.google.com', '/').should == "abc"
91
+ Net::HTTP.get('www.google.com', '/') # ===> "abc"
73
92
 
74
- Net::HTTP.get('www.something.com', '/').should =~ "Something."
93
+ Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
75
94
 
76
95
  WebMock.disable_net_connect!
77
96
 
78
- Net::HTTP.get('www.something.com', '/') ===> Failure
97
+ Net::HTTP.get('www.something.com', '/') # ===> Failure
79
98
 
80
99
  ### Clearing stubs
81
100
 
82
101
  stub_request(:any, "www.google.com")
83
102
 
84
- Net::HTTP.get('www.google.com', '/') ===> Success
103
+ Net::HTTP.get('www.google.com', '/') # ===> Success
85
104
 
86
105
  reset_webmock
87
106
 
88
- Net::HTTP.get('www.google.com', '/') ===> Failure
107
+ Net::HTTP.get('www.google.com', '/') # ===> Failure
89
108
 
90
109
 
91
110
  ### Test/Unit style assertions (they actually work everywhere, in RSpec too)
@@ -99,9 +118,17 @@ Now you are ready to write your tests/specs with stubbed HTTP calls.
99
118
  http.request(req, 'abc')
100
119
  }
101
120
 
102
- assert_requested :post, "http://www.google.com", :headers => {'Content-Length' => 3}, :body => "abc"
121
+ assert_requested :post, "http://www.google.com", :headers => {'Content-Length' => 3}, :body => "abc", :times => 1 # ===> Success
122
+
123
+ assert_not_requested :get, "http://www.something.com" # ===> Success
124
+
125
+ ### Expecting real (not stubbed) requests
126
+
127
+ WebMock.allow_net_connect!
103
128
 
104
- assert_not_requested :get, "http://www.something.com"
129
+ Net::HTTP.get('www.google.com', '/') # ===> Success
130
+
131
+ assert_requested :get, "http://www.google.com" # ===> Success
105
132
 
106
133
  ### RSpec matchers 1
107
134
 
@@ -109,13 +136,13 @@ Now you are ready to write your tests/specs with stubbed HTTP calls.
109
136
 
110
137
  request(:post, "www.something.com").should have_been_made.times(3)
111
138
 
112
- request(:any, "www.example.com").should have_not_been_made
139
+ request(:any, "www.example.com").should_not have_been_made
113
140
 
114
141
  ### RSpec matchers 2 ([fakeweb-matcher](http://github.com/freelancing-god/fakeweb-matcher) style)
115
142
 
116
143
  WebMock.should have_requested(:get, "www.google.com").with(:body => "abc", :headers => {'Content-Length' => 3}).twice
117
144
 
118
- WebMock.should have_not_requested(:get, "www.something.com")
145
+ WebMock.should_not have_requested(:get, "www.something.com")
119
146
 
120
147
  Notes
121
148
  -----
@@ -137,7 +164,7 @@ i.e
137
164
  stub_request(:get, "www.google.com").to_return(:body => "abc")
138
165
  stub_request(:get, "www.google.com").to_return(:body => "def")
139
166
 
140
- Net::HTTP.get('www.google.com', '/').body ====> "def"
167
+ Net::HTTP.get('www.google.com', '/') # ====> "def"
141
168
 
142
169
  Bugs and Issues
143
170
  ---------------
@@ -147,7 +174,7 @@ Please submit them here [http://github.com/bblimke/webmock/issues](http://github
147
174
  Suggestions
148
175
  ------------
149
176
 
150
- If you have any suggestions on how to improve WebMock please send an email to the mailing list [groups.google.com/group/fakeweb-users](http://groups.google.com/group/fakeweb-users)
177
+ If you have any suggestions on how to improve WebMock please send an email to the mailing list [groups.google.com/group/webmock-users](http://groups.google.com/group/webmock-users)
151
178
 
152
179
  I'm particularly interested in how the DSL could be improved.
153
180
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.7.1
@@ -1,37 +1,42 @@
1
1
  module WebMock
2
2
  class RequestProfileMatcher
3
-
3
+
4
4
  def initialize
5
5
  @request_execution_verifier = RequestExecutionVerifier.new
6
6
  end
7
-
7
+
8
8
  def once
9
9
  @request_execution_verifier.expected_times_executed = 1
10
10
  self
11
11
  end
12
-
12
+
13
13
  def twice
14
14
  @request_execution_verifier.expected_times_executed = 2
15
15
  self
16
16
  end
17
-
17
+
18
18
  def times(times)
19
19
  @request_execution_verifier.expected_times_executed = times.to_i
20
20
  self
21
21
  end
22
-
22
+
23
23
  def matches?(request_profile)
24
24
  @request_execution_verifier.request_profile = request_profile
25
- @request_execution_verifier.verify
25
+ @request_execution_verifier.matches?
26
26
  end
27
-
27
+
28
+ def does_not_match?(request_profile)
29
+ @request_execution_verifier.request_profile = request_profile
30
+ @request_execution_verifier.does_not_match?
31
+ end
32
+
28
33
  def failure_message
29
34
  @request_execution_verifier.failure_message
30
35
  end
31
-
36
+
32
37
 
33
38
  def negative_failure_message
34
39
  @request_execution_verifier.negative_failure_message
35
40
  end
36
41
  end
37
- end
42
+ end
@@ -30,7 +30,11 @@ module WebMock
30
30
  end
31
31
 
32
32
  def matches?(webmock)
33
- @request_execution_verifier.verify
33
+ @request_execution_verifier.matches?
34
+ end
35
+
36
+ def does_not_match?(webmock)
37
+ @request_execution_verifier.does_not_match?
34
38
  end
35
39
 
36
40
  def failure_message
@@ -1,22 +1,42 @@
1
1
  module WebMock
2
2
  class RequestExecutionVerifier
3
-
3
+
4
4
  attr_accessor :request_profile, :expected_times_executed, :times_executed
5
5
 
6
6
  def initialize(request_profile = nil, expected_times_executed = nil)
7
7
  @request_profile = request_profile
8
- @expected_times_executed = expected_times_executed || 1
8
+ @expected_times_executed = expected_times_executed
9
+ end
10
+
11
+ def matches?
12
+ @times_executed =
13
+ RequestRegistry.instance.times_executed(@request_profile)
14
+ @times_executed == (@expected_times_executed || 1)
9
15
  end
10
16
 
11
- def verify
12
- @times_executed =
17
+ def does_not_match?
18
+ @times_executed =
13
19
  RequestRegistry.instance.times_executed(@request_profile)
14
- @times_executed == @expected_times_executed
20
+ if @expected_times_executed
21
+ @times_executed != @expected_times_executed
22
+ else
23
+ @times_executed == 0
24
+ end
15
25
  end
16
26
 
27
+
17
28
  def failure_message
29
+ expected_times_executed = @expected_times_executed || 1
18
30
  %Q(The request #{request_profile.to_s} was expected to execute #{expected_times_executed} time#{ (expected_times_executed == 1) ? '' : 's'} but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
19
31
  end
20
32
 
33
+ def negative_failure_message
34
+ if @expected_times_executed
35
+ %Q(The request #{request_profile.to_s} was not expected to execute #{expected_times_executed} time#{ (expected_times_executed == 1) ? '' : 's'} but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
36
+ else
37
+ %Q(The request #{request_profile.to_s} was expected to execute 0 times but it executed #{times_executed} time#{ (times_executed == 1) ? '' : 's'})
38
+ end
39
+ end
40
+
21
41
  end
22
42
  end
@@ -18,11 +18,13 @@ module WebMock
18
18
  expected_times_executed = options.delete(:times) || 1
19
19
  request = RequestProfile.new(method, url, options[:body], options[:headers])
20
20
  verifier = RequestExecutionVerifier.new(request, expected_times_executed)
21
- assertion_failure(verifier.failure_message) unless verifier.verify
21
+ assertion_failure(verifier.failure_message) unless verifier.matches?
22
22
  end
23
23
 
24
24
  def assert_not_requested(method, url, options = {})
25
- assert_requested(method, url, options.update(:times => 0))
25
+ request = RequestProfile.new(method, url, options[:body], options[:headers])
26
+ verifier = RequestExecutionVerifier.new(request, options.delete(:times))
27
+ assertion_failure(verifier.negative_failure_message) unless verifier.does_not_match?
26
28
  end
27
29
 
28
30
  def self.allow_net_connect!
@@ -23,29 +23,68 @@ describe RequestExecutionVerifier do
23
23
  end
24
24
 
25
25
  end
26
+
27
+ describe "negative failure message" do
26
28
 
27
- describe "verify" do
29
+ it "should report failure message if it executed number of times specified" do
30
+ @verifier.times_executed = 2
31
+ @verifier.expected_times_executed = 2
32
+ @verifier.negative_failure_message.should == "The request www.google.com was not expected to execute 2 times but it executed 2 times"
33
+ end
34
+
35
+ it "should report failure message when not expected request but it executed" do
36
+ @verifier.times_executed = 1
37
+ @verifier.negative_failure_message.should == "The request www.google.com was expected to execute 0 times but it executed 1 time"
38
+ end
39
+
40
+ end
41
+
42
+ describe "matches?" do
28
43
 
29
44
  it "should succeed if request was executed expected number of times" do
30
45
  RequestRegistry.instance.
31
46
  should_receive(:times_executed).with(@request_profile).and_return(10)
32
47
  @verifier.expected_times_executed = 10
33
- @verifier.verify.should be_true
48
+ @verifier.matches?.should be_true
34
49
  end
35
50
 
36
51
  it "should fail if request was not executed expected number of times" do
37
52
  RequestRegistry.instance.
38
53
  should_receive(:times_executed).with(@request_profile).and_return(10)
39
54
  @verifier.expected_times_executed = 5
40
- @verifier.verify.should be_false
55
+ @verifier.matches?.should be_false
41
56
  end
42
57
 
43
58
  end
59
+
60
+ describe "does_not_match?" do
61
+
62
+ it "should fail if request executed expected number of times" do
63
+ RequestRegistry.instance.
64
+ should_receive(:times_executed).with(@request_profile).and_return(10)
65
+ @verifier.expected_times_executed = 10
66
+ @verifier.does_not_match?.should be_false
67
+ end
68
+
69
+ it "should succeed if request was not executed at all and expected number of times was not set" do
70
+ RequestRegistry.instance.
71
+ should_receive(:times_executed).with(@request_profile).and_return(0)
72
+ @verifier.does_not_match?.should be_true
73
+ end
74
+
75
+ it "should fail if request was executed and expected number of times was not set" do
76
+ RequestRegistry.instance.
77
+ should_receive(:times_executed).with(@request_profile).and_return(1)
78
+ @verifier.does_not_match?.should be_false
79
+ end
80
+
81
+ it "should succeed if request was not executed expected number of times" do
82
+ RequestRegistry.instance.
83
+ should_receive(:times_executed).with(@request_profile).and_return(10)
84
+ @verifier.expected_times_executed = 5
85
+ @verifier.does_not_match?.should be_true
86
+ end
44
87
 
45
- def verify
46
- @times_executed =
47
- RequestRegistry.instance.times_executed(@request_profile)
48
- @times_executed == @expected_times_executed
49
88
  end
50
89
 
51
90
  end
@@ -55,5 +55,5 @@ describe Response do
55
55
  end
56
56
 
57
57
  end
58
-
58
+
59
59
  end
@@ -230,14 +230,14 @@ describe "WebMock", :shared => true do
230
230
 
231
231
  it "should pass if request was not expected and not executed" do
232
232
  lambda {
233
- request(:get, "http://www.google.com").should have_not_been_made
233
+ request(:get, "http://www.google.com").should_not have_been_made
234
234
  }.should_not raise_error
235
235
  end
236
236
 
237
237
  it "should fail if request was not expected but executed" do
238
238
  lambda {
239
239
  http_request(:get, "http://www.google.com/")
240
- request(:get, "http://www.google.com").should have_not_been_made
240
+ request(:get, "http://www.google.com").should_not have_been_made
241
241
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
242
242
  end
243
243
 
@@ -434,10 +434,12 @@ describe "WebMock", :shared => true do
434
434
  it "should verify that non expected requests didn't occur" do
435
435
  lambda {
436
436
  http_request(:get, "http://www.google.com/")
437
- WebMock.should have_not_requested(:get, "http://www.google.com")
437
+ WebMock.should_not have_requested(:get, "http://www.google.com")
438
438
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
439
439
  end
440
440
  end
441
+
442
+
441
443
 
442
444
  describe "using assert_requested" do
443
445
 
@@ -482,7 +484,7 @@ describe "WebMock", :shared => true do
482
484
  it "should verify that non expected requests didn't occur" do
483
485
  lambda {
484
486
  http_request(:get, "http://www.google.com/")
485
- request(:get, "http://www.google.com").should have_not_been_made
487
+ request(:get, "http://www.google.com").should_not have_been_made
486
488
  }.should fail_with("The request GET http://www.google.com/ was expected to execute 0 times but it executed 1 time")
487
489
  end
488
490
  end
@@ -0,0 +1,124 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{webmock}
8
+ s.version = "0.7.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bartosz Blimke"]
12
+ s.date = %q{2009-11-20}
13
+ s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
14
+ s.email = %q{bartosz.blimke@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.md",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/webmock.rb",
26
+ "lib/webmock/adapters/rspec.rb",
27
+ "lib/webmock/adapters/rspec/matchers.rb",
28
+ "lib/webmock/adapters/rspec/request_profile_matcher.rb",
29
+ "lib/webmock/adapters/rspec/webmock_matcher.rb",
30
+ "lib/webmock/adapters/test_unit.rb",
31
+ "lib/webmock/config.rb",
32
+ "lib/webmock/errors.rb",
33
+ "lib/webmock/http_lib_adapters/net_http.rb",
34
+ "lib/webmock/request_execution_verifier.rb",
35
+ "lib/webmock/request_profile.rb",
36
+ "lib/webmock/request_registry.rb",
37
+ "lib/webmock/request_stub.rb",
38
+ "lib/webmock/response.rb",
39
+ "lib/webmock/url.rb",
40
+ "lib/webmock/util/hash_counter.rb",
41
+ "lib/webmock/utility.rb",
42
+ "lib/webmock/webmock.rb",
43
+ "spec/net_http_spec.rb",
44
+ "spec/other_net_http_libs_spec.rb",
45
+ "spec/request_execution_verifier_spec.rb",
46
+ "spec/request_profile_spec.rb",
47
+ "spec/request_registry_spec.rb",
48
+ "spec/request_stub_spec.rb",
49
+ "spec/response_spec.rb",
50
+ "spec/spec.opts",
51
+ "spec/spec_helper.rb",
52
+ "spec/util/hash_counter_spec.rb",
53
+ "spec/utility_spec.rb",
54
+ "spec/vendor/right_http_connection-1.2.4/History.txt",
55
+ "spec/vendor/right_http_connection-1.2.4/Manifest.txt",
56
+ "spec/vendor/right_http_connection-1.2.4/README.txt",
57
+ "spec/vendor/right_http_connection-1.2.4/Rakefile",
58
+ "spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
59
+ "spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
60
+ "spec/vendor/right_http_connection-1.2.4/setup.rb",
61
+ "spec/vendor/samuel-0.2.1/.document",
62
+ "spec/vendor/samuel-0.2.1/.gitignore",
63
+ "spec/vendor/samuel-0.2.1/LICENSE",
64
+ "spec/vendor/samuel-0.2.1/README.rdoc",
65
+ "spec/vendor/samuel-0.2.1/Rakefile",
66
+ "spec/vendor/samuel-0.2.1/VERSION",
67
+ "spec/vendor/samuel-0.2.1/lib/samuel.rb",
68
+ "spec/vendor/samuel-0.2.1/lib/samuel/net_http.rb",
69
+ "spec/vendor/samuel-0.2.1/lib/samuel/request.rb",
70
+ "spec/vendor/samuel-0.2.1/samuel.gemspec",
71
+ "spec/vendor/samuel-0.2.1/test/request_test.rb",
72
+ "spec/vendor/samuel-0.2.1/test/samuel_test.rb",
73
+ "spec/vendor/samuel-0.2.1/test/test_helper.rb",
74
+ "spec/vendor/samuel-0.2.1/test/thread_test.rb",
75
+ "spec/webmock_spec.rb",
76
+ "test/test_helper.rb",
77
+ "test/test_webmock.rb",
78
+ "webmock.gemspec"
79
+ ]
80
+ s.homepage = %q{http://github.com/bblimke/webmock}
81
+ s.rdoc_options = ["--charset=UTF-8"]
82
+ s.require_paths = ["lib"]
83
+ s.rubygems_version = %q{1.3.5}
84
+ s.summary = %q{Library for stubbing HTTP requests in Ruby.}
85
+ s.test_files = [
86
+ "spec/net_http_spec.rb",
87
+ "spec/other_net_http_libs_spec.rb",
88
+ "spec/request_execution_verifier_spec.rb",
89
+ "spec/request_profile_spec.rb",
90
+ "spec/request_registry_spec.rb",
91
+ "spec/request_stub_spec.rb",
92
+ "spec/response_spec.rb",
93
+ "spec/spec_helper.rb",
94
+ "spec/util/hash_counter_spec.rb",
95
+ "spec/utility_spec.rb",
96
+ "spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
97
+ "spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
98
+ "spec/vendor/right_http_connection-1.2.4/setup.rb",
99
+ "spec/vendor/samuel-0.2.1/lib/samuel/net_http.rb",
100
+ "spec/vendor/samuel-0.2.1/lib/samuel/request.rb",
101
+ "spec/vendor/samuel-0.2.1/lib/samuel.rb",
102
+ "spec/vendor/samuel-0.2.1/test/request_test.rb",
103
+ "spec/vendor/samuel-0.2.1/test/samuel_test.rb",
104
+ "spec/vendor/samuel-0.2.1/test/test_helper.rb",
105
+ "spec/vendor/samuel-0.2.1/test/thread_test.rb",
106
+ "spec/webmock_spec.rb",
107
+ "test/test_helper.rb",
108
+ "test/test_webmock.rb"
109
+ ]
110
+
111
+ if s.respond_to? :specification_version then
112
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
113
+ s.specification_version = 3
114
+
115
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
116
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
117
+ else
118
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
119
+ end
120
+ else
121
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
122
+ end
123
+ end
124
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -90,6 +90,7 @@ files:
90
90
  - spec/webmock_spec.rb
91
91
  - test/test_helper.rb
92
92
  - test/test_webmock.rb
93
+ - webmock.gemspec
93
94
  has_rdoc: true
94
95
  homepage: http://github.com/bblimke/webmock
95
96
  licenses: []