ianwhite / response_for

response for lets you decorate your actions respond_to blocks

This URL has Read+Write access

response_for / spec / controllers / pick_template_spec.rb
3b1ae5e4 » ianwhite 2008-09-13 API change and rewrite. Va... 1 require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2
3 module PickTemplateSpec
4 # example setup
5 class TemplateOnlyController < ActionController::Base
6 self.view_paths << File.join(File.dirname(__FILE__), '../fixtures/views')
7 end
8
9 class RespondToTypesController < TemplateOnlyController
10 def an_action
11 respond_to :atom, :xml, :html, :js
12 end
13 end
14
15 class RespondToBlockController < TemplateOnlyController
16 def an_action
17 respond_to do |format|
18 format.atom
19 format.xml
20 format.html
21 format.js
22 end
23 end
24 end
25
26 class ResponseForTypesController < TemplateOnlyController
27 response_for :an_action, :types => [:atom, :xml, :html, :js]
28 end
29
30 class ResponseForBlockController < TemplateOnlyController
31 response_for :an_action do |format|
32 format.atom
33 format.xml
34 format.html
35 format.js
36 end
37 end
38
39 class ResponseForMixOfBlockAndTypesController < TemplateOnlyController
40 # response_for gives priority to most recent declarations, and
41 # gives priority of blocks over types.
42 response_for :an_action, :types => :js
43 response_for :an_action, :types => :html
44 response_for :an_action, :types => :xml do |format|
45 format.atom
46 end
47 end
48
49 class SuperclassController < TemplateOnlyController
50 response_for :an_action, :types => :js
51 end
52
53 class InheritedController < SuperclassController
54 response_for :an_action, :types => [:xml, :html] do |format|
55 format.atom
56 end
57 end
58
59 # specs
60 describe "Standard behaviour of respond_to :atom, :xml, :html, :js", :shared => true do
61 it "GET :an_action, should render an_action.atom" do
62 get :an_action
63 response.body.should == 'body of an_action.atom'
64 end
65
66 describe "GET :an_action, HTTP_ACCEPT =" do
67 it "text/html, should render an_action.html" do
68 request.env["HTTP_ACCEPT"] = 'text/html'
69 get :an_action
70 response.body.should == 'body of an_action.html'
71 end
72
73 it "application/xml, should render an_action.xml" do
74 request.env["HTTP_ACCEPT"] = 'application/xml'
75 get :an_action
76 response.body.should == 'body of an_action.xml'
77 end
78
79 it "text/javascript, should render an_action.js" do
80 request.env["HTTP_ACCEPT"] = 'text/javascript'
81 get :an_action
82 response.body.should == 'body of an_action.js'
83 end
84
85 it "application/atom+xml, should render an_action.atom" do
86 request.env["HTTP_ACCEPT"] = 'application/atom+xml'
87 get :an_action
88 response.body.should == 'body of an_action.atom'
89 end
90 end
91
92 describe "GET :an_action, :format =>" do
93 it ":html, should render an_action.html" do
94 get :an_action, :format => 'html'
95 response.body.should == 'body of an_action.html'
96 end
97
98 it ":js, should render an_action.js" do
99 get :an_action, :format => 'js'
100 response.body.should == 'body of an_action.js'
101 end
102
103 it ":xml, should render an_action.xml" do
104 get :an_action, :format => 'xml'
105 response.body.should == 'body of an_action.xml'
106 end
107
108 it ":atom, should render an_action.atom" do
109 get :an_action, :format => 'atom'
110 response.body.should == 'body of an_action.atom'
111 end
112 end
113 end
114
115 describe "Picking template" do
116 integrate_views
117
118 describe TemplateOnlyController do
119 describe "GET :an_action, HTTP_ACCEPT =" do
120 it "text/html, should render an_action.html" do
121 request.env["HTTP_ACCEPT"] = 'text/html'
122 get :an_action
123 response.body.should == 'body of an_action.html'
124 end
125
126 it "application/xml, should IGNORE and render an_action.html" do
127 request.env["HTTP_ACCEPT"] = 'application/xml'
128 get :an_action
129 response.body.should == 'body of an_action.html'
130 end
131 end
132 end
133
134 describe "[:atom, :xml, :html, :js]" do
135 describe RespondToTypesController do
136 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
137 end
138
139 describe RespondToBlockController do
140 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
141 end
142
143 describe ResponseForTypesController do
144 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
145 end
146
147 describe ResponseForBlockController do
148 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
149 end
150
151 describe ResponseForMixOfBlockAndTypesController do
152 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
153 end
154
155 describe InheritedController do
156 it_should_behave_like "Standard behaviour of respond_to :atom, :xml, :html, :js"
157 end
158 end
159 end
160 end