File: response_spec.rb

package info (click to toggle)
ruby-http 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 404 kB
  • ctags: 213
  • sloc: ruby: 2,397; makefile: 7
file content (100 lines) | stat: -rw-r--r-- 2,801 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
89
90
91
92
93
94
95
96
97
98
99
100
require 'spec_helper'

describe HTTP::Response do
  it 'includes HTTP::Headers::Mixin' do
    expect(described_class).to include HTTP::Headers::Mixin
  end

  describe 'to_a' do
    let(:body)         { 'Hello world' }
    let(:content_type) { 'text/plain' }
    subject { HTTP::Response.new(200, '1.1', {'Content-Type' => content_type}, body) }

    it 'returns a Rack-like array' do
      expect(subject.to_a).to eq([200, {'Content-Type' => content_type}, body])
    end
  end

  describe 'mime_type' do
    subject { HTTP::Response.new(200, '1.1', headers, '').mime_type }

    context 'without Content-Type header' do
      let(:headers) { {} }
      it { should be_nil }
    end

    context 'with Content-Type: text/html' do
      let(:headers) { {'Content-Type' => 'text/html'} }
      it { should eq 'text/html' }
    end

    context 'with Content-Type: text/html; charset=utf-8' do
      let(:headers) { {'Content-Type' => 'text/html; charset=utf-8'} }
      it { should eq 'text/html' }
    end
  end

  describe 'charset' do
    subject { HTTP::Response.new(200, '1.1', headers, '').charset }

    context 'without Content-Type header' do
      let(:headers) { {} }
      it { should be_nil }
    end

    context 'with Content-Type: text/html' do
      let(:headers) { {'Content-Type' => 'text/html'} }
      it { should be_nil }
    end

    context 'with Content-Type: text/html; charset=utf-8' do
      let(:headers) { {'Content-Type' => 'text/html; charset=utf-8'} }
      it { should eq 'utf-8' }
    end
  end

  describe '#parse' do
    let(:headers)   { {'Content-Type' => content_type} }
    let(:body)      { '{"foo":"bar"}' }
    let(:response)  { HTTP::Response.new 200, '1.1', headers, body }

    context 'with known content type' do
      let(:content_type) { 'application/json' }
      it 'returns parsed body' do
        expect(response.parse).to eq 'foo' => 'bar'
      end
    end

    context 'with unknown content type' do
      let(:content_type) { 'application/deadbeef' }
      it 'raises HTTP::Error' do
        expect { response.parse }.to raise_error HTTP::Error
      end
    end

    context 'with explicitly given mime type' do
      let(:content_type) { 'application/deadbeef' }
      it 'ignores mime_type of response' do
        expect(response.parse 'application/json').to eq 'foo' => 'bar'
      end

      it 'supports MIME type aliases' do
        expect(response.parse :json).to eq 'foo' => 'bar'
      end
    end
  end

  describe '#flush' do
    let(:body)      { double :to_s => '' }
    let(:response)  { HTTP::Response.new 200, '1.1', {}, body }

    it 'returns response self-reference' do
      expect(response.flush).to be response
    end

    it 'flushes body' do
      expect(body).to receive :to_s
      response.flush
    end
  end
end