Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Split auto-redirection testing module into its own file.

  • Loading branch information...
commit 7b3d7f3cdfa1f6b47b9fa23a9659d6d842f7c835 1 parent ad9e00f
Hongli Lai authored
152 lib/auto_redirection/testing.rb
... ... @@ -0,0 +1,152 @@
  1 +# Copyright (c) 2008 Phusion
  2 +# http://www.phusion.nl/
  3 +#
  4 +# Permission is hereby granted, free of charge, to any person obtaining a copy
  5 +# of this software and associated documentation files (the "Software"), to deal
  6 +# in the Software without restriction, including without limitation the rights
  7 +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8 +# copies of the Software, and to permit persons to whom the Software is
  9 +# furnished to do so, subject to the following conditions:
  10 +#
  11 +# The above copyright notice and this permission notice shall be included in
  12 +# all copies or substantial portions of the Software.
  13 +#
  14 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16 +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17 +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18 +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19 +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20 +# THE SOFTWARE.
  21 +
  22 +require 'uri'
  23 +
  24 +module AutoRedirection
  25 +module Testing
  26 +protected
  27 + def get(path, parameters = nil, headers = nil)
  28 + @_referer = path
  29 + super(path, parameters, headers)
  30 + end
  31 +
  32 + def post(path, parameters = nil, headers = nil)
  33 + @_referer = path
  34 + super(path, parameters, headers)
  35 + end
  36 +
  37 + def get_with_referer(path, parameters = nil, headers = nil)
  38 + if headers
  39 + headers = headers.with_indifferent_access
  40 + else
  41 + headers = HashWithIndifferentAccess.new
  42 + end
  43 + headers.reverse_merge!(:HTTP_REFERER => @_referer)
  44 + get(path, parameters, headers)
  45 + end
  46 +
  47 + def post_with_referer(path, parameters = nil, headers = nil)
  48 + if headers
  49 + headers = headers.with_indifferent_access
  50 + else
  51 + headers = HashWithIndifferentAccess.new
  52 + end
  53 + headers.reverse_merge!(:HTTP_REFERER => @_referer)
  54 + post(path, parameters, headers)
  55 + end
  56 +
  57 + def assert_came_from(path, parameters = nil)
  58 + info = @controller.send(:get_redirection_information)
  59 + if info.nil?
  60 + raise Test::Unit::AssertionFailedError, "No redirection information."
  61 + end
  62 +
  63 + assert_equal path, info.path
  64 +
  65 + if parameters
  66 + params = info.params.with_indifferent_access.dup
  67 + params.delete(:controller)
  68 + params.delete(:action)
  69 + params.delete(:_redirection_information)
  70 + assert_equal parameters.with_indifferent_access, params
  71 + end
  72 + end
  73 +
  74 + def assert_redirection_with_method(method, path, parameters = nil)
  75 + result = parse_post_redirection_page
  76 + assert_equal path, result[:path]
  77 + if result[:method] != method
  78 + raise Test::Unit::AssertionFailedError,
  79 + "<#{method}> redirection expected, but " <<
  80 + "<#{result[:method]}> redirection found."
  81 + end
  82 + if parameters
  83 + assert_equal parameters.with_indifferent_access, result[:params]
  84 + end
  85 + end
  86 +
  87 + def get_redirection_information_from_form
  88 + hidden_field = css_select("input[name='_redirection_information']").first
  89 + if !hidden_field
  90 + raise Test::Unit::AssertionFailedError,
  91 + "There is no redirection information inside the form."
  92 + end
  93 + return hidden_field['value']
  94 + end
  95 +
  96 + def follow_redirection_with_method!
  97 + result = parse_post_redirection_page
  98 + params = result[:params].merge(:_redirection_information => result[:redirection_data])
  99 + case result[:method]
  100 + when :post
  101 + post(result[:path], params)
  102 + when :put
  103 + put(result[:path], params)
  104 + when :delete
  105 + delete(result[:path], params)
  106 + else
  107 + raise Test::Unit::AssertionFailedError,
  108 + "Unknown method '#{result[:method]}'."
  109 + end
  110 + end
  111 +
  112 +private
  113 + def parse_query_string(str)
  114 + return ActionController::AbstractRequest.parse_query_parameters(str)
  115 + end
  116 +
  117 + def parse_post_redirection_page
  118 + assert_response :ok, 'Controller did not render a redirection page.'
  119 + if css_select('#_auto_redirection_form').empty?
  120 + raise Test::Unit::AssertionFailedError,
  121 + 'Controller did not render a redirection page.'
  122 + end
  123 +
  124 + form = css_select('#_auto_redirection_form').first
  125 + if !form
  126 + raise Test::Unit::AssertionFailedError,
  127 + 'Controller did not render a redirection page.'
  128 + end
  129 +
  130 + result = {}
  131 + uri = URI.parse(CGI::unescapeHTML(form['action']))
  132 + result[:path] = uri.path
  133 +
  134 + method_field = css_select("#_auto_redirection_form input[name='_method']").first
  135 + if method_field
  136 + result[:method] = method_field['value'].to_sym
  137 + else
  138 + result[:method] = :post
  139 + end
  140 +
  141 + params = parse_query_string(uri.query || "")
  142 + result[:params] = params.with_indifferent_access
  143 +
  144 + redirection_info_field = css_select("#_auto_redirection_form input[name='_redirection_information']").first
  145 + if redirection_info_field
  146 + result[:redirection_data] = redirection_info_field['value']
  147 + end
  148 +
  149 + return result
  150 + end
  151 +end
  152 +end # AutoRedirection
1  test/abstract_unit.rb
@@ -11,6 +11,7 @@
11 11 require 'action_view/test_case'
12 12
13 13 require 'auto_redirection'
  14 +require 'auto_redirection/testing'
14 15
15 16 # Show backtraces for deprecated behavior for quicker cleanup.
16 17 ActiveSupport::Deprecation.debug = true
132 test/test_methods_test.rb
... ... @@ -1,139 +1,7 @@
1 1 require File.dirname(__FILE__) + '/abstract_unit'
2 2 require File.dirname(__FILE__) + '/book_store'
3   -require 'uri'
4   -
5   -module AutoRedirection
6   -module Testing
7   -protected
8   - def get(path, parameters = nil, headers = nil)
9   - @_referer = path
10   - super(path, parameters, headers)
11   - end
12   -
13   - def post(path, parameters = nil, headers = nil)
14   - @_referer = path
15   - super(path, parameters, headers)
16   - end
17   -
18   - def get_with_referer(path, parameters = nil, headers = nil)
19   - if headers
20   - headers = headers.with_indifferent_access
21   - else
22   - headers = HashWithIndifferentAccess.new
23   - end
24   - headers.reverse_merge!(:HTTP_REFERER => @_referer)
25   - get(path, parameters, headers)
26   - end
27   -
28   - def post_with_referer(path, parameters = nil, headers = nil)
29   - if headers
30   - headers = headers.with_indifferent_access
31   - else
32   - headers = HashWithIndifferentAccess.new
33   - end
34   - headers.reverse_merge!(:HTTP_REFERER => @_referer)
35   - post(path, parameters, headers)
36   - end
37   -
38   - def assert_came_from(path, parameters = nil)
39   - info = @controller.send(:get_redirection_information)
40   - if info.nil?
41   - raise Test::Unit::AssertionFailedError, "No redirection information."
42   - end
43   -
44   - assert_equal path, info.path
45   -
46   - if parameters
47   - params = info.params.with_indifferent_access.dup
48   - params.delete(:controller)
49   - params.delete(:action)
50   - params.delete(:_redirection_information)
51   - assert_equal parameters.with_indifferent_access, params
52   - end
53   - end
54   -
55   - def assert_redirection_with_method(method, path, parameters = nil)
56   - result = parse_post_redirection_page
57   - assert_equal path, result[:path]
58   - if result[:method] != method
59   - raise Test::Unit::AssertionFailedError,
60   - "<#{method}> redirection expected, but " <<
61   - "<#{result[:method]}> redirection found."
62   - end
63   - if parameters
64   - assert_equal parameters.with_indifferent_access, result[:params]
65   - end
66   - end
67   -
68   - def get_redirection_information_from_form
69   - hidden_field = css_select("input[name='_redirection_information']").first
70   - if !hidden_field
71   - raise Test::Unit::AssertionFailedError,
72   - "There is no redirection information inside the form."
73   - end
74   - return hidden_field['value']
75   - end
76   -
77   - def follow_redirection_with_method!
78   - result = parse_post_redirection_page
79   - params = result[:params].merge(:_redirection_information => result[:redirection_data])
80   - case result[:method]
81   - when :post
82   - post(result[:path], params)
83   - when :put
84   - put(result[:path], params)
85   - when :delete
86   - delete(result[:path], params)
87   - else
88   - raise Test::Unit::AssertionFailedError,
89   - "Unknown method '#{result[:method]}'."
90   - end
91   - end
92   -
93   -private
94   - def parse_query_string(str)
95   - return ActionController::AbstractRequest.parse_query_parameters(str)
96   - end
97   -
98   - def parse_post_redirection_page
99   - assert_response :ok, 'Controller did not render a redirection page.'
100   - if css_select('#_auto_redirection_form').empty?
101   - raise Test::Unit::AssertionFailedError,
102   - 'Controller did not render a redirection page.'
103   - end
104   -
105   - form = css_select('#_auto_redirection_form').first
106   - if !form
107   - raise Test::Unit::AssertionFailedError,
108   - 'Controller did not render a redirection page.'
109   - end
110   -
111   - result = {}
112   - uri = URI.parse(CGI::unescapeHTML(form['action']))
113   - result[:path] = uri.path
114   -
115   - method_field = css_select("#_auto_redirection_form input[name='_method']").first
116   - if method_field
117   - result[:method] = method_field['value'].to_sym
118   - else
119   - result[:method] = :post
120   - end
121   -
122   - params = parse_query_string(uri.query || "")
123   - result[:params] = params.with_indifferent_access
124   -
125   - redirection_info_field = css_select("#_auto_redirection_form input[name='_redirection_information']").first
126   - if redirection_info_field
127   - result[:redirection_data] = redirection_info_field['value']
128   - end
129   -
130   - return result
131   - end
132   -end
133   -end
134 3
135 4 class TestMethodsTest < ActionController::IntegrationTest
136   - include AutoRedirection
137 5 include TestHelpers
138 6 include AutoRedirection::Testing
139 7

0 comments on commit 7b3d7f3

Please sign in to comment.
Something went wrong with that request. Please try again.