File: abstract_response_spec.rb

package info (click to toggle)
ruby-rest-client 1.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 680 kB
  • ctags: 167
  • sloc: ruby: 2,799; makefile: 4
file content (88 lines) | stat: -rw-r--r-- 3,345 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
78
79
80
81
82
83
84
85
86
87
88
require 'spec_helper'

describe RestClient::AbstractResponse do

  class MyAbstractResponse

    include RestClient::AbstractResponse

    attr_accessor :size

    def initialize net_http_res, args, request
      @net_http_res = net_http_res
      @args = args
      @request = request
    end

  end

  before do
    @net_http_res = double('net http response')
    @request = double('restclient request', :url => 'http://example.com')
    @response = MyAbstractResponse.new(@net_http_res, {}, @request)
  end

  it "fetches the numeric response code" do
    expect(@net_http_res).to receive(:code).and_return('200')
    expect(@response.code).to eq 200
  end

  it "has a nice description" do
    expect(@net_http_res).to receive(:to_hash).and_return({'Content-Type' => ['application/pdf']})
    expect(@net_http_res).to receive(:code).and_return('200')
    expect(@response.description).to eq "200 OK | application/pdf  bytes\n"
  end

  it "beautifies the headers by turning the keys to symbols" do
    h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ])
    expect(h.keys.first).to eq :content_type
  end

  it "beautifies the headers by turning the values to strings instead of one-element arrays" do
    h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] )
    expect(h.values.first).to eq 'text/html'
  end

  it "fetches the headers" do
    expect(@net_http_res).to receive(:to_hash).and_return('content-type' => [ 'text/html' ])
    expect(@response.headers).to eq({ :content_type => 'text/html' })
  end

  it "extracts cookies from response headers" do
    expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
    expect(@response.cookies).to eq({ 'session_id' => '1' })
  end

  it "extract strange cookies" do
    expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
    expect(@response.headers).to eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']})
    expect(@response.cookies).to eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' })
  end

  it "doesn't escape cookies" do
    expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/'])
    expect(@response.cookies).to eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' })
  end

  it "can access the net http result directly" do
    expect(@response.net_http_res).to eq @net_http_res
  end

  describe "#return!" do
    it "should return the response itself on 200-codes" do
      expect(@net_http_res).to receive(:code).and_return('200')
      expect(@response.return!).to be_equal(@response)
    end

    it "should raise RequestFailed on unknown codes" do
      expect(@net_http_res).to receive(:code).and_return('1000')
      expect { @response.return! }.to raise_error RestClient::RequestFailed
    end

    it "should raise an error on a redirection after non-GET/HEAD requests" do
      expect(@net_http_res).to receive(:code).and_return('301')
      @response.args.merge(:method => :put)
      expect { @response.return! }.to raise_error RestClient::RequestFailed
    end
  end
end