~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/controller/webservice_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'abstract_unit'
2
 
 
3
 
class WebServiceTest < ActionController::IntegrationTest
4
 
  class TestController < ActionController::Base
5
 
    def assign_parameters
6
 
      if params[:full]
7
 
        render :text => dump_params_keys
8
 
      else
9
 
        render :text => (params.keys - ['controller', 'action']).sort.join(", ")
10
 
      end
11
 
    end
12
 
 
13
 
    def dump_params_keys(hash = params)
14
 
      hash.keys.sort.inject("") do |s, k|
15
 
        value = hash[k]
16
 
        value = Hash === value ? "(#{dump_params_keys(value)})" : ""
17
 
        s << ", " unless s.empty?
18
 
        s << "#{k}#{value}"
19
 
      end
20
 
    end
21
 
 
22
 
    def rescue_action(e) raise end
23
 
  end
24
 
 
25
 
  def setup
26
 
    @controller = TestController.new
27
 
    @default_param_parsers = ActionController::Base.param_parsers.dup
28
 
  end
29
 
 
30
 
  def teardown
31
 
    ActionController::Base.param_parsers = @default_param_parsers
32
 
  end
33
 
 
34
 
  def test_check_parameters
35
 
    with_test_route_set do
36
 
      get "/"
37
 
      assert_equal '', @controller.response.body
38
 
    end
39
 
  end
40
 
 
41
 
  def test_post_xml
42
 
    with_test_route_set do
43
 
      post "/", '<entry attributed="true"><summary>content...</summary></entry>',
44
 
        {'CONTENT_TYPE' => 'application/xml'}
45
 
 
46
 
      assert_equal 'entry', @controller.response.body
47
 
      assert @controller.params.has_key?(:entry)
48
 
      assert_equal 'content...', @controller.params["entry"]['summary']
49
 
      assert_equal 'true', @controller.params["entry"]['attributed']
50
 
    end
51
 
  end
52
 
 
53
 
  def test_put_xml
54
 
    with_test_route_set do
55
 
      put "/", '<entry attributed="true"><summary>content...</summary></entry>',
56
 
        {'CONTENT_TYPE' => 'application/xml'}
57
 
 
58
 
      assert_equal 'entry', @controller.response.body
59
 
      assert @controller.params.has_key?(:entry)
60
 
      assert_equal 'content...', @controller.params["entry"]['summary']
61
 
      assert_equal 'true', @controller.params["entry"]['attributed']
62
 
    end
63
 
  end
64
 
 
65
 
  def test_put_xml_using_a_type_node
66
 
    with_test_route_set do
67
 
      put "/", '<type attributed="true"><summary>content...</summary></type>',
68
 
        {'CONTENT_TYPE' => 'application/xml'}
69
 
 
70
 
      assert_equal 'type', @controller.response.body
71
 
      assert @controller.params.has_key?(:type)
72
 
      assert_equal 'content...', @controller.params["type"]['summary']
73
 
      assert_equal 'true', @controller.params["type"]['attributed']
74
 
    end
75
 
  end
76
 
 
77
 
  def test_put_xml_using_a_type_node_and_attribute
78
 
    with_test_route_set do
79
 
      put "/", '<type attributed="true"><summary type="boolean">false</summary></type>',
80
 
        {'CONTENT_TYPE' => 'application/xml'}
81
 
 
82
 
      assert_equal 'type', @controller.response.body
83
 
      assert @controller.params.has_key?(:type)
84
 
      assert_equal false, @controller.params["type"]['summary']
85
 
      assert_equal 'true', @controller.params["type"]['attributed']
86
 
    end
87
 
  end
88
 
 
89
 
  def test_post_xml_using_a_type_node
90
 
    with_test_route_set do
91
 
      post "/", '<font attributed="true"><type>arial</type></font>',
92
 
        {'CONTENT_TYPE' => 'application/xml'}
93
 
 
94
 
      assert_equal 'font', @controller.response.body
95
 
      assert @controller.params.has_key?(:font)
96
 
      assert_equal 'arial', @controller.params['font']['type']
97
 
      assert_equal 'true', @controller.params["font"]['attributed']
98
 
    end
99
 
  end
100
 
 
101
 
  def test_post_xml_using_a_root_node_named_type
102
 
    with_test_route_set do
103
 
      post "/", '<type type="integer">33</type>',
104
 
        {'CONTENT_TYPE' => 'application/xml'}
105
 
 
106
 
      assert @controller.params.has_key?(:type)
107
 
      assert_equal 33, @controller.params['type']
108
 
    end
109
 
  end
110
 
 
111
 
  def test_post_xml_using_an_attributted_node_named_type
112
 
    with_test_route_set do
113
 
      ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
114
 
      post "/", '<request><type type="string">Arial,12</type><z>3</z></request>',
115
 
        {'CONTENT_TYPE' => 'application/xml'}
116
 
 
117
 
      assert_equal 'type, z', @controller.response.body
118
 
      assert @controller.params.has_key?(:type)
119
 
      assert_equal 'Arial,12', @controller.params['type'], @controller.params.inspect
120
 
      assert_equal '3', @controller.params['z'], @controller.params.inspect
121
 
    end
122
 
  end
123
 
 
124
 
  def test_register_and_use_yaml
125
 
    with_test_route_set do
126
 
      ActionController::Base.param_parsers[Mime::YAML] = Proc.new { |d| YAML.load(d) }
127
 
      post "/", {"entry" => "loaded from yaml"}.to_yaml,
128
 
        {'CONTENT_TYPE' => 'application/x-yaml'}
129
 
 
130
 
      assert_equal 'entry', @controller.response.body
131
 
      assert @controller.params.has_key?(:entry)
132
 
      assert_equal 'loaded from yaml', @controller.params["entry"]
133
 
    end
134
 
  end
135
 
 
136
 
  def test_register_and_use_yaml_as_symbol
137
 
    with_test_route_set do
138
 
      ActionController::Base.param_parsers[Mime::YAML] = :yaml
139
 
      post "/", {"entry" => "loaded from yaml"}.to_yaml,
140
 
        {'CONTENT_TYPE' => 'application/x-yaml'}
141
 
 
142
 
      assert_equal 'entry', @controller.response.body
143
 
      assert @controller.params.has_key?(:entry)
144
 
      assert_equal 'loaded from yaml', @controller.params["entry"]
145
 
    end
146
 
  end
147
 
 
148
 
  def test_register_and_use_xml_simple
149
 
    with_test_route_set do
150
 
      ActionController::Base.param_parsers[Mime::XML] = Proc.new { |data| Hash.from_xml(data)['request'].with_indifferent_access }
151
 
      post "/", '<request><summary>content...</summary><title>SimpleXml</title></request>',
152
 
        {'CONTENT_TYPE' => 'application/xml'}
153
 
 
154
 
      assert_equal 'summary, title', @controller.response.body
155
 
      assert @controller.params.has_key?(:summary)
156
 
      assert @controller.params.has_key?(:title)
157
 
      assert_equal 'content...', @controller.params["summary"]
158
 
      assert_equal 'SimpleXml', @controller.params["title"]
159
 
    end
160
 
  end
161
 
 
162
 
  def test_use_xml_ximple_with_empty_request
163
 
    with_test_route_set do
164
 
      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
165
 
      assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
166
 
      assert_equal "", @controller.response.body
167
 
    end
168
 
  end
169
 
 
170
 
  def test_dasherized_keys_as_xml
171
 
    with_test_route_set do
172
 
      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
173
 
      post "/?full=1", "<first-key>\n<sub-key>...</sub-key>\n</first-key>",
174
 
        {'CONTENT_TYPE' => 'application/xml'}
175
 
      assert_equal 'action, controller, first_key(sub_key), full', @controller.response.body
176
 
      assert_equal "...", @controller.params[:first_key][:sub_key]
177
 
    end
178
 
  end
179
 
 
180
 
  def test_typecast_as_xml
181
 
    with_test_route_set do
182
 
      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
183
 
      xml = <<-XML
184
 
        <data>
185
 
          <a type="integer">15</a>
186
 
          <b type="boolean">false</b>
187
 
          <c type="boolean">true</c>
188
 
          <d type="date">2005-03-17</d>
189
 
          <e type="datetime">2005-03-17T21:41:07Z</e>
190
 
          <f>unparsed</f>
191
 
          <g type="integer">1</g>
192
 
          <g>hello</g>
193
 
          <g type="date">1974-07-25</g>
194
 
        </data>
195
 
      XML
196
 
      post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
197
 
 
198
 
      params = @controller.params
199
 
      assert_equal 15, params[:data][:a]
200
 
      assert_equal false, params[:data][:b]
201
 
      assert_equal true, params[:data][:c]
202
 
      assert_equal Date.new(2005,3,17), params[:data][:d]
203
 
      assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
204
 
      assert_equal "unparsed", params[:data][:f]
205
 
      assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
206
 
    end
207
 
  end
208
 
 
209
 
  def test_entities_unescaped_as_xml_simple
210
 
    with_test_route_set do
211
 
      ActionController::Base.param_parsers[Mime::XML] = :xml_simple
212
 
      xml = <<-XML
213
 
        <data>&lt;foo &quot;bar&apos;s&quot; &amp; friends&gt;</data>
214
 
      XML
215
 
      post "/", xml, {'CONTENT_TYPE' => 'application/xml'}
216
 
      assert_equal %(<foo "bar's" & friends>), @controller.params[:data]
217
 
    end
218
 
  end
219
 
 
220
 
  def test_typecast_as_yaml
221
 
    with_test_route_set do
222
 
      ActionController::Base.param_parsers[Mime::YAML] = :yaml
223
 
      yaml = <<-YAML
224
 
        ---
225
 
        data:
226
 
          a: 15
227
 
          b: false
228
 
          c: true
229
 
          d: 2005-03-17
230
 
          e: 2005-03-17T21:41:07Z
231
 
          f: unparsed
232
 
          g:
233
 
            - 1
234
 
            - hello
235
 
            - 1974-07-25
236
 
      YAML
237
 
      post "/", yaml, {'CONTENT_TYPE' => 'application/x-yaml'}
238
 
      params = @controller.params
239
 
      assert_equal 15, params[:data][:a]
240
 
      assert_equal false, params[:data][:b]
241
 
      assert_equal true, params[:data][:c]
242
 
      assert_equal Date.new(2005,3,17), params[:data][:d]
243
 
      assert_equal Time.utc(2005,3,17,21,41,7), params[:data][:e]
244
 
      assert_equal "unparsed", params[:data][:f]
245
 
      assert_equal [1, "hello", Date.new(1974,7,25)], params[:data][:g]
246
 
    end
247
 
  end
248
 
 
249
 
  private
250
 
    def with_test_route_set
251
 
      with_routing do |set|
252
 
        set.draw do |map|
253
 
          map.with_options :controller => "web_service_test/test" do |c|
254
 
            c.connect "/", :action => "assign_parameters"
255
 
          end
256
 
        end
257
 
        yield
258
 
      end
259
 
    end
260
 
end