File: get_spec.rb

package info (click to toggle)
ruby-ethon 0.16.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: ruby: 5,403; sh: 9; makefile: 8
file content (126 lines) | stat: -rw-r--r-- 3,142 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true
require 'spec_helper'

describe Ethon::Easy::Http::Get do
  let(:easy) { Ethon::Easy.new }
  let(:url) { "http://localhost:3001/" }
  let(:params) { nil }
  let(:form) { nil }
  let(:options) { {} }
  let(:get) { described_class.new(url, {:params => params, :body => form}.merge(options)) }

  describe "#setup" do
    it "sets url" do
      get.setup(easy)
      expect(easy.url).to eq(url)
    end

    context "when body" do
      let(:form) { { :a => 1 } }

      it "sets customrequest" do
        expect(easy).to receive(:customrequest=).with("GET")
        get.setup(easy)
      end
    end

    context "when no body" do
      it "doesn't set customrequest" do
        expect(easy).to receive(:customrequest=).never
        get.setup(easy)
      end
    end

    context "when requesting" do
      before do
        get.setup(easy)
        easy.perform
      end

      context "when url already contains params" do
        let(:url) { "http://localhost:3001/?query=here" }
        let(:params) { {:a => "1&b=2"} }

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

        it "is a get request" do
          expect(easy.response_body).to include('"REQUEST_METHOD":"GET"')
        end

        it "requests parameterized url" do
          expect(easy.effective_url).to eq("http://localhost:3001/?query=here&a=1%26b%3D2")
        end
      end

      context "when params and no body" do
        let(:params) { {:a => "1&b=2"} }

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

        it "is a get request" do
          expect(easy.response_body).to include('"REQUEST_METHOD":"GET"')
        end

        it "requests parameterized url" do
          expect(easy.effective_url).to eq("http://localhost:3001/?a=1%26b%3D2")
        end
      end

      context "when params and body" do
        let(:params) { {:a => "1&b=2"} }
        let(:form) { {:b => "2"} }

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

        it "is a get request" do
          expect(easy.response_body).to include('"REQUEST_METHOD":"GET"')
        end

        it "requests parameterized url" do
          expect(easy.effective_url).to eq("http://localhost:3001/?a=1%26b%3D2")
        end
      end

      context "with :escape" do
        let(:params) { {:a => "1&b=2"} }

        context 'missing' do
          it "escapes values" do
            expect(easy.url).to eq("#{url}?a=1%26b%3D2")
          end
        end

        context 'nil' do
          let(:options) { {:escape => nil} }

          it "escapes values" do
            expect(easy.url).to eq("#{url}?a=1%26b%3D2")
          end
        end

        context 'true' do
          let(:options) { {:escape => true} }

          it "escapes values" do
            expect(easy.url).to eq("#{url}?a=1%26b%3D2")
          end
        end

        context 'false' do
          let(:options) { {:escape => false} }

          it "sends raw values" do
            expect(easy.url).to eq("#{url}?a=1&b=2")
          end
        end
      end

    end
  end
end