File: typhoeus_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 (105 lines) | stat: -rw-r--r-- 2,893 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
require 'spec_helper'

describe Typhoeus do
  before(:each) do
    Typhoeus.configure { |config| config.verbose = false; config.block_connection = false }
  end

  describe ".configure" do
    it "yields config" do
      Typhoeus.configure do |config|
        expect(config).to be_a(Typhoeus::Config)
      end
    end

    it "sets values config" do
      Typhoeus::Config.verbose = true
      expect(Typhoeus::Config.verbose).to be_truthy
    end
  end

  describe ".stub" do
    let(:base_url) { "www.example.com" }

    shared_examples "lazy response construction" do
      it "calls the block to construct a response when a request matches the stub" do
        expected_response = Typhoeus::Response.new
        Typhoeus.stub(base_url) do |request|
          expected_response
        end

        response = Typhoeus.get(base_url)

        expect(response).to be(expected_response)
      end
    end

    context "when no similar expectation exists" do
      include_examples "lazy response construction"

      it "returns expectation" do
        expect(Typhoeus.stub(base_url)).to be_a(Typhoeus::Expectation)
      end

      it "adds expectation" do
        Typhoeus.stub(:get, "")
        expect(Typhoeus::Expectation.all.size).to eq(1)
      end
    end

    context "when similar expectation exists" do
      include_examples "lazy response construction"

      let(:expectation) { Typhoeus::Expectation.new(base_url) }
      before { Typhoeus::Expectation.all << expectation }

      it "returns expectation" do
        expect(Typhoeus.stub(base_url)).to be_a(Typhoeus::Expectation)
      end

      it "doesn't add expectation" do
        Typhoeus.stub(base_url)
        expect(Typhoeus::Expectation.all.size).to eq(1)
      end
    end
  end

  describe ".before" do
    it "adds callback" do
      Typhoeus.before { true }
      expect(Typhoeus.before.size).to eq(1)
    end
  end

  describe ".with_connection" do
    it "executes block with block connection is false" do
      Typhoeus.with_connection { expect(Typhoeus::Config.block_connection).to be(false) }
    end

    it "sets block connection back to previous value" do
      Typhoeus::Config.block_connection = true
      Typhoeus.with_connection {}
      expect(Typhoeus::Config.block_connection).to be(true)
    end

    it "returns result of block" do
      expect(Typhoeus.with_connection { "123" }).to eq("123")
    end
  end

  [:get, :post, :put, :delete, :head, :patch, :options].each do |name|
    describe ".#{name}" do
      let(:response) { Typhoeus::Request.method(name).call("http://localhost:3001") }

      it "returns ok" do
        expect(response.return_code).to eq(:ok)
      end

      unless name == :head
        it "makes #{name.to_s.upcase} requests" do
          expect(response.response_body).to include("\"REQUEST_METHOD\":\"#{name.to_s.upcase}\"")
        end
      end
    end
  end
end