File: header_spec.rb

package info (click to toggle)
ruby-typhoeus 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: ruby: 4,489; makefile: 6
file content (147 lines) | stat: -rw-r--r-- 4,685 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require 'spec_helper'

describe Typhoeus::Response::Header do
  let(:raw) { nil }
  let(:header) { Typhoeus::Response::Header.new(raw) }

  describe ".new" do
    context "when string" do
      let(:raw) { 'Date: Fri, 29 Jun 2012 10:09:23 GMT' }

      it "sets Date" do
        expect(header['Date']).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
      end

      it "provides case insensitive access" do
        expect(header['DaTe']).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
      end

      it "provides symbol access" do
        expect(header[:date]).to eq('Fri, 29 Jun 2012 10:09:23 GMT')
      end
    end

    context "when hash" do
      let(:raw) { { 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' } }

      it "sets Date" do
        expect(header['Date']).to eq(raw['Date'])
      end

      it "provides case insensitive access" do
        expect(header['DaTe']).to eq(raw['Date'])
      end
    end
  end

  describe "#parse" do
    context "when no header" do
      it "returns nil" do
        expect(header).to be_empty
      end
    end

    context "when header" do
      let(:raw) do
        'HTTP/1.1 200 OK
        Set-Cookie: NID=61=LblqYgUOu; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly
        Date: Fri, 29 Jun 2012 10:09:23 GMT
        Expires: -1
        Cache-Control: private, max-age=0
        Content-Type: text/html; charset=ISO-8859-1
        Set-Cookie: PREF=ID=77e93yv0hPtejLou; expires=Sun, 29-Jun-2014 10:09:23 GMT; path=/; domain=.google.de
        Set-Cookie: NID=61=LblqYgh5Ou; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly
        P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
        Server: gws
        X-XSS-Protection: 1; mode=block
        X-Frame-Options: SAMEORIGIN
        Transfer-Encoding: chunked'.gsub(/^\s{8}/, '')
      end

      it "sets raw" do
        expect(header.send(:raw)).to eq(raw)
      end

      it "sets Set-Cookie" do
        expect(header['set-cookie'].size).to eq(3)
      end

      it "provides case insensitive access" do
        expect(header['Set-CooKie'].size).to eq(3)
      end

      [
        'NID=61=LblqYgUOu; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly',
        'PREF=ID=77e93yv0hPtejLou; expires=Sun, 29-Jun-2014 10:09:23 GMT; path=/; domain=.google.de',
        'NID=61=LblqYgh5Ou; expires=Sat, 29-Dec-2012 10:09:23 GMT; path=/; domain=.google.de; HttpOnly'
      ].each_with_index do |cookie, i|
        it "sets Cookie##{i}" do
          expect(header['set-cookie']).to include(cookie)
        end
      end

      {
        'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT', 'Expires' => '-1',
        'Cache-Control' => 'private, max-age=0',
        'Content-Type' => 'text/html; charset=ISO-8859-1',
        'P3P' => 'CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."',
        'Server' => 'gws', 'X-XSS-Protection' => '1; mode=block',
        'X-Frame-Options' => 'SAMEORIGIN', 'Transfer-Encoding' => 'chunked'
      }.each do |name, value|
        it "sets #{name}" do
          expect(header[name.downcase]).to eq(value)
        end
      end

      context 'includes a multi-line header' do
        let(:raw) do
          'HTTP/1.1 200 OK
          Date: Fri, 29 Jun 2012 10:09:23 GMT
          Content-Security-Policy: default-src "self";
            img-src * data: "self";
            upgrade-insecure-requests;'.gsub(/^\s{10}/, '')
        end

        it "joins header parts" do
          expect(header).to eq({
            'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT',
            'Content-Security-Policy' => 'default-src "self"; img-src * data: "self"; upgrade-insecure-requests;'
          })
        end
      end

      context 'includes line with only whitespace' do
        let(:raw) do
          'HTTP/1.1 200 OK
          Date: Fri, 29 Jun 2012 10:09:23 GMT
            
          '.gsub(/^\s{10}/, '')
        end

        it 'ignores it' do
          expect(header).to eq({ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' })
        end
      end

      context 'with broken headers' do
        let(:raw) do
          'HTTP/1.1 200 OK
          Date:
          Content-Type
          '.gsub(/^\s{10}/, '')
        end

        it 'returns empty string for invalid headers' do
          expect(header.to_hash).to include({ 'Date' => '', 'Content-Type' => '' })
        end
      end
    end
  end

  it "can be Marshal'd" do
    header = Typhoeus::Response::Header.new("Foo: Bar")
    expect {
      Marshal.dump(header)
    }.not_to raise_error
  end
end