File: request_spec.rb

package info (click to toggle)
ruby-typhoeus 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: ruby: 4,381; makefile: 6
file content (232 lines) | stat: -rw-r--r-- 6,429 bytes parent folder | download | duplicates (5)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'spec_helper'

describe Typhoeus::Request do
  let(:base_url) { "localhost:3001" }
  let(:options) { {:verbose => true, :headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :maxredirs => 50} }
  let(:request) { Typhoeus::Request.new(base_url, options) }

  describe ".url" do
    context "when no parameters" do
      it "returns base_url" do
        expect(request.url).to eq(request.base_url)
      end
    end

    context "when parameters" do
      let(:options) { {:params => {:a => 1}} }

      it "returns full url" do
        expect(request.url).to eq("#{request.base_url}?a=1")
      end
    end

    it "pushes an easy back into the pool" do
      easy = double.as_null_object
      allow(Typhoeus::Pool).to receive(:get).and_return(easy)
      expect(Typhoeus::Pool).to receive(:release).with(easy)
      request.url
    end
  end

  describe ".new" do
    it "stores base_url" do
      expect(request.base_url).to eq(base_url)
    end

    it "stores options" do
      expect(request.options).to eq(options)
    end

    it "stores original options" do
      expect(request.original_options).to eq(options)
      expect(request.original_options).to_not be(request.options)
    end

    it "sets defaults" do
      expect(request.options[:headers]['User-Agent']).to be
    end
  end

  describe "set_defaults" do
    context "when header with user agent" do
      let(:options) { {:headers => {'User-Agent' => "Custom"} } }

      it "doesn't modify user agent" do
        expect(request.options[:headers]['User-Agent']).to eq("Custom")
      end
    end

    context "when header without user agent" do
      let(:options) { {:headers => {} } }

      it "add user agent" do
        agent = request.options[:headers]['User-Agent']
        expect(agent).to eq(Typhoeus::USER_AGENT)
      end
    end

    context "when Config.user_agent set" do
      before { Typhoeus.configure { |config| config.user_agent = "Default" } }
      after { Typhoeus.configure { |config| config.user_agent = nil } }

      context "with headers" do
        let(:options) { {:headers => { "User-Agent" => "Fubar" } } }

        it "uses the request options' user agent" do
          expect(request.options[:headers]["User-Agent"]).to eq("Fubar")
        end
      end

      context "without headers" do
        let(:options) { {:headers => {} } }

        it "uses the global options' user agent" do
          expect(request.options[:headers]["User-Agent"]).to eq("Default")
        end
      end
    end

    context "when Config.verbose set" do
      before { Typhoeus.configure { |config| config.verbose = true} }
      after { Typhoeus.configure { |config| config.verbose = false} }

      it "respects" do
        expect(request.options[:verbose]).to be_truthy
      end
    end

    context "when maxredirs" do
      context "when not set" do
        it "defaults to 50" do
          expect(request.options[:maxredirs]).to be(50)
        end
      end

      context "when set" do
        let(:options) { {:maxredirs => 1} }

        it "respects" do
          expect(request.options[:maxredirs]).to be(1)
        end
      end
    end

    context "when Config.proxy set" do
      before { Typhoeus.configure { |config| config.proxy = "http://proxy.internal" } }
      after { Typhoeus.configure { |config| config.proxy = nil } }

      it "respects" do
        expect(request.options[:proxy]).to eq("http://proxy.internal")
      end

      context "when option proxy set" do
        let(:options) { {:proxy => nil} }

        it "does not override" do
          expect(request.options[:proxy]).to be_nil
        end
      end
    end
  end

  describe "#eql?" do
    context "when another class" do
      let(:other) { "" }

      it "returns false" do
        expect(request).to_not eql other
      end
    end

    context "when same class" do
      let(:other) { Typhoeus::Request.new("base_url", options) }

      context "when other base_url" do
        it "returns false" do
          expect(request).to_not eql other
        end
      end

      context "when same base_url and other options" do
        let(:other) { Typhoeus::Request.new(base_url, {}) }

        it "returns false" do
          expect(request).to_not eql other
        end
      end


      context "when same base_url and options" do
        context "when same order" do
          let(:other) { Typhoeus::Request.new(base_url, options) }

          it "returns true" do
            expect(request).to eql other
          end
        end

        context "when different order" do
          let(:other_options) {
            {:headers => { 'User-Agent' => "Fubar", 'Expect' => ""}, :verbose => true }
          }
          let(:other) { Typhoeus::Request.new(base_url, other_options)}

          it "returns true" do
            expect(request).to eql other
          end
        end
      end
    end
  end

  describe "#hash" do
    context "when request.eql?(other)" do
      context "when different order" do
        let(:other_options) {
          {:headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :verbose => true }
        }
        let(:other) { Typhoeus::Request.new(base_url, other_options)}

        it "has same hashes" do
          expect(request.hash).to eq(other.hash)
        end
      end

      context "when same order" do
        let(:other) { Typhoeus::Request.new(base_url, options) }

        it "has same hashes" do
          expect(request.hash).to eq(other.hash)
        end
      end

      context "when hashes with different orders are contained in arrays" do
        let(:request) { Typhoeus::Request.new(base_url, :params => [{:b => 2, :a => 1}]) }
        let(:other) { Typhoeus::Request.new(base_url, :params => [{:a => 1, :b => 2}]) }
        it "has different hashes" do
          expect(request.hash).to eq(other.hash)
        end
      end
    end

    context "when not request.eql?(other)" do
      let(:request) { Typhoeus::Request.new(base_url, :params => {:foo => 'bar'}) }
      let(:other) { Typhoeus::Request.new(base_url, :params => {:foo => 'baz'}) }

      it "has different hashes" do
        expect(request.hash).to_not eq(other.hash)
      end
    end
  end

  describe "#encoded_body" do
    let(:request) {
      Typhoeus::Request.new("www.example.com",:body => {:a => 1})
    }

    it "returns encoded body" do
      expect(request.encoded_body).to eq("a=1")
    end
  end

end