forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_spec.rb
More file actions
92 lines (78 loc) · 2.52 KB
/
Copy pathrender_spec.rb
File metadata and controls
92 lines (78 loc) · 2.52 KB
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
require 'spec_helper'
require 'goliath/rack/render'
require 'goliath/goliath'
describe Goliath::Rack::Render do
let(:env) do
env = Goliath::Env.new
env['params'] = {}
env
end
let(:app) { mock('app').as_null_object }
let(:render) { Goliath::Rack::Render.new(app) }
it 'accepts an app' do
lambda { Goliath::Rack::Render.new('my app') }.should_not raise_error
end
it 'returns the status, body and app headers' do
app_body = {'c' => 'd'}
app.should_receive(:call).and_return([200, {'a' => 'b'}, app_body])
status, headers, body = render.call(env)
status.should == 200
headers['a'].should == 'b'
body.should == app_body
end
describe 'Vary' do
it 'adds Accept to provided Vary header' do
app.should_receive(:call).and_return([200, {'Vary' => 'Cookie'}, {}])
status, headers, body = render.call(env)
headers['Vary'].should == 'Cookie,Accept'
end
it 'sets Accept if there is no Vary header' do
app.should_receive(:call).and_return([200, {}, {}])
status, headers, body = render.call(env)
headers['Vary'].should == 'Accept'
end
end
CONTENT_TYPES = {
'rss' => 'application/rss+xml',
'xml' => 'application/xml',
'html' => 'text/html',
'json' => 'application/json',
'yaml' => 'text/yaml',
}
describe 'Content-Type' do
before(:each) do
app.should_receive(:call).and_return([200, {}, {}])
end
describe 'from header' do
CONTENT_TYPES.values.each do |type|
it "handles content type for #{type}" do
env['HTTP_ACCEPT'] = type
status, headers, body = render.call(env)
headers['Content-Type'].should =~ /^#{Regexp.escape(type)}/
end
end
end
describe 'from URL param' do
CONTENT_TYPES.each_pair do |format, content_type|
it "converts #{format} to #{content_type}" do
env['params']['format'] = format
status, headers, body = render.call(env)
headers['Content-Type'].should =~ /^#{Regexp.escape(content_type)}/
end
end
end
it 'prefers URL format over header' do
env['HTTP_ACCEPT'] = 'application/xml'
env['params']['format'] = 'json'
status, headers, body = render.call(env)
headers['Content-Type'].should =~ %r{^application/json}
end
describe 'charset' do
it 'is set if not present' do
env['params']['format'] = 'json'
status, headers, body = render.call(env)
headers['Content-Type'].should =~ /; charset=utf-8$/
end
end
end
end