File: shared_test.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (126 lines) | stat: -rw-r--r-- 5,875 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require File.expand_path(File.dirname(__FILE__) + '/http_request')

module SharedTest
  include HttpRequestTestHelper

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

  def test_assert_requested_with_stub_and_block_raises_error
    assert_raises ArgumentError do
      assert_requested(@stub_http) {}
    end
  end

  def test_assert_not_requested_with_stub_and_block_raises_error
    assert_raises ArgumentError do
      assert_not_requested(@stub_http) {}
    end
  end

  def test_error_on_non_stubbed_request
    assert_raise_with_message(WebMock::NetConnectNotAllowedError, %r{Real HTTP connections are disabled. Unregistered request: GET http://www.example.net/ with 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_stub_occured
    http_request(:get, "http://www.example.com/")
    assert_requested(@stub_http, times: 1)
    assert_requested(@stub_http)
  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_stub_didnt_occur
    expected_message = "The request ANY 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(@stub_http)
    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_expected_request_occured_with_query_params
    stub_request(:any, "http://www.example.com").with(query: hash_including({"a" => ["b", "c"]}))
    http_request(:get, "http://www.example.com/?a[]=b&a[]=c&x=1")
    assert_requested(:get, "http://www.example.com",
      query: hash_including({"a" => ["b", "c"]}))
  end

  def test_verification_that_expected_request_not_occured_with_query_params
    stub_request(:any, 'http://www.example.com').with(query: hash_including(a: ['b', 'c']))
    stub_request(:any, 'http://www.example.com').with(query: hash_excluding(a: ['b', 'c']))
    http_request(:get, 'http://www.example.com/?a[]=b&a[]=c&x=1')
    assert_not_requested(:get, 'http://www.example.com', query: hash_excluding('a' => ['b', 'c']))
  end

  def test_verification_that_expected_request_occured_with_excluding_query_params
    stub_request(:any, 'http://www.example.com').with(query: hash_excluding('a' => ['b', 'c']))
    http_request(:get, 'http://www.example.com/?a[]=b&a[]=y&x=1')
    assert_requested(:get, 'http://www.example.com', query: hash_excluding('a' => ['b', 'c']))
  end

  def test_verification_that_expected_request_occured_with_hash_excluding_body
    stub_request(:post, 'http://www.example.com').with(body: hash_excluding('a' => ['b', 'c']))
    http_request(:post, 'http://www.example.com/',
      body: 'a[]=b&a[]=y&x=1',
      headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
    )
    assert_requested(:post, 'http://www.example.com', body: hash_excluding('a' => ['b', 'c']))
  end

  def test_verification_that_expected_request_not_occured_with_hash_excluding_body
    stub_request(:post, 'http://www.example.com').with(body: hash_excluding('a' => ['b', 'c']))
    http_request(:post, 'http://www.example.com/',
      body: 'a[]=b&a[]=c&x=1',
      headers: {'Content-Type' => 'application/x-www-form-urlencoded'}
    )
    assert_not_requested(:post, 'http://www.example.com', body: hash_excluding('a' => ['b', 'c']))
  end

  def test_verification_that_non_expected_request_didnt_occur
    expected_message = %r(The request GET http://www.example.com/ was not expected to execute 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

  def test_refute_requested_alias
    expected_message = %r(The request GET http://www.example.com/ was not expected to execute 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/")
      refute_requested(:get, "http://www.example.com")
    end
  end

  def test_verification_that_non_expected_stub_didnt_occur
    expected_message = %r(The request ANY http://www.example.com/ was not expected to execute 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(@stub_http)
    end
  end
end