webmock 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.gitignore +21 -0
  2. data/LICENSE +281 -0
  3. data/README.md +170 -0
  4. data/Rakefile +54 -0
  5. data/VERSION +1 -0
  6. data/lib/webmock.rb +18 -0
  7. data/lib/webmock/adapters/rspec.rb +23 -0
  8. data/lib/webmock/adapters/rspec/matchers.rb +19 -0
  9. data/lib/webmock/adapters/rspec/request_profile_matcher.rb +37 -0
  10. data/lib/webmock/adapters/rspec/webmock_matcher.rb +45 -0
  11. data/lib/webmock/adapters/test_unit.rb +25 -0
  12. data/lib/webmock/config.rb +7 -0
  13. data/lib/webmock/errors.rb +5 -0
  14. data/lib/webmock/http_lib_adapters/net_http.rb +129 -0
  15. data/lib/webmock/request_execution_verifier.rb +22 -0
  16. data/lib/webmock/request_profile.rb +67 -0
  17. data/lib/webmock/request_registry.rb +47 -0
  18. data/lib/webmock/request_stub.rb +26 -0
  19. data/lib/webmock/response.rb +31 -0
  20. data/lib/webmock/url.rb +46 -0
  21. data/lib/webmock/util/hash_counter.rb +12 -0
  22. data/lib/webmock/utility.rb +65 -0
  23. data/lib/webmock/webmock.rb +58 -0
  24. data/spec/net_http_spec.rb +33 -0
  25. data/spec/other_net_http_libs_spec.rb +37 -0
  26. data/spec/request_execution_verifier_spec.rb +51 -0
  27. data/spec/request_profile_spec.rb +166 -0
  28. data/spec/request_registry_spec.rb +114 -0
  29. data/spec/request_stub_spec.rb +54 -0
  30. data/spec/response_spec.rb +59 -0
  31. data/spec/spec.opts +1 -0
  32. data/spec/spec_helper.rb +58 -0
  33. data/spec/util/hash_counter_spec.rb +24 -0
  34. data/spec/utility_spec.rb +70 -0
  35. data/spec/vendor/right_http_connection-1.2.4/History.txt +59 -0
  36. data/spec/vendor/right_http_connection-1.2.4/Manifest.txt +7 -0
  37. data/spec/vendor/right_http_connection-1.2.4/README.txt +54 -0
  38. data/spec/vendor/right_http_connection-1.2.4/Rakefile +103 -0
  39. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +160 -0
  40. data/spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +435 -0
  41. data/spec/vendor/right_http_connection-1.2.4/setup.rb +1585 -0
  42. data/spec/vendor/samuel-0.2.1/.document +5 -0
  43. data/spec/vendor/samuel-0.2.1/.gitignore +5 -0
  44. data/spec/vendor/samuel-0.2.1/LICENSE +20 -0
  45. data/spec/vendor/samuel-0.2.1/README.rdoc +70 -0
  46. data/spec/vendor/samuel-0.2.1/Rakefile +62 -0
  47. data/spec/vendor/samuel-0.2.1/VERSION +1 -0
  48. data/spec/vendor/samuel-0.2.1/lib/samuel.rb +52 -0
  49. data/spec/vendor/samuel-0.2.1/lib/samuel/net_http.rb +10 -0
  50. data/spec/vendor/samuel-0.2.1/lib/samuel/request.rb +96 -0
  51. data/spec/vendor/samuel-0.2.1/samuel.gemspec +69 -0
  52. data/spec/vendor/samuel-0.2.1/test/request_test.rb +193 -0
  53. data/spec/vendor/samuel-0.2.1/test/samuel_test.rb +42 -0
  54. data/spec/vendor/samuel-0.2.1/test/test_helper.rb +66 -0
  55. data/spec/vendor/samuel-0.2.1/test/thread_test.rb +32 -0
  56. data/spec/webmock_spec.rb +492 -0
  57. data/test/test_helper.rb +9 -0
  58. data/test/test_webmock.rb +56 -0
  59. metadata +144 -0
@@ -0,0 +1,114 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RequestRegistry do
4
+
5
+ before(:each) do
6
+ RequestRegistry.instance.reset_webmock
7
+ @request_profile = RequestProfile.new(:get, "www.google.com")
8
+ @request_stub = RequestStub.new(:get, "www.google.com")
9
+ end
10
+
11
+ describe "reset_webmock" do
12
+ before(:each) do
13
+ RequestRegistry.instance.register_request_stub(@request_stub)
14
+ RequestRegistry.instance.response_for_request(@request_profile)
15
+ end
16
+
17
+ it "should clean request stubs" do
18
+ RequestRegistry.instance.registered_request?(@request_profile).should == @request_stub
19
+ RequestRegistry.instance.reset_webmock
20
+ RequestRegistry.instance.registered_request?(@request_profile).should == nil
21
+ end
22
+
23
+ it "should clean list of executed requests" do
24
+ RequestRegistry.instance.times_executed(@request_profile).should == 1
25
+ RequestRegistry.instance.reset_webmock
26
+ RequestRegistry.instance.times_executed(@request_profile).should == 0
27
+ end
28
+
29
+ end
30
+
31
+ describe "registering and reporting registered requests" do
32
+
33
+ it "should return registered stub" do
34
+ RequestRegistry.instance.register_request_stub(@request_stub).should == @request_stub
35
+ end
36
+
37
+ it "should report if request stub is not registered" do
38
+ RequestRegistry.instance.registered_request?(@request_profile).should == nil
39
+ end
40
+
41
+ it "should register and report registered stib" do
42
+ RequestRegistry.instance.register_request_stub(@request_stub)
43
+ RequestRegistry.instance.registered_request?(@request_profile).should == @request_stub
44
+ end
45
+
46
+
47
+ end
48
+
49
+ describe "response for request" do
50
+
51
+ it "should registered response for request profile" do
52
+ @request_stub.response = @response = Response.new
53
+ RequestRegistry.instance.register_request_stub(@request_stub)
54
+ RequestRegistry.instance.response_for_request(@request_profile).should == @response
55
+ end
56
+
57
+ it "should report nothing if no response for request is registered" do
58
+ RequestRegistry.instance.response_for_request(@request_profile).should == nil
59
+ end
60
+
61
+ it "should increase number of times request was executed" do
62
+ RequestRegistry.instance.times_executed(@request_profile).should == 0
63
+ RequestRegistry.instance.response_for_request(@request_profile)
64
+ RequestRegistry.instance.times_executed(@request_profile).should == 1
65
+ end
66
+
67
+ it "should always return last registered matching response" do
68
+ @request_stub1 = RequestStub.new(:get, "www.google.com")
69
+ @request_stub1.response = @response1 = Response.new
70
+ @request_stub2 = RequestStub.new(:get, "www.google.com")
71
+ @request_stub2.response = @response2 = Response.new
72
+ @request_stub3 = RequestStub.new(:get, "www.google.org")
73
+ @request_stub3.response = @response3 = Response.new
74
+ RequestRegistry.instance.register_request_stub(@request_stub1)
75
+ RequestRegistry.instance.register_request_stub(@request_stub2)
76
+ RequestRegistry.instance.register_request_stub(@request_stub3)
77
+ RequestRegistry.instance.response_for_request(@request_profile).should == @response2
78
+ end
79
+
80
+ end
81
+
82
+ describe "times executed" do
83
+
84
+ def times_executed(request_profile)
85
+ self.requested.hash.select { |executed_request_profile, times_executed|
86
+ executed_request_profile.match(request_profile)
87
+ }.inject(0) {|sum, (_, times_executed)| sum =+ times_executed }
88
+ end
89
+
90
+ before(:each) do
91
+ @request_stub1 = RequestStub.new(:get, "www.google.com")
92
+ @request_stub2 = RequestStub.new(:get, "www.google.net")
93
+ @request_stub3 = RequestStub.new(:get, "www.google.org")
94
+ RequestRegistry.instance.response_for_request(RequestProfile.new(:get, "www.google.com"))
95
+ RequestRegistry.instance.response_for_request(RequestProfile.new(:get, "www.google.com"))
96
+ RequestRegistry.instance.response_for_request(RequestProfile.new(:get, "www.google.org"))
97
+ end
98
+
99
+ it "should report 0 if no request matching profile was requested" do
100
+ RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.google.net")).should == 0
101
+ end
102
+
103
+ it "should report number of times matching profile was requested" do
104
+ RequestRegistry.instance.times_executed(RequestProfile.new(:get, "www.google.com")).should == 2
105
+ end
106
+
107
+ it "should report number of times all matching profile were requested" do
108
+ RequestRegistry.instance.times_executed(RequestProfile.new(:get, /.*google.*/)).should == 3
109
+ end
110
+
111
+
112
+ end
113
+
114
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe RequestStub do
4
+
5
+ before(:each) do
6
+ @request_stub = RequestStub.new(:get, "www.google.com")
7
+ end
8
+
9
+ it "should have request profile with method and url" do
10
+ @request_stub.request_profile.method.should == :get
11
+ @request_stub.request_profile.uri.host.should == "www.google.com"
12
+ end
13
+
14
+ it "should have response" do
15
+ @request_stub.response.should be_a(WebMock::Response)
16
+ end
17
+
18
+ describe "with" do
19
+
20
+ it "should assign body to request profile" do
21
+ @request_stub.with(:body => "abc")
22
+ @request_stub.request_profile.body.should == "abc"
23
+ end
24
+
25
+ it "should assign normalized headers to request profile" do
26
+ Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
27
+ @request_stub.with(:headers => {'A' => 'a'})
28
+ @request_stub.request_profile.headers.should == {'B' => 'b'}
29
+ end
30
+
31
+ end
32
+
33
+ describe "to_return" do
34
+
35
+ it "should assign response with provided options" do
36
+ @request_stub.to_return(:body => "abc", :status => 500)
37
+ @request_stub.response.body.should == "abc"
38
+ @request_stub.response.status.should == 500
39
+ end
40
+
41
+ end
42
+
43
+ describe "to_raise" do
44
+
45
+ it "should assign response with exception to be thrown" do
46
+ @request_stub.to_raise(ArgumentError)
47
+ lambda {
48
+ @request_stub.response.raise_error_if_any
49
+ }.should raise_error(ArgumentError, "Exception from WebMock")
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Response do
4
+ before(:each) do
5
+ @response = Response.new(:headers => {'A' => 'a'})
6
+ end
7
+
8
+ it "should report normalized headers" do
9
+ Utility.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
10
+ @response.headers.should == {'B' => 'b'}
11
+ end
12
+
13
+ describe "status" do
14
+
15
+ it "should be 200 by default" do
16
+ @response.status.should == 200
17
+ end
18
+
19
+ it "should return assigned status" do
20
+ @response = Response.new(:status => 500)
21
+ @response.status.should == 500
22
+ end
23
+
24
+ end
25
+
26
+ describe "raising error" do
27
+
28
+ it "should raise error if any assigned" do
29
+ @response = Response.new(:exception => ArgumentError)
30
+ lambda {
31
+ @response.raise_error_if_any
32
+ }.should raise_error(ArgumentError, "Exception from WebMock")
33
+ end
34
+
35
+ it "should not raise error if no error assigned" do
36
+ @response.raise_error_if_any
37
+ end
38
+
39
+ end
40
+
41
+ describe "body" do
42
+
43
+ it "should return empty body by default" do
44
+ @response.body.should == ''
45
+ end
46
+
47
+ it "should report body if assigned" do
48
+ @response = Response.new(:body => "abc")
49
+ @response.body.should == "abc"
50
+ end
51
+
52
+ it "should report content of a file as body if provided" do
53
+ @response = Response.new(:body => __FILE__)
54
+ @response.body.should == File.new(__FILE__).read
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,58 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'webmock'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ include WebMock
8
+
9
+ def fail()
10
+ raise_error(Spec::Expectations::ExpectationNotMetError)
11
+ end
12
+
13
+ def fail_with(message)
14
+ raise_error(Spec::Expectations::ExpectationNotMetError,message)
15
+ end
16
+
17
+ class Proc
18
+ def should_pass
19
+ lambda { self.call }.should_not raise_error
20
+ end
21
+ end
22
+
23
+ # Sets several expectations that a real HTTP request makes it
24
+ # past WebMock to the socket layer. You can use this when you need to check
25
+ # that a request isn't handled by WebMock
26
+ #This solution is copied from FakeWeb project
27
+ def setup_expectations_for_real_request(options = {})
28
+ # Socket handling
29
+ if options[:port] == 443
30
+ socket = mock("SSLSocket")
31
+ OpenSSL::SSL::SSLSocket.should_receive(:===).with(socket).at_least(:once).and_return(true)
32
+ OpenSSL::SSL::SSLSocket.should_receive(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).at_least(:once).and_return(socket)
33
+ socket.stub!(:sync_close=).and_return(true)
34
+ socket.should_receive(:connect).at_least(:once).with()
35
+ else
36
+ socket = mock("TCPSocket")
37
+ Socket.should_receive(:===).with(socket).at_least(:once).and_return(true)
38
+ end
39
+
40
+ TCPSocket.should_receive(:open).with(options[:host], options[:port]).at_least(:once).and_return(socket)
41
+ socket.stub!(:closed?).and_return(false)
42
+ socket.stub!(:close).and_return(true)
43
+
44
+ # Request/response handling
45
+ request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
46
+ socket.should_receive(:write).with(/#{request_parts[0]}.*#{request_parts[1]}.*/m).and_return(100)
47
+
48
+ 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]}")
49
+ socket.should_receive(:sysread).any_number_of_times.and_raise(EOFError)
50
+ end
51
+
52
+ def setup_expectations_for_real_google_request(options = {})
53
+ defaults = { :host => "www.google.com", :port => 80, :method => "GET",
54
+ :path => "/",
55
+ :response_code => 200, :response_message => "OK",
56
+ :response_body => "<title>Google fake response</title>" }
57
+ setup_expectations_for_real_request(defaults.merge(options))
58
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe HashCounter do
4
+
5
+ it "should return 0 for non existing key" do
6
+ HashCounter.new.get(:abc).should == 0
7
+ end
8
+
9
+ it "should increase the returned value on every put with the same key" do
10
+ counter =HashCounter.new
11
+ counter.put(:abc)
12
+ counter.get(:abc).should == 1
13
+ counter.put(:abc)
14
+ counter.get(:abc).should == 2
15
+ end
16
+
17
+ it "should only increase value for given key provided to put" do
18
+ counter =HashCounter.new
19
+ counter.put(:abc)
20
+ counter.get(:abc).should == 1
21
+ counter.get(:def).should == 0
22
+ end
23
+
24
+ end
@@ -0,0 +1,70 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Utility do
4
+
5
+ it "should decode_userinfo_from_header handles basic auth" do
6
+ authorization_header = "Basic dXNlcm5hbWU6c2VjcmV0"
7
+ userinfo = Utility.decode_userinfo_from_header(authorization_header)
8
+ userinfo.should == "username:secret"
9
+ end
10
+
11
+ it "should encode unsafe chars in userinfo does not encode userinfo safe punctuation" do
12
+ userinfo = "user;&=+$,:secret"
13
+ userinfo.should == Utility.encode_unsafe_chars_in_userinfo(userinfo)
14
+ end
15
+
16
+ it "should encode unsafe chars in userinfo does not encode rfc 3986 unreserved characters" do
17
+ userinfo = "-.!~*'()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:secret"
18
+ userinfo.should == Utility.encode_unsafe_chars_in_userinfo(userinfo)
19
+ end
20
+
21
+ it "should encode unsafe chars in userinfo does encode other characters" do
22
+ userinfo, safe_userinfo = 'us#rn@me:sec//ret?"', 'us%23rn%40me:sec%2F%2Fret%3F%22'
23
+ safe_userinfo.should == Utility.encode_unsafe_chars_in_userinfo(userinfo)
24
+ end
25
+
26
+ it "should strip_default_port_from_uri strips 80 from http with path" do
27
+ uri = "http://example.com:80/foo/bar"
28
+ stripped_uri = Utility.strip_default_port_from_uri(uri)
29
+ stripped_uri.should == "http://example.com/foo/bar"
30
+ end
31
+
32
+ it "should strip_default_port_from_uri strips 80 from http without path" do
33
+ uri = "http://example.com:80"
34
+ stripped_uri = Utility.strip_default_port_from_uri(uri)
35
+ stripped_uri.should == "http://example.com"
36
+ end
37
+
38
+ it "should strip_default_port_from_uri strips 443 from https without path" do
39
+ uri = "https://example.com:443"
40
+ stripped_uri = Utility.strip_default_port_from_uri(uri)
41
+ stripped_uri.should == "https://example.com"
42
+ end
43
+
44
+ it "should strip_default_port_from_uri strips 443 from https" do
45
+ uri = "https://example.com:443/foo/bar"
46
+ stripped_uri = Utility.strip_default_port_from_uri(uri)
47
+ stripped_uri.should == "https://example.com/foo/bar"
48
+ end
49
+
50
+ it "should strip_default_port_from_uri does not strip 8080 from http" do
51
+ uri = "http://example.com:8080/foo/bar"
52
+ uri.should == Utility.strip_default_port_from_uri(uri)
53
+ end
54
+
55
+ it "should strip_default_port_from_uri does not strip 443 from http" do
56
+ uri = "http://example.com:443/foo/bar"
57
+ uri.should == Utility.strip_default_port_from_uri(uri)
58
+ end
59
+
60
+ it "should strip_default_port_from_uri does not strip 80 from query string" do
61
+ uri = "http://example.com/?a=:80&b=c"
62
+ uri.should == Utility.strip_default_port_from_uri(uri)
63
+ end
64
+
65
+ it "should strip_default_port_from_uri does not modify strings that do not start with http or https" do
66
+ uri = "httpz://example.com:80/"
67
+ uri.should == Utility.strip_default_port_from_uri(uri)
68
+ end
69
+
70
+ end
@@ -0,0 +1,59 @@
1
+ == 0.0.1 2007-05-15
2
+ * 1 major enhancement:
3
+ * Initial release
4
+
5
+ == 0.1.2 2007-06-27
6
+
7
+ * No major changes.
8
+
9
+ == 0.1.3 2007-07-09
10
+
11
+ * No change.
12
+
13
+ == 0.1.4 2007-08-10
14
+
15
+ * r1442, todd, 2007-08-07 15:45:24
16
+ * # 373, Add support in right_http_connection for bailing out to a block while
17
+ reading the HTTP response (to support GET streaming...)
18
+
19
+ * r1411, todd, 2007-08-03 15:14:45
20
+ * # 373, Stream uploads (PUTs) if the source is a file, stream, or anything
21
+ read()-able
22
+
23
+ == 1.1.0 2007-08-15
24
+ Initial public release
25
+
26
+ == 1.2.0 2007-10-05
27
+
28
+ * r1867, konstantin, 2007-10-05 06:19:45
29
+ * # 220, (re)open connection to server if none exists or connection params
30
+ have changed
31
+
32
+ == 1.2.1
33
+
34
+ * r2648, konstantin, 01-24-08 11:12:00
35
+ * net_fix.rb moved from right_aws gem to fix the problem with uploading the streamable
36
+ objects to S3
37
+
38
+ * r2764, konstantin, 02-08-08 00:05:00 +03:00
39
+ * "RightAws: incompatible Net::HTTP monkey-patch" exception is raised if our net_fix
40
+ patch was overriden (by attachment_fu for example, to avoid this load attachment_fu
41
+ before loading the right_http_connection gem).
42
+
43
+ == 1.2.2
44
+
45
+ * r3524, konstantin, 2008-04-17 11:35:42 +0400
46
+ * Fixed a problem with incorrect error handling (connection retries always failed).
47
+
48
+ == 1.2.3
49
+
50
+ - Added support for setting retry & timeout parameters in the constructor
51
+ - Improve handling of data streams during upload: if there is a failure and a retry, reset
52
+ the seek pointer for the subsequent re-request
53
+
54
+ == 1.2.4
55
+
56
+ * r4984, konstantin, 2008-08-11 14:49:18 +0400
57
+ * fixed a bug: <NoMethodError: You have a nil object when you didn't expect it!
58
+ The error occurred while evaluating nil.body_stream>
59
+