Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix gemspec #94

Closed
wants to merge 6 commits into from

2 participants

@sferik

This pull request does the following:

  • Replaced Jeweler with Bundler::GemHelper.install_tasks because Jeweler was causing problems and is unnecessary
  • Fix specs to work with the latest version of curb, which returns "Moved Temporarily" instead of "Found" for HTTP 302.
  • Opt-in for testing with rubygems-test
  • Cleaned up end-of-line whitespace. This makes contributing much nicer for vim users.
@sferik

I put all these changes in separate commits, so it should be easy for you to pull only the changes you want.

@jcf
Collaborator

Hi Erik,

I've just pushed a load of commits. Most of what's in this pull request is in there with the following exceptions.

I've left out opt-in for testing (sferik@99848c8) because there's no diff rendered and as a result it's not immediately obvious what the difference is.

I've not done the whitespace clean up yet. I plan on doing that at the same time as running set et|retab with argdo to make sure there's no trailing whitespace and no mixed tabs. This will obviously rewrite a whole lot of the gem and I want it to be clear that's all it is. Just whitespace changes.

I've not merged the latest version of curb fix (sferik@4ce59f5) because when I run specs against curb v0.7.15 with the changeset in place I get spec failures, which I don't without the sferik@4ce59f5 commit.

Everything else is in there. Thanks for your help with all of this. I'm happy to revisit any of these omissions but will close this pull request for now.

@jcf jcf closed this
@sferik

sferik@99848c8 touched a blank file called .gemtest. That's why it's not showing in the diff. For more info, see: http://test.rubygems.org/

@jcf
Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 18, 2011
  1. @sferik
  2. @sferik

    Cleanup EOL whitespace

    sferik authored
  3. @sferik
  4. @sferik
  5. @sferik

    Fix specs to work with the latest version of curb

    sferik authored
    The latest version of Curb returns "Moved Temporarily" instead of
    "Found" for HTTP 302.
  6. @sferik

    Require jruby-openssl on JRuby

    sferik authored
This page is out of date. Refresh to see the latest.
Showing with 299 additions and 553 deletions.
  1. 0  .gemtest
  2. +6 −3 Gemfile
  3. +5 −26 Rakefile
  4. +0 −1  VERSION
  5. +3 −3 lib/webmock/api.rb
  6. +1 −1  lib/webmock/callback_registry.rb
  7. +2 −2 lib/webmock/config.rb
  8. +2 −2 lib/webmock/errors.rb
  9. +9 −9 lib/webmock/http_lib_adapters/curb.rb
  10. +6 −6 lib/webmock/http_lib_adapters/httpclient.rb
  11. +4 −5 lib/webmock/http_lib_adapters/net_http.rb
  12. +2 −2 lib/webmock/http_lib_adapters/net_http_response.rb
  13. +3 −3 lib/webmock/http_lib_adapters/patron.rb
  14. +1 −1  lib/webmock/request_pattern.rb
  15. +2 −2 lib/webmock/request_stub.rb
  16. +2 −2 lib/webmock/responses_sequence.rb
  17. +2 −2 lib/webmock/rspec.rb
  18. +4 −4 lib/webmock/rspec/matchers.rb
  19. +1 −1  lib/webmock/rspec/matchers/webmock_matcher.rb
  20. +4 −4 lib/webmock/util/hash_keys_stringifier.rb
  21. +3 −3 lib/webmock/util/headers.rb
  22. +1 −1  lib/webmock/util/uri.rb
  23. +4 −0 lib/webmock/version.rb
  24. +2 −8 lib/webmock/webmock.rb
  25. +7 −7 spec/curb_spec.rb
  26. +2 −2 spec/curb_spec_helper.rb
  27. +9 −9 spec/em_http_request_spec_helper.rb
  28. +3 −3 spec/errors_spec.rb
  29. +22 −22 spec/example_curl_output.txt
  30. +0 −1  spec/httpclient_spec.rb
  31. +3 −3 spec/httpclient_spec_helper.rb
  32. +6 −6 spec/net_http_spec_helper.rb
  33. +1 −1  spec/network_connection.rb
  34. +0 −30 spec/other_net_http_libs_spec.rb
  35. +2 −2 spec/patron_spec_helper.rb
  36. +8 −8 spec/request_execution_verifier_spec.rb
  37. +3 −3 spec/request_pattern_spec.rb
  38. +19 −19 spec/request_stub_spec.rb
  39. +12 −12 spec/response_spec.rb
  40. +3 −3 spec/spec_helper.rb
  41. +7 −7 spec/stub_request_snippet_spec.rb
  42. +4 −5 spec/util/headers_spec.rb
  43. +1 −1  spec/util/uri_spec.rb
  44. +97 −106 spec/webmock_shared.rb
  45. +21 −212 webmock.gemspec
View
0  .gemtest
No changes.
View
9 Gemfile
@@ -1,4 +1,7 @@
-source "http://rubygems.org/"
-gemspec
+source 'http://rubygems.org/'
+
+platforms :jruby do
+ gem 'jruby-openssl', '~> 0.7'
+end
-gem 'jeweler'
+gemspec
View
31 Rakefile
@@ -1,27 +1,7 @@
-require 'rubygems'
-require 'rake'
+#!/usr/bin/env rake
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "webmock"
- gem.summary = %Q{Library for stubbing HTTP requests in Ruby.}
- gem.description = %Q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
- gem.email = "bartosz.blimke@gmail.com"
- gem.homepage = "http://github.com/bblimke/webmock"
- gem.authors = ["Bartosz Blimke"]
- gem.add_dependency "addressable", "~> 2.2", "> 2.2.5"
- gem.add_dependency "crack", ">=0.1.7"
- gem.add_development_dependency "rspec", ">= 2.0.0"
- gem.add_development_dependency "httpclient", ">= 2.1.5.2"
- gem.add_development_dependency "patron", ">= 0.4.9" unless RUBY_PLATFORM =~ /java/
- gem.add_development_dependency "em-http-request", ">= 0.2.14" unless RUBY_PLATFORM =~ /java/
- gem.add_development_dependency "curb", ">= 0.7.8" unless RUBY_PLATFORM =~ /java/
- end
- Jeweler::GemcutterTasks.new
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
+require 'bundler'
+Bundler::GemHelper.install_tasks
namespace :spec do
desc 'Run specs against 1.8.6, REE, 1.8.7, 1.9.2 and jRuby'
@@ -34,9 +14,9 @@ namespace :spec do
end
end
-require "rspec/core/rake_task"
+require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new do |t|
- t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
t.pattern = 'spec/**/*_spec.rb'
end
@@ -58,4 +38,3 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/webmock/webmock.rb')
end
-
View
1  VERSION
@@ -1 +0,0 @@
-1.6.3
View
6 lib/webmock/api.rb
@@ -1,7 +1,7 @@
module WebMock
module API
extend self
-
+
def stub_request(method, uri)
WebMock::StubRegistry.instance.register_request_stub(WebMock::RequestStub.new(method, uri))
end
@@ -11,7 +11,7 @@ def stub_request(method, uri)
def a_request(method, uri)
WebMock::RequestPattern.new(method, uri)
end
-
+
class << self
alias :request :a_request
end
@@ -29,4 +29,4 @@ def assert_not_requested(method, uri, options = {}, &block)
WebMock::AssertionFailure.failure(verifier.negative_failure_message) unless verifier.does_not_match?
end
end
-end
+end
View
2  lib/webmock/callback_registry.rb
@@ -26,7 +26,7 @@ def self.invoke_callbacks(options, request_signature, response)
def self.reset
@@callbacks = []
end
-
+
def self.any_callbacks?
!@@callbacks.empty?
end
View
4 lib/webmock/config.rb
@@ -1,7 +1,7 @@
module WebMock
- class Config
+ class Config
include Singleton
-
+
attr_accessor :allow_net_connect
attr_accessor :allow_localhost
attr_accessor :allow
View
4 lib/webmock/errors.rb
@@ -1,5 +1,5 @@
module WebMock
-
+
class NetConnectNotAllowedError < StandardError
def initialize(request_signature)
text = "Real HTTP connections are disabled. Unregistered request: #{request_signature}"
@@ -18,4 +18,4 @@ def stubbing_instructions(request_signature)
end
end
-end
+end
View
18 lib/webmock/http_lib_adapters/curb.rb
@@ -19,7 +19,7 @@ def curb_or_webmock
webmock_response = build_webmock_response
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :curb, :real_request => true}, request_signature,
- webmock_response)
+ webmock_response)
end
res
else
@@ -54,15 +54,15 @@ def build_request_signature
end
def build_curb_response(webmock_response)
- raise Curl::Err::TimeoutError if webmock_response.should_timeout
+ raise Curl::Err::TimeoutError if webmock_response.should_timeout
webmock_response.raise_error_if_any
-
+
@body_str = webmock_response.body
@response_code = webmock_response.status[0]
@header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n"
if webmock_response.headers
- @header_str << webmock_response.headers.map do |k,v|
+ @header_str << webmock_response.headers.map do |k,v|
"#{k}: #{v.is_a?(Array) ? v.join(", ") : v}"
end.join("\r\n")
@@ -115,7 +115,7 @@ def build_webmock_response
###
### Mocks of Curl::Easy methods below here.
- ###
+ ###
def http_with_webmock(method)
@webmock_method = method
@@ -163,11 +163,11 @@ def perform_with_webmock
@webmock_method ||= :get
curb_or_webmock do
perform_without_webmock
- end
+ end
end
alias :perform_without_webmock :perform
alias :perform :perform_with_webmock
-
+
def put_data_with_webmock= data
@webmock_method = :put
@put_data = data
@@ -175,7 +175,7 @@ def put_data_with_webmock= data
end
alias_method :put_data_without_webmock=, :put_data=
alias_method :put_data=, :put_data_with_webmock=
-
+
def post_body_with_webmock= data
@webmock_method = :post
self.post_body_without_webmock = data
@@ -261,7 +261,7 @@ def self.http_#{verb}(url, data, &block)
end
METHOD
end
-
+
module WebmockHelper
# Borrowed from Patron:
# http://github.com/toland/patron/blob/master/lib/patron/response.rb
View
12 lib/webmock/http_lib_adapters/httpclient.rb
@@ -20,7 +20,7 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
response = build_httpclient_response(webmock_response, stream, &block)
res = conn.push(response)
WebMock::CallbackRegistry.invoke_callbacks(
- {:lib => :http_client}, request_signature, webmock_response)
+ {:lib => :http_client}, request_signature, webmock_response)
res
elsif WebMock.net_connect_allowed?(request_signature.uri)
res = if stream
@@ -45,7 +45,7 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
def do_request_async_with_webmock(method, uri, query, body, extheader)
req = create_request(method, uri, query, body, extheader)
request_signature = build_request_signature(req)
-
+
if WebMock::StubRegistry.instance.registered_request?(request_signature) ||
WebMock.net_connect_allowed?(request_signature.uri)
do_request_async_without_webmock(method, uri, query, body, extheader)
@@ -70,7 +70,7 @@ def build_httpclient_response(webmock_response, stream = false, &block)
response.reason=webmock_response.status[1]
webmock_response.headers.to_a.each { |name, value| response.header.set(name, value) }
- raise HTTPClient::TimeoutError if webmock_response.should_timeout
+ raise HTTPClient::TimeoutError if webmock_response.should_timeout
webmock_response.raise_error_if_any
block.call(nil, body) if block
@@ -78,7 +78,7 @@ def build_httpclient_response(webmock_response, stream = false, &block)
response
end
end
-
+
def build_webmock_response(httpclient_response)
webmock_response = WebMock::Response.new
webmock_response.status = [httpclient_response.status, httpclient_response.reason]
@@ -102,8 +102,8 @@ def build_request_signature(req)
auth = www_auth.basic_auth
auth.challenge(req.header.request_uri, nil)
-
- headers = req.header.all.inject({}) do |headers, header|
+
+ headers = req.header.all.inject({}) do |headers, header|
headers[header[0]] ||= [];
headers[header[0]] << header[1]
headers
View
9 lib/webmock/http_lib_adapters/net_http.rb
@@ -92,12 +92,12 @@ def connect_with_webmock
def build_net_http_response(webmock_response, &block)
response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
response.instance_variable_set(:@body, webmock_response.body)
- webmock_response.headers.to_a.each do |name, values|
+ webmock_response.headers.to_a.each do |name, values|
values = [values] unless values.is_a?(Array)
values.each do |value|
response.add_field(name, value)
end
- end
+ end
response.instance_variable_set(:@read, true)
@@ -111,14 +111,14 @@ def build_net_http_response(webmock_response, &block)
response
end
-
+
def build_webmock_response(net_http_response)
webmock_response = WebMock::Response.new
webmock_response.status = [
net_http_response.code.to_i,
net_http_response.message]
webmock_response.headers = net_http_response.to_hash
- webmock_response.body = net_http_response.body
+ webmock_response.body = net_http_response.body
webmock_response
end
@@ -126,7 +126,6 @@ def build_webmock_response(net_http_response)
end
-
module WebMock
module NetHTTPUtility
View
4 lib/webmock/http_lib_adapters/net_http_response.rb
@@ -1,4 +1,4 @@
-# This code is entierly copied from VCR (http://github.com/myronmarston/vcr) by courtesy of Myron Marston
+# This code is entierly copied from VCR (http://github.com/myronmarston/vcr) by courtesy of Myron Marston
# A Net::HTTP response that has already been read raises an IOError when #read_body
# is called with a destination string or block.
@@ -29,4 +29,4 @@ def read_body(dest = nil, &block)
end
end
end
-end
+end
View
6 lib/webmock/http_lib_adapters/patron.rb
@@ -21,7 +21,7 @@ def handle_request_with_webmock(req)
webmock_response = build_webmock_response(res)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :patron, :real_request => true}, request_signature,
- webmock_response)
+ webmock_response)
end
res
else
@@ -75,7 +75,7 @@ def build_request_signature(req)
end
def build_patron_response(webmock_response)
- raise Patron::TimeoutError if webmock_response.should_timeout
+ raise Patron::TimeoutError if webmock_response.should_timeout
webmock_response.raise_error_if_any
res = Patron::Response.new
res.instance_variable_set(:@body, webmock_response.body)
@@ -84,7 +84,7 @@ def build_patron_response(webmock_response)
res.instance_variable_set(:@headers, webmock_response.headers)
res
end
-
+
def build_webmock_response(patron_response)
webmock_response = WebMock::Response.new
reason = patron_response.status_line.scan(%r(\AHTTP/(\d+\.\d+)\s+(\d\d\d)\s*([^\r\n]+)?\r?\z))[0][2]
View
2  lib/webmock/request_pattern.rb
@@ -40,7 +40,7 @@ def assign_options(options)
@headers_pattern = HeadersPattern.new(options[:headers]) if options.has_key?(:headers)
@uri_pattern.add_query_params(options[:query]) if options.has_key?(:query)
end
-
+
def create_uri_pattern(uri)
if uri.is_a?(Regexp)
URIRegexpPattern.new(uri)
View
4 lib/webmock/request_stub.rb
@@ -24,12 +24,12 @@ def to_return(*response_hashes, &block)
end
def to_raise(*exceptions)
- @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e|
+ @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e|
ResponseFactory.response_for(:exception => e)
})
self
end
-
+
def to_timeout
@responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(:should_timeout => true)])
self
View
4 lib/webmock/responses_sequence.rb
@@ -1,5 +1,5 @@
module WebMock
-
+
class ResponsesSequence
attr_accessor :times_to_repeat
@@ -36,5 +36,5 @@ def increase_position
end
end
-
+
end
View
4 lib/webmock/rspec.rb
@@ -6,7 +6,7 @@
elsif defined?(Spec)
RSPEC_NAMESPACE = Spec
RSPEC_CONFIGURER = Spec::Runner
-else
+else
begin
require 'rspec/core'
require 'rspec/expectations'
@@ -19,7 +19,7 @@
end
require 'webmock/rspec/matchers'
-
+
RSPEC_CONFIGURER.configure { |config|
config.include WebMock::API
View
8 lib/webmock/rspec/matchers.rb
@@ -7,17 +7,17 @@ module Matchers
def have_been_made
WebMock::RequestPatternMatcher.new
end
-
+
def have_not_been_made
WebMock::RequestPatternMatcher.new.times(0)
end
-
+
def have_requested(method, uri)
WebMock::WebMockMatcher.new(method, uri)
end
-
+
def have_not_requested(method, uri)
WebMock::WebMockMatcher.new(method, uri).times(0)
end
end
-end
+end
View
2  lib/webmock/rspec/matchers/webmock_matcher.rb
@@ -29,7 +29,7 @@ def times(times)
def matches?(webmock)
@request_execution_verifier.matches?
end
-
+
def does_not_match?(webmock)
@request_execution_verifier.does_not_match?
end
View
8 lib/webmock/util/hash_keys_stringifier.rb
@@ -1,14 +1,14 @@
module WebMock
module Util
class HashKeysStringifier
-
+
def self.stringify_keys!(arg)
case arg
when Array
arg.map { |elem| stringify_keys!(elem) }
when Hash
Hash[
- *arg.map { |key, value|
+ *arg.map { |key, value|
k = key.is_a?(Symbol) ? key.to_s : key
v = stringify_keys!(value)
[k,v]
@@ -17,7 +17,7 @@ def self.stringify_keys!(arg)
arg
end
end
-
+
end
end
-end
+end
View
6 lib/webmock/util/headers.rb
@@ -26,12 +26,12 @@ def self.sorted_headers_string(headers)
when Regexp then v.inspect
when Array then "["+v.map{|v| "'#{v.to_s}'"}.join(", ")+"]"
else "'#{v.to_s}'"
- end
+ end
"'#{k}'=>#{v}"
- end.sort.join(", ")
+ end.sort.join(", ")
str << '}'
end
-
+
def self.decode_userinfo_from_header(header)
header.sub(/^Basic /, "").unpack("m").first
end
View
2  lib/webmock/util/uri.rb
@@ -67,7 +67,7 @@ def self.encode_unsafe_chars_in_userinfo(userinfo)
end
def self.is_uri_localhost?(uri)
- uri.is_a?(Addressable::URI) &&
+ uri.is_a?(Addressable::URI) &&
%w(localhost 127.0.0.1 0.0.0.0).include?(uri.host)
end
View
4 lib/webmock/version.rb
@@ -0,0 +1,4 @@
+module WebMock
+ # The version of the gem
+ VERSION = '1.6.3' unless defined?(::WebMock::VERSION)
+end
View
10 lib/webmock/webmock.rb
@@ -21,12 +21,6 @@ class << self
alias :request :a_request
end
- def self.version
- open(File.join(File.dirname(__FILE__), '../../VERSION')) { |f|
- f.read.strip
- }
- end
-
def self.allow_net_connect!(options = {})
Config.instance.allow_net_connect = true
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
@@ -52,12 +46,12 @@ def self.reset!
WebMock::RequestRegistry.instance.reset!
WebMock::StubRegistry.instance.reset!
end
-
+
def self.reset_webmock
WebMock::Deprecation.warning("WebMock.reset_webmock is deprecated. Please use WebMock.reset! method instead")
reset!
end
-
+
def self.reset_callbacks
WebMock::CallbackRegistry.reset
end
View
14 spec/curb_spec.rb
@@ -6,7 +6,7 @@
shared_examples_for "Curb" do
include CurbSpecHelper
-
+
it_should_behave_like "WebMock"
describe "when doing PUTs" do
@@ -35,7 +35,7 @@
stub_request(:any, "example.com").to_return(:body => body)
test = nil
- @curl.on_success do |c|
+ @curl.on_success do |c|
test = c.body_str
end
@curl.http_get
@@ -48,7 +48,7 @@
to_return(:status => [response_code, "Server On Fire"])
test = nil
- @curl.on_failure do |c, code|
+ @curl.on_failure do |c, code|
test = code
end
@curl.http_get
@@ -61,19 +61,19 @@
to_return(:body => body)
test = nil
- @curl.on_body do |data|
+ @curl.on_body do |data|
test = data
end
@curl.http_get
test.should == body
end
-
+
it "should call on_header when response headers are read" do
stub_request(:any, "example.com").
to_return(:headers => {:one => 1})
test = nil
- @curl.on_header do |data|
+ @curl.on_header do |data|
test = data
end
@curl.http_get
@@ -217,7 +217,7 @@
end
end
end
-
+
describe "#content_type" do
before(:each) do
@curl = Curl::Easy.new
View
4 spec/curb_spec_helper.rb
@@ -17,7 +17,7 @@ def http_request(method, uri, options = {}, &block)
def setup_request(uri, curl, options={})
curl ||= Curl::Easy.new
- curl.url = uri.omit(:userinfo).to_s
+ curl.url = uri.omit(:userinfo).to_s
curl.username = uri.user
curl.password = uri.password
curl.timeout = 10
@@ -54,7 +54,7 @@ def curb_http_request(uri, method, body, options)
curl.post_body = body
when :put
curl.put_data = body
- end
+ end
curl.http(method)
curl
View
18 spec/em_http_request_spec_helper.rb
@@ -12,23 +12,23 @@ def http_request(method, uri, options = {}, &block)
EventMachine.run {
request = EventMachine::HttpRequest.new("#{uri.omit(:userinfo).normalize.to_s}")
http = request.send(:setup_request, method, {
- :timeout => 10,
+ :timeout => 10,
:body => options[:body],
:query => options[:query],
'authorization' => [uri.user, uri.password],
:head => options[:headers]}, &block)
http.errback {
error = if http.respond_to?(:errors)
- http.errors
+ http.errors
else
http.error
- end
- failed
+ end
+ failed
}
- http.callback {
+ http.callback {
response = OpenStruct.new({
:body => http.response,
- :headers => WebMock::Util::Headers.normalize_headers(extract_response_headers(http)),
+ :headers => WebMock::Util::Headers.normalize_headers(extract_response_headers(http)),
:message => http.response_header.http_reason,
:status => http.response_header.status.to_s
})
@@ -53,15 +53,15 @@ def setup_expectations_for_real_request(options = {})
def http_library
:em_http_request
end
-
+
private
-
+
def extract_response_headers(http)
headers = {}
if http.response_header
http.response_header.each do |k,v|
v = v.join(", ") if v.is_a?(Array)
- headers[k] = v
+ headers[k] = v
end
end
headers
View
6 spec/errors_spec.rb
@@ -9,9 +9,9 @@
with(request_signature).and_return(mock(:to_s => "bbb"))
expected = "Real HTTP connections are disabled. Unregistered request: aaa" +
"\n\nYou can stub this request with the following snippet:\n\n" +
- "bbb\n\n============================================================"
- WebMock::NetConnectNotAllowedError.new(request_signature).message.should == expected
+ "bbb\n\n============================================================"
+ WebMock::NetConnectNotAllowedError.new(request_signature).message.should == expected
end
end
end
-end
+end
View
44 spec/example_curl_output.txt
@@ -1,22 +1,22 @@
-HTTP/1.1 202 OK
-Content-Type: text/html; charset=UTF-8
-Connection: Keep-Alive
-Date: Sat, 23 Jan 2010 01:01:05 GMT
-Content-Length: 438
-Accept: image/jpeg
-Accept: image/png
-
-<HTML>
-<HEAD>
- <TITLE>Example Web Page</TITLE>
-</HEAD>
-<body>
-<p>You have reached this web page by typing &quot;example.com&quot;,
-&quot;example.net&quot;,
- or &quot;example.org&quot; into your web browser.</p>
-<p>These domain names are reserved for use in documentation and are not available
- for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
- 2606</a>, Section 3.</p>
-</BODY>
-</HTML>
-
+HTTP/1.1 202 OK
+Content-Type: text/html; charset=UTF-8
+Connection: Keep-Alive
+Date: Sat, 23 Jan 2010 01:01:05 GMT
+Content-Length: 419
+Accept: image/jpeg
+Accept: image/png
+
+<HTML>
+<HEAD>
+ <TITLE>Example Web Page</TITLE>
+</HEAD>
+<body>
+<p>You have reached this web page by typing &quot;example.com&quot;,
+&quot;example.net&quot;,
+ or &quot;example.org&quot; into your web browser.</p>
+<p>These domain names are reserved for use in documentation and are not available
+ for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC
+ 2606</a>, Section 3.</p>
+</BODY>
+</HTML>
+
View
1  spec/httpclient_spec.rb
@@ -28,7 +28,6 @@
:get, "http://www.example.com/",
:headers => {"a" => ["c", "b"]}).status.should == "200"
end
-
describe "async requests" do
View
6 spec/httpclient_spec_helper.rb
@@ -2,7 +2,7 @@ module HTTPClientSpecHelper
class << self
attr_accessor :async_mode
end
-
+
def http_request(method, uri, options = {}, &block)
uri = Addressable::URI.heuristic_parse(uri)
c = HTTPClient.new
@@ -17,7 +17,7 @@ def http_request(method, uri, options = {}, &block)
else
response = c.request(*params, &block)
end
- headers = response.header.all.inject({}) do |headers, header|
+ headers = response.header.all.inject({}) do |headers, header|
if !headers.has_key?(header[0])
headers[header[0]] = header[1]
else
@@ -75,7 +75,7 @@ def setup_expectations_for_real_request(options = {})
socket.stub!(:eof?).and_return(true)
socket.stub!(:close).and_return(true)
end
-
+
def http_library
:http_client
end
View
12 spec/net_http_spec_helper.rb
@@ -8,14 +8,14 @@ def http_request(method, uri, options = {}, &block)
response = nil
clazz = Net::HTTP.const_get("#{method.to_s.capitalize}")
req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", nil)
- options[:headers].each do |k,v|
+ options[:headers].each do |k,v|
if v.is_a?(Array)
v.each_with_index do |v,i|
i == 0 ? (req[k] = v) : req.add_field(k, v)
end
else
req[k] = v
- end
+ end
end if options[:headers]
req.basic_auth uri.user, uri.password if uri.user
@@ -34,11 +34,11 @@ def http_request(method, uri, options = {}, &block)
OpenStruct.new({
:body => response.body,
:headers => WebMock::Util::Headers.normalize_headers(headers),
- :status => response.code,
+ :status => response.code,
:message => response.message
})
end
-
+
def client_timeout_exception_class
Timeout::Error
end
@@ -71,12 +71,12 @@ def setup_expectations_for_real_request(options = {})
# Request/response handling
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
socket.should_receive(:write).with(/#{request_parts[0]}.*#{request_parts[1]}.*/m).and_return(100)
-
+
read_method = RUBY_VERSION >= "1.9.2" ? :read_nonblock : :sysread
socket.should_receive(read_method).once.and_return("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}")
socket.should_receive(read_method).any_number_of_times.and_raise(EOFError)
end
-
+
def http_library
:net_http
end
View
2  spec/network_connection.rb
@@ -22,4 +22,4 @@ def self.is_network_available?
false
end
end
-end
+end
View
30 spec/other_net_http_libs_spec.rb
@@ -1,30 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "loading other Net::HTTP based libraries" do
-
- def capture_output_from_requiring(libs, additional_code = "")
- requires = libs.map { |lib| "require '#{lib}'" }
- requires << " require 'addressable/uri'"
- requires << " require 'crack'"
- requires = requires.join("; ")
- webmock_dir = "#{File.dirname(__FILE__)}/../lib"
- vendor_dirs = Dir["#{File.dirname(__FILE__)}/vendor/*/lib"]
- load_path_opts = vendor_dirs.unshift(webmock_dir).map { |dir| "-I#{dir}" }.join(" ")
-
- # TODO: use the same Ruby executable that this test was invoked with
- `ruby #{load_path_opts} -e "#{requires}; #{additional_code}" 2>&1 | cat`
- end
-
- it "should requiring right http connection before webmock and then connecting does not print warning" do
- additional_code = "Net::HTTP.start('example.com')"
- output = capture_output_from_requiring %w(right_http_connection webmock), additional_code
- output.should be_empty
- end
-
- it "should requiring right http connection after webmock and then connecting prints warning" do
- additional_code = "Net::HTTP.start('example.com')"
- output = capture_output_from_requiring %w(webmock right_http_connection), additional_code
- output.should match(%r(Warning: RightHttpConnection has to be required before WebMock is required !!!))
- end
-
-end
View
4 spec/patron_spec_helper.rb
@@ -11,7 +11,7 @@ def http_request(method, uri, options = {}, &block)
sess.connect_timeout = 10
sess.timeout = 10
sess.max_redirects = 0
-
+
response = sess.request(method, "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers] || {}, {
:data => options[:body]
})
@@ -19,7 +19,7 @@ def http_request(method, uri, options = {}, &block)
if response.headers
response.headers.each do |k,v|
v = v.join(", ") if v.is_a?(Array)
- headers[k] = v
+ headers[k] = v
end
end
OpenStruct.new({
View
16 spec/request_execution_verifier_spec.rb
@@ -17,7 +17,7 @@
@verifier.expected_times_executed = 2
expected_text = "The request www.example.com was expected to execute 2 times but it executed 0 times"
expected_text << @executed_requests_info
- @verifier.failure_message.should == expected_text
+ @verifier.failure_message.should == expected_text
end
it "should report failure message correctly when executed times is one" do
@@ -29,7 +29,7 @@
end
end
-
+
describe "negative failure message" do
it "should report failure message if it executed number of times specified" do
@@ -66,26 +66,26 @@
end
end
-
+
describe "does_not_match?" do
it "should fail if request executed expected number of times" do
WebMock::RequestRegistry.instance.
should_receive(:times_executed).with(@request_pattern).and_return(10)
@verifier.expected_times_executed = 10
- @verifier.does_not_match?.should be_false
+ @verifier.does_not_match?.should be_false
end
-
+
it "should succeed if request was not executed at all and expected number of times was not set" do
WebMock::RequestRegistry.instance.
should_receive(:times_executed).with(@request_pattern).and_return(0)
- @verifier.does_not_match?.should be_true
+ @verifier.does_not_match?.should be_true
end
-
+
it "should fail if request was executed and expected number of times was not set" do
WebMock::RequestRegistry.instance.
should_receive(:times_executed).with(@request_pattern).and_return(1)
- @verifier.does_not_match?.should be_false
+ @verifier.does_not_match?.should be_false
end
it "should succeed if request was not executed expected number of times" do
View
6 spec/request_pattern_spec.rb
@@ -13,7 +13,7 @@
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s.should ==
"GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
end
-
+
it "should report string describing itself with query params" do
WebMock::RequestPattern.new(:get, /.*example.*/, :query => {'a' => ['b', 'c']}).to_s.should ==
"GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
@@ -140,12 +140,12 @@ def match(request_signature)
WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end
-
+
it "should not match for query params are different than the declared in hash" do
WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
end
-
+
it "should match for query params are the same as declared as string" do
WebMock::RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
View
38 spec/request_stub_spec.rb
@@ -23,7 +23,7 @@
it "should assign normalized headers to request pattern" do
@request_stub.with(:headers => {'A' => 'a'})
- @request_stub.request_pattern.to_s.should ==
+ @request_stub.request_pattern.to_s.should ==
WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A' => 'a'}).to_s
end
@@ -69,13 +69,13 @@
@request_stub.response
@request_stub.response.body.should == "def"
end
-
+
it "should return responses in a sequence passed as comma separated params" do
@request_stub.to_return({:body => "abc"}, {:body => "def"})
@request_stub.response.body.should == "abc"
@request_stub.response.body.should == "def"
end
-
+
it "should return responses declared in multiple to_return declarations" do
@request_stub.to_return({:body => "abc"}).to_return({:body => "def"})
@request_stub.response.body.should == "abc"
@@ -92,7 +92,7 @@
@request_stub.response.raise_error_if_any
}.should raise_error(ArgumentError, "Exception from WebMock")
end
-
+
it "should assign sequence of responses with response with exception to be thrown" do
@request_stub.to_return(:body => "abc").then.to_raise(ArgumentError)
@request_stub.response.body.should == "abc"
@@ -110,7 +110,7 @@
@request_stub.response.raise_error_if_any
}.should raise_error(IndexError, "Exception from WebMock")
end
-
+
it "should raise exceptions declared in multiple to_raise declarations" do
@request_stub.to_raise(ArgumentError).then.to_raise(IndexError)
lambda {
@@ -122,7 +122,7 @@
end
end
-
+
describe "to_timeout" do
it "should assign response with timeout" do
@@ -144,30 +144,30 @@
end
end
-
-
+
+
describe "times" do
-
+
it "should give error if declared before any response declaration is declared" do
lambda {
@request_stub.times(3)
- }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
+ }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
end
-
+
it "should repeat returning last declared response declared number of times" do
@request_stub.to_return({:body => "abc"}).times(2).then.to_return({:body => "def"})
@request_stub.response.body.should == "abc"
@request_stub.response.body.should == "abc"
@request_stub.response.body.should == "def"
end
-
+
it "should repeat raising last declared exception declared number of times" do
@request_stub.to_return({:body => "abc"}).times(2).then.to_return({:body => "def"})
@request_stub.response.body.should == "abc"
@request_stub.response.body.should == "abc"
@request_stub.response.body.should == "def"
end
-
+
it "should repeat returning last declared sequence of responses declared number of times" do
@request_stub.to_return({:body => "abc"}, {:body => "def"}).times(2).then.to_return({:body => "ghj"})
@request_stub.response.body.should == "abc"
@@ -176,23 +176,23 @@
@request_stub.response.body.should == "def"
@request_stub.response.body.should == "ghj"
end
-
+
it "should return self" do
@request_stub.to_return({:body => "abc"}).times(1).should == @request_stub
end
-
+
it "should raise error if argument is not integer" do
lambda {
@request_stub.to_return({:body => "abc"}).times("not number")
- }.should raise_error("times(N) accepts integers >= 1 only")
+ }.should raise_error("times(N) accepts integers >= 1 only")
end
-
+
it "should raise error if argument is < 1" do
lambda {
@request_stub.to_return({:body => "abc"}).times(0)
- }.should raise_error("times(N) accepts integers >= 1 only")
+ }.should raise_error("times(N) accepts integers >= 1 only")
end
-
+
end
end
View
24 spec/response_spec.rb
@@ -137,16 +137,16 @@
it "should read headers" do
@response.headers.should == {
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
- "Content-Type"=>"text/html; charset=UTF-8",
- "Content-Length"=>"438",
- "Connection"=>"Keep-Alive",
- "Accept"=>"image/jpeg, image/png"
+ "Date" => "Sat, 23 Jan 2010 01:01:05 GMT",
+ "Content-Type" => "text/html; charset=UTF-8",
+ "Content-Length" => "419",
+ "Connection" => "Keep-Alive",
+ "Accept" => "image/jpeg, image/png"
}
end
it "should read body" do
- @response.body.size.should == 438
+ @response.body.size.should == 419
end
it "should close IO" do
@@ -169,20 +169,20 @@
@response.headers.should == {
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
- "Content-Length"=>"438",
+ "Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
}
end
it "should read body" do
- @response.body.size.should == 438
+ @response.body.size.should == 419
end
it "should work with transfer-encoding set to chunked" do
- @input.gsub!("Content-Length: 438", "Transfer-Encoding: chunked")
+ @input.gsub!("Content-Length: 419", "Transfer-Encoding: chunked")
@response = WebMock::Response.new(@input)
- @response.body.size.should == 438
+ @response.body.size.should == 419
end
end
@@ -249,14 +249,14 @@
describe "as a file" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s] })
- response.evaluate(@request_signature).body.size.should == 438
+ response.evaluate(@request_signature).body.size.should == 419
end
end
describe "as a string" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s].read })
- response.evaluate(@request_signature).body.size.should == 438
+ response.evaluate(@request_signature).body.size.should == 419
end
end
end
View
6 spec/spec_helper.rb
@@ -2,7 +2,7 @@
require 'httpclient'
unless RUBY_PLATFORM =~ /java/
require 'curb'
- require 'patron'
+ require 'patron'
require 'em-http'
end
@@ -15,7 +15,7 @@
require 'network_connection'
RSpec.configure do |config|
- config.include WebMock::API
+ config.include WebMock::API
unless NetworkConnection.is_network_available?
warn("No network connectivity. Only examples which do not make real network connections will run.")
no_network_connection = true
@@ -45,7 +45,7 @@ def should_pass
def setup_expectations_for_real_example_com_request(options = {})
defaults = { :host => "www.example.com", :port => 80, :method => "GET",
:path => "/",
- :response_code => 302, :response_message => "Found",
+ :response_code => 302, :response_message => "Moved Temporarily",
:response_body => "" }
setup_expectations_for_real_request(defaults.merge(options))
end
View
14 spec/stub_request_snippet_spec.rb
@@ -13,32 +13,32 @@
it "should print stub request snippet with body if available" do
@request_signature.body = "abcdef"
- expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
+ expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
"\n with(:body => \"abcdef\")." +
"\n to_return(:status => 200, :body => \"\", :headers => {})"
WebMock::StubRequestSnippet.new(@request_signature).to_s.should == expected
end
-
+
it "should print stub request snippet with multiline body" do
@request_signature.body = "abc\ndef"
- expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
+ expected = %Q(stub_request(:get, "http://www.example.com/?a=b&c=d").)+
"\n with(:body => \"abc\\ndef\")." +
"\n to_return(:status => 200, :body => \"\", :headers => {})"
WebMock::StubRequestSnippet.new(@request_signature).to_s.should == expected
end
-
+
it "should print stub request snippet with headers if any" do
@request_signature.headers = {'B' => 'b', 'A' => 'a'}
- expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
+ expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
"\n with(:headers => {\'A\'=>\'a\', \'B\'=>\'b\'})." +
"\n to_return(:status => 200, :body => \"\", :headers => {})"
WebMock::StubRequestSnippet.new(@request_signature).to_s.should == expected
end
-
+
it "should print stub request snippet with body and headers" do
@request_signature.body = "abcdef"
@request_signature.headers = {'B' => 'b', 'A' => 'a'}
- expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
+ expected = 'stub_request(:get, "http://www.example.com/?a=b&c=d").'+
"\n with(:body => \"abcdef\", \n :headers => {\'A\'=>\'a\', \'B\'=>\'b\'})." +
"\n to_return(:status => 200, :body => \"\", :headers => {})"
WebMock::StubRequestSnippet.new(@request_signature).to_s.should == expected
View
9 spec/util/headers_spec.rb
@@ -9,20 +9,19 @@
end
describe "sorted_headers_string" do
-
+
it "should return nice string for hash with string values" do
WebMock::Util::Headers.sorted_headers_string({"a" => "b"}).should == "{'A'=>'b'}"
end
-
+
it "should return nice string for hash with array values" do
WebMock::Util::Headers.sorted_headers_string({"a" => ["b", "c"]}).should == "{'A'=>['b', 'c']}"
end
-
+
it "should return nice string for hash with array values and string values" do
WebMock::Util::Headers.sorted_headers_string({"a" => ["b", "c"], "d" => "e"}).should == "{'A'=>['b', 'c'], 'D'=>'e'}"
end
-
-
+
end
end
View
2  spec/util/uri_spec.rb
@@ -171,7 +171,7 @@
uri = 'http://www.example.com:80/path?a[]=b&a[]=c'
lambda { WebMock::Util::URI.normalize_uri(uri) }.should_not raise_error(ArgumentError)
end
-
+
end
describe "stripping default port" do
View
203 spec/webmock_shared.rb
@@ -9,15 +9,6 @@
class MyException < StandardError; end;
-describe "WebMock version" do
-
- it "should report version" do
- WebMock.version.should == open(File.join(File.dirname(__FILE__), "..", "VERSION")).read.strip
- end
-
-end
-
-
shared_examples_for "WebMock" do
before(:each) do
WebMock.disable_net_connect!
@@ -64,7 +55,7 @@ class MyException < StandardError; end;
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end
-
+
it "should return stubbed response if request with path was stubbed" do
stub_http_request(:get, "www.example.com/hello_world").to_return(:body => "abc")
http_request(:get, "http://www.example.com/hello_world").body.should == "abc"
@@ -111,7 +102,7 @@ class MyException < StandardError; end;
}.should raise_error(connection_refused_exception_class)
end
end
-
+
describe "is not allowed with exception for allowed domains" do
before(:each) do
WebMock.disable_net_connect!(:allow => ["www.example.org"])
@@ -152,26 +143,26 @@ class MyException < StandardError; end;
stub_http_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
end
-
+
end
describe "on query params" do
-
+
it "should match the request by query params declared as a hash" do
stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
end
-
+
it "should match the request by query declared as a string" do
stub_http_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").to_return(:body => "abc")
http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
end
-
+
it "should match the request by query params declared both in uri and query option" do
stub_http_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
end
-
+
end
describe "on method" do
@@ -232,27 +223,27 @@ class MyException < StandardError; end;
end
end
-
- describe "when body is declared as a hash" do
+
+ describe "when body is declared as a hash" do
before(:each) do
stub_http_request(:post, "www.example.com").
with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
end
-
+
describe "for request with url encoded body" do
-
+
it "should match request if hash matches body" do
http_request(
:post, "http://www.example.com/",
:body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
end
-
+
it "should match request if hash matches body in different order of params" do
http_request(
:post, "http://www.example.com/",
:body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
end
-
+
it "should not match if hash doesn't match url encoded body" do
lambda {
http_request(
@@ -260,47 +251,47 @@ class MyException < StandardError; end;
:body => 'c[d][]=f&a=1&c[d][]=e').status.should == "200"
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
end
-
+
end
-
-
+
+
describe "for request with json body and content type is set to json" do
-
+
it "should match if hash matches body" do
http_request(
:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
:body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
end
-
+
it "should match if hash matches body in different form" do
http_request(
:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
:body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}").status.should == "200"
end
-
+
end
-
+
describe "for request with xml body and content type is set to xml" do
before(:each) do
WebMock.reset!
stub_http_request(:post, "www.example.com").
with(:body => { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
end
-
+
it "should match if hash matches body" do
http_request(
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
:body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
end
-
+
it "should match if hash matches body in different form" do
http_request(
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
+ :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
:body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
end
-
+
end
-
+
end
end
@@ -313,30 +304,30 @@ class MyException < StandardError; end;
:get, "http://www.example.com/",
:headers => SAMPLE_HEADERS).status.should == "200"
end
-
+
it "should match requests if headers are the same and declared as array" do
stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b"]} )
http_request(
:get, "http://www.example.com/",
:headers => {"a" => "b"}).status.should == "200"
end
-
+
describe "when multiple headers with the same key are used" do
-
+
it "should match requests if headers are the same" do
stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
http_request(
:get, "http://www.example.com/",
:headers => {"a" => ["b", "c"]}).status.should == "200"
end
-
+
it "should match requests if headers are the same but in different order" do
stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
http_request(
:get, "http://www.example.com/",
:headers => {"a" => ["c", "b"]}).status.should == "200"
end
-
+
it "should not match requests if headers are different" do
stub_http_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]})
@@ -346,7 +337,7 @@ class MyException < StandardError; end;
:headers => {"a" => ["b", "d"]})
}.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
end
-
+
end
it "should match requests if request headers are not stubbed" do
@@ -464,14 +455,14 @@ class MyException < StandardError; end;
http_request(:get, "http://www.example.com/")
}.should raise_error(MyException, "Exception from WebMock")
end
-
+
it "should raise exception if declared in a stubbed response as exception instance" do
stub_http_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(MyException, "hello world")
end
-
+
it "should raise exception if declared in a stubbed response as exception instance" do
stub_http_request(:get, "www.example.com").to_raise("hello world")
lambda {
@@ -491,7 +482,7 @@ class MyException < StandardError; end;
describe "raising timeout errors" do
-
+
it "should raise timeout exception if declared in a stubbed response" do
stub_http_request(:get, "www.example.com").to_timeout
lambda {
@@ -521,7 +512,7 @@ class MyException < StandardError; end;
response = http_request(:get, "http://www.example.com/")
response.headers["Content-Length"].should == "8888"
end
-
+
it "should return declared headers when there are multiple headers with the same key" do
stub_http_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
response = http_request(:get, "http://www.example.com/")
@@ -532,21 +523,21 @@ class MyException < StandardError; end;
stub_http_request(:get, "www.example.com").to_return(:status => 500)
http_request(:get, "http://www.example.com/").status.should == "500"
end
-
+
it "should return declared status message" do
stub_http_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
http_request(:get, "http://www.example.com/").message.should == "Internal Server Error"
end
-
+
it "should return default status code" do
stub_http_request(:get, "www.example.com")
http_request(:get, "http://www.example.com/").status.should == "200"
end
-
+
it "should return default empty message" do
stub_http_request(:get, "www.example.com")
http_request(:get, "http://www.example.com/").message.should == ""
- end
+ end
it "should return body declared as IO" do
stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
@@ -626,22 +617,22 @@ def call(request)
it "should return recorded headers" do
@response.headers.should == {
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
- "Content-Type"=>"text/html; charset=UTF-8",
- "Content-Length"=>"438",
- "Connection"=>"Keep-Alive",
- "Accept"=>"image/jpeg, image/png"
+ "Date" => "Sat, 23 Jan 2010 01:01:05 GMT",
+ "Content-Type" => "text/html; charset=UTF-8",
+ "Content-Length" => "419",
+ "Connection" => "Keep-Alive",
+ "Accept" => "image/jpeg, image/png"
}
end
it "should return recorded body" do
- @response.body.size.should == 438
+ @response.body.size.should == 419
end
it "should return recorded status" do
@response.status.should == "202"
end
-
+
it "should return recorded status message" do
@response.message.should == "OK"
end
@@ -664,20 +655,20 @@ def call(request)
@response.headers.should == {
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
- "Content-Length"=>"438",
+ "Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
}
end
it "should return recorded body" do
- @response.body.size.should == 438
+ @response.body.size.should == 419
end
it "should return recorded status" do
@response.status.should == "202"
end
-
+
it "should return recorded status message" do
@response.message.should == "OK"
end
@@ -692,12 +683,12 @@ def call(request)
it "should return response from evaluated file" do
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
- http_request(:get, "http://www.example.com/").body.size.should == 438
+ http_request(:get, "http://www.example.com/").body.size.should == 419
end
it "should return response from evaluated string" do
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
- http_request(:get, "http://www.example.com/").body.size.should == 438
+ http_request(:get, "http://www.example.com/").body.size.should == 419
end
end
@@ -873,7 +864,7 @@ def call(request)
a_request(:get, "http://www.example.com").should_not have_been_made
}.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
end
-
+
it "should fail with message with executed requests listed" do
lambda {
http_request(:get, "http://www.example.com/")
@@ -963,14 +954,14 @@ def call(request)
a_request(:get, /.*example.*/).should have_been_made
}.should_not raise_error
end
-
+
end
describe "when matching requests with query params" do
before(:each) do
stub_http_request(:any, /.*example.*/)
end
-
+
it "should pass if the request was executed with query params declared in a hash in query option" do
lambda {
http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
@@ -991,7 +982,7 @@ def call(request)
a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
}.should_not raise_error
end
-
+
end
it "should fail if requested more times than expected" do
@@ -1047,22 +1038,22 @@ def call(request)
with(:body => "xabc").should have_been_made
}.should fail_with(%r(The request GET http://www.example.com/ with body "xabc" was expected to execute 1 time but it executed 0 times))
end
-
+
end
-
+
describe "when expected body is declared as a hash" do
let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
let(:fail_message) {%r(The request POST http://www.example.com/ with body \{"a"=>"1", "b"=>"five", "c"=>\{"d"=>\["e", "f"\]\}\} was expected to execute 1 time but it executed 0 times)}
describe "when request is executed with url encoded body matching hash" do
-
+
it "should succeed" do
lambda {
http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should_not raise_error
end
-
+
it "should succeed if url encoded params have different order" do
lambda {
http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&b=five&c[d][]=f')
@@ -1076,7 +1067,7 @@ def call(request)
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should fail_with(fail_message)
end
-
+
end
describe "when request is executed with json body matching hash and content type is set to json" do
@@ -1088,7 +1079,7 @@ def call(request)
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should_not raise_error
end
-
+
it "should succeed if json body is in different form" do
lambda {
http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
@@ -1096,13 +1087,13 @@ def call(request)
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should_not raise_error
end
-
+
end
describe "when request is executed with xml body matching hash and content type is set to xml" do
let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
-
+
it "should succeed" do
lambda {
http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
@@ -1110,7 +1101,7 @@ def call(request)
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should_not raise_error
end
-
+
it "should succeed if xml body is in different form" do
lambda {
http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
@@ -1118,9 +1109,9 @@ def call(request)
a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
}.should_not raise_error
end
-
+
end
-
+
end
it "should succeed if request was executed with the same headers" do
@@ -1130,7 +1121,7 @@ def call(request)
with(:headers => SAMPLE_HEADERS).should have_been_made
}.should_not raise_error
end
-
+
it "should succeed if request was executed with the same headers with value declared as array" do
lambda {
http_request(:get, "http://www.example.com/", :headers => {"a" => "b"})
@@ -1138,9 +1129,9 @@ def call(request)
with(:headers => {"a" => ["b"]}).should have_been_made
}.should_not raise_error
end
-
+
describe "when multiple headers with the same key are passed" do
-
+
it "should succeed if request was executed with the same headers" do
lambda {
http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1148,7 +1139,7 @@ def call(request)
with(:headers => {"a" => ["b", "c"]}).should have_been_made
}.should_not raise_error
end
-
+
it "should succeed if request was executed with the same headers but different order" do
lambda {
http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1156,7 +1147,7 @@ def call(request)
with(:headers => {"a" => ["c", "b"]}).should have_been_made
}.should_not raise_error
end
-
+
it "should fail if request was executed with different headers" do
lambda {
http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
@@ -1164,7 +1155,7 @@ def call(request)
with(:headers => {"a" => ["b", "d"]}).should have_been_made
}.should fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>\['b', 'd'\]\} was expected to execute 1 time but it executed 0 times))
end
-
+
end
it "should fail if request was executed with different headers" do
@@ -1404,20 +1395,20 @@ def call(request)
describe "callbacks" do
-
+
describe "after_request" do
before(:each) do
WebMock.reset_callbacks
stub_request(:get, "http://www.example.com")