~ubuntu-branches/ubuntu/oneiric/ruby-rest-client/oneiric

« back to all changes in this revision

Viewing changes to spec/abstract_response_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-04-26 15:55:30 UTC
  • Revision ID: james.westby@ubuntu.com-20110426155530-5fhl7gw0gqj9f3il
Tags: upstream-1.6.1
ImportĀ upstreamĀ versionĀ 1.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
 
2
 
 
3
describe RestClient::AbstractResponse do
 
4
 
 
5
  class MyAbstractResponse
 
6
 
 
7
    include RestClient::AbstractResponse
 
8
 
 
9
    attr_accessor :size
 
10
 
 
11
    def initialize net_http_res, args
 
12
      @net_http_res = net_http_res
 
13
      @args = args
 
14
    end
 
15
 
 
16
  end
 
17
 
 
18
  before do
 
19
    @net_http_res = mock('net http response')
 
20
    @response = MyAbstractResponse.new(@net_http_res, {})
 
21
  end
 
22
 
 
23
  it "fetches the numeric response code" do
 
24
    @net_http_res.should_receive(:code).and_return('200')
 
25
    @response.code.should == 200
 
26
  end
 
27
 
 
28
  it "has a nice description" do
 
29
    @net_http_res.should_receive(:to_hash).and_return({'Content-Type' => ['application/pdf']})
 
30
    @net_http_res.should_receive(:code).and_return('200')
 
31
    @response.description == '200 OK | application/pdf  bytes\n'
 
32
  end
 
33
 
 
34
  it "beautifies the headers by turning the keys to symbols" do
 
35
    h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ])
 
36
    h.keys.first.should == :content_type
 
37
  end
 
38
 
 
39
  it "beautifies the headers by turning the values to strings instead of one-element arrays" do
 
40
    h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] )
 
41
    h.values.first.should == 'text/html'
 
42
  end
 
43
 
 
44
  it "fetches the headers" do
 
45
    @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
 
46
    @response.headers.should == { :content_type => 'text/html' }
 
47
  end
 
48
 
 
49
  it "extracts cookies from response headers" do
 
50
    @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
 
51
    @response.cookies.should == { 'session_id' => '1' }
 
52
  end
 
53
 
 
54
  it "extract strange cookies" do
 
55
    @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
 
56
    @response.cookies.should == { 'session_id' => 'ZJ%2FHQVH6YE+rVkTpn0zvTQ%3D%3D' }
 
57
  end
 
58
 
 
59
  it "doesn't escape cookies" do
 
60
    @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/'])
 
61
    @response.cookies.should == { 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' }
 
62
  end
 
63
 
 
64
  it "can access the net http result directly" do
 
65
    @response.net_http_res.should == @net_http_res
 
66
  end
 
67
end