/
pick_template_spec.rb
160 lines (134 loc) · 4.75 KB
/
pick_template_spec.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
module PickTemplateSpec
# example setup
class TemplateOnlyController < ActionController::Base
self.view_paths = [File.join(File.dirname(__FILE__), '../fixtures/views')]
end
class RespondToTypesController < TemplateOnlyController
def an_action
respond_to :atom, :xml, :html, :js
end
end
class RespondToBlockController < TemplateOnlyController
def an_action
respond_to do |format|
format.atom
format.xml
format.html
format.js
end
end
end
class ResponseForTypesController < TemplateOnlyController
response_for :an_action, :types => [:atom, :xml, :html, :js]
end
class ResponseForBlockController < TemplateOnlyController
response_for :an_action do |format|
format.atom
format.xml
format.html
format.js
end
end
class ResponseForMixOfBlockAndTypesController < TemplateOnlyController
# response_for gives priority to most recent declarations, and
# gives priority of blocks over types.
response_for :an_action, :types => :js
response_for :an_action, :types => :html
response_for :an_action, :types => :xml do |format|
format.atom
end
end
class SuperclassController < TemplateOnlyController
response_for :an_action, :types => :js
end
class InheritedController < SuperclassController
response_for :an_action, :types => [:xml, :html] do |format|
format.atom
end
end
# specs
describe "Standard behaviour of respond_to :atom, :xml, :html, :js", :shared => true do
it "GET :an_action, should render an_action.atom" do
get :an_action
response.body.should == 'body of an_action.atom'
end
describe "GET :an_action, HTTP_ACCEPT =" do
it "text/html, should render an_action.html" do
request.env["HTTP_ACCEPT"] = 'text/html'
get :an_action
response.body.should == 'body of an_action.html'
end
it "application/xml, should render an_action.xml" do
request.env["HTTP_ACCEPT"] = 'application/xml'
get :an_action
response.body.should == 'body of an_action.xml'
end
it "text/javascript, should render an_action.js" do
request.env["HTTP_ACCEPT"] = 'text/javascript'
get :an_action
response.body.should == 'body of an_action.js'
end
it "application/atom+xml, should render an_action.atom" do
request.env["HTTP_ACCEPT"] = 'application/atom+xml'
get :an_action
response.body.should == 'body of an_action.atom'
end
end
describe "GET :an_action, :format =>" do
it ":html, should render an_action.html" do
get :an_action, :format => 'html'
response.body.should == 'body of an_action.html'
end
it ":js, should render an_action.js" do
get :an_action, :format => 'js'
response.body.should == 'body of an_action.js'
end
it ":xml, should render an_action.xml" do
get :an_action, :format => 'xml'
response.body.should == 'body of an_action.xml'
end
it ":atom, should render an_action.atom" do
get :an_action, :format => 'atom'
response.body.should == 'body of an_action.atom'
end
end
end
describe "Picking template" do
integrate_views
describe TemplateOnlyController do
describe "GET :an_action, HTTP_ACCEPT =" do
it "text/html, should render an_action.html" do
request.env["HTTP_ACCEPT"] = 'text/html'
get :an_action
response.body.should == 'body of an_action.html'
end
it "application/xml, should IGNORE and render an_action.html" do
request.env["HTTP_ACCEPT"] = 'application/xml'
get :an_action
response.body.should == 'body of an_action.html'
end
end
end
describe "[:atom, :xml, :html, :js]" do
describe RespondToTypesController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
describe RespondToBlockController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
describe ResponseForTypesController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
describe ResponseForBlockController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
describe ResponseForMixOfBlockAndTypesController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
describe InheritedController do
it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
end
end
end
end