File: excon_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 (77 lines) | stat: -rw-r--r-- 2,691 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
require 'spec_helper'
require 'acceptance/webmock_shared'
require 'acceptance/excon/excon_spec_helper'

describe "Excon" do
  include ExconSpecHelper
  include_context "with WebMock", :no_url_auth

  it 'should allow Excon requests to use query hash paramters' do
    stub_request(:get, "http://example.com/resource/?a=1&b=2").to_return(body: "abc")
    expect(Excon.new('http://example.com').get(path: "resource/", query: {a: 1, b: 2}).body).to eq("abc")
  end

  it 'should support Excon :expects options' do
    stub_request(:get, "http://example.com/").to_return(body: 'a')
    expect { Excon.new('http://example.com').get(expects: 204) }.to raise_error(Excon::Errors::OK)
  end

  context "with response_block" do
    it "should support excon response_block for real requests", net_connect: true do
      a = []
      WebMock.allow_net_connect!
      r = Excon.new("#{HTTP_STATUS_SERVICE}/200", headers: { "Accept" => "*" }).
        get(response_block: lambda {|e, remaining, total| a << e}, chunk_size: 1)
      expect(a).to eq(["2", "0", "0", " ", "O", "K"])
      expect(r.body).to eq("")
    end

    it "should support excon response_block" do
      a = []
      stub_request(:get, "http://example.com/").to_return(body: "abc")
      r = Excon.new('http://example.com').get(response_block: lambda {|e, remaining, total| a << e}, chunk_size: 1)
      expect(a).to eq(['a', 'b', 'c'])
      expect(r.body).to eq("")
    end

    it "should invoke callbacks with response body even if a real request is made", net_connect: true do
      a = []
      WebMock.allow_net_connect!
      response = nil
      WebMock.after_request { |_, res|
        response = res
      }
      r = Excon.new("#{HTTP_STATUS_SERVICE}/200", headers: { "Accept" => "*" }).
        get(response_block: lambda {|e, remaining, total| a << e}, chunk_size: 1)
      expect(response.body).to eq("200 OK")
      expect(a).to eq(["2", "0", "0", " ", "O", "K"])
      expect(r.body).to eq("")
    end
  end

  let(:file) { File.new(__FILE__) }
  let(:file_contents) { File.read(__FILE__) }

  it 'handles file uploads correctly' do
    stub_request(:put, "http://example.com/upload").with(body: file_contents)

    yielded_request_body = nil
    WebMock.after_request do |req, res|
      yielded_request_body = req.body
    end

    Excon.new("http://example.com").put(path: "upload", body: file)

    expect(yielded_request_body).to eq(file_contents)
  end

  describe '.request_params_from' do

    it 'rejects invalid request keys' do
      request_params = WebMock::HttpLibAdapters::ExconAdapter.request_params_from(body: :keep, fake: :reject)
      expect(request_params).to eq(body: :keep)
    end

  end

end