changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec_on_rails/spec/rails/matchers/redirect_to_spec.rb

changeset 16: 01fd3f10ae84
author: moriq@moriq.com
date: Mon Mar 10 10:13:18 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec_on_rails
1require File.dirname(__FILE__) + '/../../spec_helper'
2
3['isolation','integration'].each do |mode|
4 describe "redirect_to behaviour", :type => :controller do
5 if mode == 'integration'
6 integrate_views
7 end
8 controller_name :redirect_spec
9
10 it "redirected to another action" do
11 get 'action_with_redirect_to_somewhere'
12 response.should redirect_to(:action => 'somewhere')
13 end
14
15 it "redirected to another controller and action" do
16 get 'action_with_redirect_to_other_somewhere'
17 response.should redirect_to(:controller => 'render_spec', :action => 'text_action')
18 end
19
20 it "redirected to another action (with 'and return')" do
21 get 'action_with_redirect_to_somewhere_and_return'
22 response.should redirect_to(:action => 'somewhere')
23 end
24
25 it "redirected to correct path with leading /" do
26 get 'action_with_redirect_to_somewhere'
27 response.should redirect_to('/redirect_spec/somewhere')
28 end
29
30 it "redirected to correct path without leading /" do
31 get 'action_with_redirect_to_somewhere'
32 response.should redirect_to('redirect_spec/somewhere')
33 end
34
35 it "redirected to correct internal URL" do
36 get 'action_with_redirect_to_somewhere'
37 response.should redirect_to("http://test.host/redirect_spec/somewhere")
38 end
39
40 it "redirected to correct external URL" do
41 get 'action_with_redirect_to_rspec_site'
42 response.should redirect_to("http://rspec.rubyforge.org")
43 end
44
45 it "redirected :back" do
46 request.env['HTTP_REFERER'] = "http://test.host/previous/page"
47 get 'action_with_redirect_back'
48 response.should redirect_to(:back)
49 end
50
51 it "redirected :back and should redirect_to URL matches" do
52 request.env['HTTP_REFERER'] = "http://test.host/previous/page"
53 get 'action_with_redirect_back'
54 response.should redirect_to("http://test.host/previous/page")
55 end
56
57 it "redirected from within a respond_to block" do
58 get 'action_with_redirect_in_respond_to'
59 response.should redirect_to('redirect_spec/somewhere')
60 end
61
62 params_as_hash = {:action => "somewhere", :id => 1111, :param1 => "value1", :param2 => "value2"}
63
64 it "redirected to an internal URL containing a query string" do
65 get "action_with_redirect_which_creates_query_string"
66 response.should redirect_to(params_as_hash)
67 end
68
69 it "redirected to an internal URL containing a query string, one way it might be generated" do
70 get "action_with_redirect_with_query_string_order1"
71 response.should redirect_to(params_as_hash)
72 end
73
74 it "redirected to an internal URL containing a query string, another way it might be generated" do
75 get "action_with_redirect_with_query_string_order2"
76 response.should redirect_to(params_as_hash)
77 end
78
79 it "redirected to an internal URL which is unroutable but matched via a string" do
80 get "action_with_redirect_to_unroutable_url_inside_app"
81 response.should redirect_to("http://test.host/nonexistant/none")
82 end
83
84 end
85
86
87 describe "redirect_to with a controller spec in #{mode} mode and a custom request.host", :type => :controller do
88 if mode == 'integration'
89 integrate_views
90 end
91 controller_name :redirect_spec
92 before do
93 request.host = "some.custom.host"
94 end
95
96 it "should pass when redirected to another action" do
97 get 'action_with_redirect_to_somewhere'
98 response.should redirect_to(:action => 'somewhere')
99 end
100 end
101
102 describe "Given a controller spec in #{mode} mode", :type => :controller do
103 if mode == 'integration'
104 integrate_views
105 end
106 controller_name :redirect_spec
107
108 it "an action that redirects should not result in an error if no should redirect_to expectation is called" do
109 get 'action_with_redirect_to_somewhere'
110 end
111
112 it "an action that redirects should not result in an error if should_not redirect_to expectation was called, but not to that action" do
113 get 'action_with_redirect_to_somewhere'
114 response.should_not redirect_to(:action => 'another_destination')
115 end
116
117 it "an action that redirects should result in an error if should_not redirect_to expectation was called to that action" do
118 get 'action_with_redirect_to_somewhere'
119 lambda {
120 response.should_not redirect_to(:action => 'somewhere')
121 }.should fail_with("expected not to be redirected to {:action=>\"somewhere\"}, but was")
122 end
123
124 it "an action that does not redirects should not result in an error if should_not redirect_to expectation was called" do
125 get 'action_with_no_redirect'
126 response.should_not redirect_to(:action => 'any_destination')
127 end
128
129
130 end
131
132 describe "Given a controller spec in #{mode} mode, should redirect_to should fail when", :type => :controller do
133 if mode == 'integration'
134 integrate_views
135 end
136 controller_name :redirect_spec
137
138 it "redirected to wrong action" do
139 get 'action_with_redirect_to_somewhere'
140 lambda {
141 response.should redirect_to(:action => 'somewhere_else')
142 }.should fail_with("expected redirect to {:action=>\"somewhere_else\"}, got redirect to \"http://test.host/redirect_spec/somewhere\"")
143 end
144
145 it "redirected to incorrect path with leading /" do
146 get 'action_with_redirect_to_somewhere'
147 lambda {
148 response.should redirect_to('/redirect_spec/somewhere_else')
149 }.should fail_with('expected redirect to "/redirect_spec/somewhere_else", got redirect to "http://test.host/redirect_spec/somewhere"')
150 end
151
152 it "redirected to incorrect path without leading /" do
153 get 'action_with_redirect_to_somewhere'
154 lambda {
155 response.should redirect_to('redirect_spec/somewhere_else')
156 }.should fail_with('expected redirect to "redirect_spec/somewhere_else", got redirect to "http://test.host/redirect_spec/somewhere"')
157 end
158
159 it "redirected to incorrect internal URL (based on the action)" do
160 get 'action_with_redirect_to_somewhere'
161 lambda {
162 response.should redirect_to("http://test.host/redirect_spec/somewhere_else")
163 }.should fail_with('expected redirect to "http://test.host/redirect_spec/somewhere_else", got redirect to "http://test.host/redirect_spec/somewhere"')
164 end
165
166 it "redirected to wrong external URL" do
167 get 'action_with_redirect_to_rspec_site'
168 lambda {
169 response.should redirect_to("http://test.unit.rubyforge.org")
170 }.should fail_with('expected redirect to "http://test.unit.rubyforge.org", got redirect to "http://rspec.rubyforge.org"')
171 end
172
173 it "redirected to incorrect internal URL (based on the directory path)" do
174 get 'action_with_redirect_to_somewhere'
175 lambda {
176 response.should redirect_to("http://test.host/non_existent_controller/somewhere")
177 }.should fail_with('expected redirect to "http://test.host/non_existent_controller/somewhere", got redirect to "http://test.host/redirect_spec/somewhere"')
178 end
179
180 it "expected redirect :back, but redirected to a new URL" do
181 get 'action_with_no_redirect'
182 lambda {
183 response.should redirect_to(:back)
184 }.should fail_with('expected redirect to :back, got no redirect')
185 end
186
187 it "no redirect at all" do
188 get 'action_with_no_redirect'
189 lambda {
190 response.should redirect_to(:action => 'nowhere')
191 }.should fail_with("expected redirect to {:action=>\"nowhere\"}, got no redirect")
192 end
193
194 it "redirected to an internal URL which is unroutable and matched via a hash" do
195 get "action_with_redirect_to_unroutable_url_inside_app"
196 route = {:controller => "nonexistant", :action => "none"}
197 lambda {
198 response.should redirect_to(route)
199 }.should raise_error(ActionController::RoutingError, /(no route found to match|No route matches) \"\/nonexistant\/none\" with \{\}/)
200 end
201
202 end
203end