File: api_spec.rb

package info (click to toggle)
ruby-webmock 3.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,905; makefile: 6
file content (175 lines) | stat: -rw-r--r-- 5,541 bytes parent folder | download | duplicates (4)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'spec_helper'

describe WebMock::API do
  describe '#hash_including' do
    subject { klass.new.hash_including(args) }
    let(:args) { { data: :one } }

    context 'when mixed into a class that does not define `hash_including`' do
      let(:klass) do
        Class.new do
          include WebMock::API
        end
      end

      it 'uses WebMock::Matchers::HashIncludingMatcher' do
        expect(subject).to be_a(WebMock::Matchers::HashIncludingMatcher)
      end

      #  by testing equality for HashIncludingMatcher (which stringifies the passed hash) we are
      #  testing HashIncludingMatcher.initialize behavior as well
      context "when args correspond to an hash" do
        it "creates 'HashIncludingMatcher'" do
          expect(subject).to eq("data" => :one)
        end
      end

      context "when args are one or many keys" do
        subject {klass.new.hash_including(:foo, :bar)}
        let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }

        it "creates 'HashIncludingMatcher' with keys anythingized" do
          expect(subject).to eq("foo" => anything, "bar" => anything )
        end
      end

      context "when args are both keys and key/value pairs" do
        subject {klass.new.hash_including(:foo, :bar, data: :one)}
        let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }

        it "creates 'HashIncludingMatcher' with keys anythingized" do
          expect(subject).to eq("foo" => anything, "bar" => anything, "data" => :one)
        end
      end

      context "when args are an empty hash" do
        subject {klass.new.hash_including({})}

        it "creates 'HashIncludingMatcher' with an empty hash" do
          expect(subject).to eq({})
        end
      end
    end


    context 'when mixed into a class with a parent that defines `hash_including`' do
      subject { klass.new.hash_including(*args) }
      let(:args) { %w(:foo, :bar, {:data => :one}) }
      let(:klass) do
        Class.new(
          Class.new do
            def hash_including(*args)
              args
            end
          end
        ) { include WebMock::API }
      end

      it 'uses super and passes the args untampered' do
        expect(subject).to eq(args)
      end
    end
  end

  describe '#hash_excluding' do
    subject { klass.new.hash_excluding(args) }
    let(:args) { { data: :one } }

    context 'when mixed into a class that does not define `hash_including`' do
      let(:klass) do
        Class.new do
          include WebMock::API
        end
      end

      it 'uses WebMock::Matchers::HashIncludingMatcher' do
        expect(subject).to be_a(WebMock::Matchers::HashExcludingMatcher)
      end

      #  by testing equality for HashIncludingMatcher (which stringifies the passed hash) we are
      #  testing HashIncludingMatcher.initialize behavior as well
      context 'when args correspond to an hash' do
        context 'creates "HashExcludingMatcher"' do
          it 'equals hash with similar key but different value' do
            expect(subject).to eq('data' => :two)
          end

          it 'equals hash with similar value but different key' do
            expect(subject).to eq('data2' => :one)
          end

          it 'equals hash with defferent value and key' do
            expect(subject).to eq('data2' => :two)
          end

          it 'not equals with similar value and key' do
            expect(subject).not_to eq('data' => :one)
          end
        end
      end

      context 'when args are one or many keys' do
        subject { klass.new.hash_excluding(:foo, :bar) }
        let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }

        it "creates 'HashExcludingMatcher' with keys anythingized" do
          expect(subject).not_to eq('foo' => anything, 'bar' => anything )
        end
      end

      context 'when args are both keys and key/value pairs' do
        subject { klass.new.hash_excluding(:foo, :bar, data: :one) }
        let(:anything) { WebMock::Matchers::AnyArgMatcher.new(nil) }

        it 'creates "HashExcludingMatcher" with keys anythingized' do
          expect(subject).not_to eq('foo' => anything, 'bar' => anything, 'data' => :one)
        end
      end

      context 'when args are an empty hash' do
        subject { klass.new.hash_excluding({}) }

        it 'creates "HashExcludingMatcher" with an empty hash' do
          expect(subject).to eq({})
        end
      end
    end

    context 'when mixed into a class with a parent that defines `hash_excluding`' do
      subject { klass.new.hash_excluding(*args) }
      let(:args) { %w(:foo, :bar, {:data => :one}) }
      let(:klass) do
        Class.new(
          Class.new do
            def hash_excluding(*args)
              args
            end
          end
        ) { include WebMock::API }
      end

      it 'uses super and passes the args untampered' do
        expect(subject).to eq(args)
      end
    end
  end

  describe '#reset_executed_requests!' do
    subject {  WebMock::API.reset_executed_requests! }

    let(:request_signature) { WebMock::RequestSignature.new(:get, "www.example.com") }
    let(:request_pattern)   { WebMock::RequestPattern.new(:get, "www.example.com") }

    before do
      WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
    end

    it 'resets request registry counter' do
      expect{
        subject
      }.to change{
        WebMock::RequestRegistry.instance.times_executed(request_pattern)
      }.from(1).to(0)
    end
  end
end