webmock 1.6.4 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +3 -1
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.md +211 -118
  5. data/Gemfile +16 -1
  6. data/Guardfile +24 -0
  7. data/LICENSE +1 -1
  8. data/README.md +64 -15
  9. data/Rakefile +19 -6
  10. data/lib/webmock.rb +9 -4
  11. data/lib/webmock/api.rb +5 -4
  12. data/lib/webmock/assertion_failure.rb +1 -1
  13. data/lib/webmock/callback_registry.rb +1 -1
  14. data/lib/webmock/config.rb +2 -2
  15. data/lib/webmock/cucumber.rb +1 -1
  16. data/lib/webmock/errors.rb +17 -5
  17. data/lib/webmock/http_lib_adapters/{curb.rb → curb_adapter.rb} +79 -49
  18. data/lib/webmock/http_lib_adapters/{em_http_request.rb → em_http_request/em_http_request_0_x.rb} +20 -15
  19. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +201 -0
  20. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -0
  21. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
  22. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
  23. data/lib/webmock/http_lib_adapters/{httpclient.rb → httpclient_adapter.rb} +35 -8
  24. data/lib/webmock/http_lib_adapters/net_http.rb +84 -25
  25. data/lib/webmock/http_lib_adapters/net_http_response.rb +17 -17
  26. data/lib/webmock/http_lib_adapters/patron_adapter.rb +124 -0
  27. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +166 -0
  28. data/lib/webmock/minitest.rb +15 -0
  29. data/lib/webmock/rack_response.rb +52 -0
  30. data/lib/webmock/request_pattern.rb +4 -2
  31. data/lib/webmock/request_signature.rb +4 -0
  32. data/lib/webmock/request_stub.rb +35 -2
  33. data/lib/webmock/responses_sequence.rb +2 -2
  34. data/lib/webmock/rspec.rb +2 -2
  35. data/lib/webmock/rspec/matchers.rb +9 -4
  36. data/lib/webmock/rspec/matchers/webmock_matcher.rb +1 -1
  37. data/lib/webmock/stub_registry.rb +1 -1
  38. data/lib/webmock/stub_request_snippet.rb +14 -11
  39. data/lib/webmock/util/hash_keys_stringifier.rb +4 -4
  40. data/lib/webmock/util/headers.rb +3 -3
  41. data/lib/webmock/util/json.rb +54 -0
  42. data/lib/webmock/util/uri.rb +1 -1
  43. data/lib/webmock/version.rb +1 -1
  44. data/lib/webmock/webmock.rb +20 -3
  45. data/minitest/test_helper.rb +29 -0
  46. data/minitest/test_webmock.rb +6 -0
  47. data/minitest/webmock_spec.rb +30 -0
  48. data/spec/curb_spec.rb +26 -8
  49. data/spec/curb_spec_helper.rb +6 -6
  50. data/spec/em_http_request_spec.rb +95 -1
  51. data/spec/em_http_request_spec_helper.rb +16 -16
  52. data/spec/errors_spec.rb +19 -4
  53. data/spec/example_curl_output.txt +22 -22
  54. data/spec/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
  55. data/spec/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
  56. data/spec/httpclient_spec.rb +1 -1
  57. data/spec/httpclient_spec_helper.rb +3 -38
  58. data/spec/my_rack_app.rb +18 -0
  59. data/spec/net_http_shared.rb +125 -0
  60. data/spec/net_http_spec.rb +27 -31
  61. data/spec/net_http_spec_helper.rb +4 -34
  62. data/spec/network_connection.rb +1 -1
  63. data/spec/patron_spec_helper.rb +4 -7
  64. data/spec/quality_spec.rb +60 -0
  65. data/spec/rack_response_spec.rb +33 -0
  66. data/spec/real_net_http_spec.rb +20 -0
  67. data/spec/request_execution_verifier_spec.rb +8 -8
  68. data/spec/request_pattern_spec.rb +3 -3
  69. data/spec/request_stub_spec.rb +19 -19
  70. data/spec/response_spec.rb +8 -8
  71. data/spec/spec_helper.rb +14 -11
  72. data/spec/stub_request_snippet_spec.rb +85 -37
  73. data/spec/support/webmock_server.rb +62 -0
  74. data/spec/typhoeus_hydra_spec.rb +53 -0
  75. data/spec/typhoeus_hydra_spec_helper.rb +50 -0
  76. data/spec/util/headers_spec.rb +5 -5
  77. data/spec/util/json_spec.rb +7 -0
  78. data/spec/util/uri_spec.rb +1 -1
  79. data/spec/vendor/addressable/lib/uri.rb +1 -0
  80. data/spec/vendor/crack/lib/crack.rb +1 -0
  81. data/spec/vendor/right_http_connection-1.2.4/History.txt +4 -4
  82. data/spec/vendor/right_http_connection-1.2.4/README.txt +4 -4
  83. data/spec/vendor/right_http_connection-1.2.4/Rakefile +3 -3
  84. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +4 -4
  85. data/spec/vendor/right_http_connection-1.2.4/setup.rb +4 -4
  86. data/spec/webmock_shared.rb +375 -143
  87. data/spec/webmock_spec.rb +7 -0
  88. data/test/http_request.rb +24 -0
  89. data/test/shared_test.rb +47 -0
  90. data/test/test_helper.rb +6 -3
  91. data/test/test_webmock.rb +2 -67
  92. data/webmock.gemspec +8 -7
  93. metadata +153 -88
  94. data/lib/webmock/http_lib_adapters/patron.rb +0 -100
  95. data/spec/other_net_http_libs_spec.rb +0 -30
@@ -1,100 +0,0 @@
1
- if defined?(Patron)
2
-
3
- module Patron
4
- class Session
5
-
6
- def handle_request_with_webmock(req)
7
- request_signature = build_request_signature(req)
8
-
9
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
10
-
11
- if WebMock::StubRegistry.instance.registered_request?(request_signature)
12
- webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
13
- handle_file_name(req, webmock_response)
14
- res = build_patron_response(webmock_response)
15
- WebMock::CallbackRegistry.invoke_callbacks(
16
- {:lib => :patron}, request_signature, webmock_response)
17
- res
18
- elsif WebMock.net_connect_allowed?(request_signature.uri)
19
- res = handle_request_without_webmock(req)
20
- if WebMock::CallbackRegistry.any_callbacks?
21
- webmock_response = build_webmock_response(res)
22
- WebMock::CallbackRegistry.invoke_callbacks(
23
- {:lib => :patron, :real_request => true}, request_signature,
24
- webmock_response)
25
- end
26
- res
27
- else
28
- raise WebMock::NetConnectNotAllowedError.new(request_signature)
29
- end
30
- end
31
-
32
- alias_method :handle_request_without_webmock, :handle_request
33
- alias_method :handle_request, :handle_request_with_webmock
34
-
35
-
36
-
37
- def handle_file_name(req, webmock_response)
38
- if req.action == :get && req.file_name
39
- begin
40
- File.open(req.file_name, "w") do |f|
41
- f.write webmock_response.body
42
- end
43
- rescue Errno::EACCES
44
- raise ArgumentError.new("Unable to open specified file.")
45
- end
46
- end
47
- end
48
-
49
- def build_request_signature(req)
50
- uri = WebMock::Util::URI.heuristic_parse(req.url)
51
- uri.path = uri.normalized_path.gsub("[^:]//","/")
52
- uri.user = req.username
53
- uri.password = req.password
54
-
55
- if [:put, :post].include?(req.action)
56
- if req.file_name
57
- if !File.exist?(req.file_name) || !File.readable?(req.file_name)
58
- raise ArgumentError.new("Unable to open specified file.")
59
- end
60
- request_body = File.read(req.file_name)
61
- elsif req.upload_data
62
- request_body = req.upload_data
63
- else
64
- raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
65
- end
66
- end
67
-
68
- request_signature = WebMock::RequestSignature.new(
69
- req.action,
70
- uri.to_s,
71
- :body => request_body,
72
- :headers => req.headers
73
- )
74
- request_signature
75
- end
76
-
77
- def build_patron_response(webmock_response)
78
- raise Patron::TimeoutError if webmock_response.should_timeout
79
- webmock_response.raise_error_if_any
80
- res = Patron::Response.new
81
- res.instance_variable_set(:@body, webmock_response.body)
82
- res.instance_variable_set(:@status, webmock_response.status[0])
83
- res.instance_variable_set(:@status_line, webmock_response.status[1])
84
- res.instance_variable_set(:@headers, webmock_response.headers)
85
- res
86
- end
87
-
88
- def build_webmock_response(patron_response)
89
- webmock_response = WebMock::Response.new
90
- reason = patron_response.status_line.scan(%r(\AHTTP/(\d+\.\d+)\s+(\d\d\d)\s*([^\r\n]+)?\r?\z))[0][2]
91
- webmock_response.status = [patron_response.status, reason]
92
- webmock_response.body = patron_response.body
93
- webmock_response.headers = patron_response.headers
94
- webmock_response
95
- end
96
-
97
- end
98
- end
99
-
100
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "loading other Net::HTTP based libraries" do
4
-
5
- def capture_output_from_requiring(libs, additional_code = "")
6
- requires = libs.map { |lib| "require '#{lib}'" }
7
- requires << " require 'addressable/uri'"
8
- requires << " require 'crack'"
9
- requires = requires.join("; ")
10
- webmock_dir = "#{File.dirname(__FILE__)}/../lib"
11
- vendor_dirs = Dir["#{File.dirname(__FILE__)}/vendor/*/lib"]
12
- load_path_opts = vendor_dirs.unshift(webmock_dir).map { |dir| "-I#{dir}" }.join(" ")
13
-
14
- # TODO: use the same Ruby executable that this test was invoked with
15
- `ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1 | cat`
16
- end
17
-
18
- it "should requiring right http connection before webmock and then connecting does not print warning" do
19
- additional_code = "Net::HTTP.start('example.com')"
20
- output = capture_output_from_requiring %w(right_http_connection webmock), additional_code
21
- output.should be_empty
22
- end
23
-
24
- it "should requiring right http connection after webmock and then connecting prints warning" do
25
- additional_code = "Net::HTTP.start('example.com')"
26
- output = capture_output_from_requiring %w(webmock right_http_connection), additional_code
27
- output.should match(%r(Warning: RightHttpConnection has to be required before WebMock is required !!!))
28
- end
29
-
30
- end