Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 72 lines (60 sloc) 2.812 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
require File.expand_path(File.dirname(__FILE__) + '/test_helper')

require 'ostruct'

class TestWebMock < Test::Unit::TestCase

  def http_request(method, uri, options = {})
    begin
      uri = URI.parse(uri)
    rescue
      uri = Addressable::URI.heuristic_parse(uri)
    end
    response = nil
    clazz = ::Net::HTTP.const_get("#{method.to_s.capitalize}")
    req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers])
    req.basic_auth uri.user, uri.password if uri.user
    http = ::Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if uri.scheme == "https"
    response = http.start {|http|
      http.request(req, options[:body])
    }
    OpenStruct.new({
      :body => response.body,
      :headers => response,
      :status => response.code })
  end


  def setup
    super
    stub_http_request(:any, "http://www.example.com")
    stub_http_request(:any, "https://www.example.com")
  end

  def test_error_on_non_stubbed_request
    default_ruby_headers = (RUBY_VERSION >= "1.9.1") ? "{'Accept'=>'*/*', 'User-Agent'=>'Ruby'}" : "{'Accept'=>'*/*'}"
    assert_raise(WebMock::NetConnectNotAllowedError, "Real HTTP connections are disabled. Unregistered request: GET http://www.example.net/ with headers #{default_ruby_headers}") do
      http_request(:get, "http://www.example.net/")
    end
  end

  def test_verification_that_expected_request_occured
    http_request(:get, "http://www.example.com/")
    assert_requested(:get, "http://www.example.com", :times => 1)
    assert_requested(:get, "http://www.example.com")
  end

  def test_verification_that_expected_request_didnt_occur
    expected_message = "The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times"
    expected_message << "\n\nThe following requests were made:\n\nNo requests were made.\n============================================================"
    assert_fail(expected_message) do
      assert_requested(:get, "http://www.example.com")
    end
  end

  def test_verification_that_expected_request_occured_with_body_and_headers
    http_request(:get, "http://www.example.com/",
      :body => "abc", :headers => {'A' => 'a'})
    assert_requested(:get, "http://www.example.com",
      :body => "abc", :headers => {'A' => 'a'})
  end

  def test_verification_that_non_expected_request_didnt_occur
    expected_message = %r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time\n\nThe following requests were made:\n\nGET http://www.example.com/ with headers .+ was made 1 time\n\n============================================================)
    assert_fail(expected_message) do
      http_request(:get, "http://www.example.com/")
      assert_not_requested(:get, "http://www.example.com")
    end
  end

end
Something went wrong with that request. Please try again.