aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/response/response_spec.rb
blob: 9d2097a9878f10ef7b71a1d10adce41b4329d231 (plain) (blame)
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
# encoding: UTF-8
require 'spec_helper'

describe Zero::Response do
  subject { Zero::Response.new() }

  describe '#to_a' do
    it "returns an array within status header and body" do
      subject.status = 200
      subject.header = {}
      subject.body   = []

      value = subject.to_a

      value.should be_an_instance_of(Array)
      value[0].should eq(200) # Status code
      value[1].should be_an_instance_of(Hash)  # Headers
      value[2].should eq([])  # Body
    end

    it "returns the content length in the header" do
      subject.body = ['foobar']
      value = subject.to_a

      value[1]['Content-Length'].should eq('6')  # Headers
    end

    it "does not fix the Content-Length, if it's already set" do
      subject.body = ['foobar']
      subject.header = {'Content-Length' => '3'}
      value = subject.to_a

      value[1]['Content-Length'].should eq('3')  # Headers
    end

    it "returns the Content-Type in the header" do
      subject.header = {'Content-Type' => 'application/json'}
      value = subject.to_a

      value[1]['Content-Type'].should eq('application/json')  # Headers
    end

    it "returns as default 'text/html' as Content-Type" do
      value = subject.to_a

      value[1]['Content-Type'].should eq('text/html')  # Headers
    end

    it "removes Content-Type, Content-Length and body on status code 204" do
      subject.body.push '"foobar"'
      subject.content_type = 'application/json'
      subject.header['Content-Length'] = 8

      subject.status = 204
      value = subject.to_a

      value[1].should eq({})  # Headers
    end

    it "removes Content-Type, Content-Length and body on status code 304" do
      subject.body.push '"foobar"'
      subject.content_type = 'application/json'
      subject.header['Content-Length'] = 8

      subject.status = 304
      value = subject.to_a

      value[1].should eq({})  # Headers
    end
  end

  describe '#status' do
    it "must return the status always as an integer" do
      subject.status = "foobar"
      subject.status.should eq(0)

      subject.status = 240.5
      subject.status.should eq(240)
    end

    it "must return 200, if no status code was set" do
      subject.status.should eq(200)
    end
  end

  describe '#header' do
    it "must return an empty hash, if no header was set" do
      subject.header.should eq({})
    end
  end

  describe '#body' do
    it "must return an empty array, if no body was set" do
      subject.body.should eq([])
    end
  end

  describe '#content_length' do
    it "sets the Content-Length to '0', if there is no content" do
      subject.content_length

      subject.header['Content-Length'].should eq('0')
    end

    it "sets the Content-Length to the size of the message body" do
      subject.body = ['foo', 'bar']
      subject.content_length

      subject.header['Content-Length'].should eq('6')
    end

     it "sets the Content-Length to the bytesize of the message body" do
      subject.body = ['föö', 'bär']
      subject.content_length

      subject.header['Content-Length'].should eq('9')
    end
  end

  describe '#content_type' do
    it "sets the Content-Type to the given value" do
      subject.content_type = 'application/json'

      subject.header['Content-Type'].should eq('application/json')
    end
  end

end