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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
require "abstract_unit"
class JsonParamsParsingTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
class << self
attr_accessor :last_request_parameters
end
def parse
self.class.last_request_parameters = request.request_parameters
head :ok
end
end
def teardown
TestController.last_request_parameters = nil
end
test "parses json params for application json" do
assert_parses(
{ "person" => { "name" => "David" } },
"{\"person\": {\"name\": \"David\"}}", "CONTENT_TYPE" => "application/json"
)
end
test "parses boolean and number json params for application json" do
assert_parses(
{ "item" => { "enabled" => false, "count" => 10 } },
"{\"item\": {\"enabled\": false, \"count\": 10}}", "CONTENT_TYPE" => "application/json"
)
end
test "parses json params for application jsonrequest" do
assert_parses(
{ "person" => { "name" => "David" } },
"{\"person\": {\"name\": \"David\"}}", "CONTENT_TYPE" => "application/jsonrequest"
)
end
test "does not parse unregistered media types such as application/vnd.api+json" do
assert_parses(
{},
"{\"person\": {\"name\": \"David\"}}", "CONTENT_TYPE" => "application/vnd.api+json"
)
end
test "nils are stripped from collections" do
assert_parses(
{ "person" => [] },
"{\"person\":[null]}", "CONTENT_TYPE" => "application/json"
)
assert_parses(
{ "person" => ["foo"] },
"{\"person\":[\"foo\",null]}", "CONTENT_TYPE" => "application/json"
)
assert_parses(
{ "person" => [] },
"{\"person\":[null, null]}", "CONTENT_TYPE" => "application/json"
)
end
test "logs error if parsing unsuccessful" do
with_test_routing do
output = StringIO.new
json = "[\"person]\": {\"name\": \"David\"}}"
post "/parse", params: json, headers: { "CONTENT_TYPE" => "application/json", "action_dispatch.show_exceptions" => true, "action_dispatch.logger" => ActiveSupport::Logger.new(output) }
assert_response :bad_request
output.rewind && err = output.read
assert err =~ /Error occurred while parsing request parameters/
end
end
test "occurring a parse error if parsing unsuccessful" do
with_test_routing do
begin
$stderr = StringIO.new # suppress the log
json = "[\"person]\": {\"name\": \"David\"}}"
exception = assert_raise(ActionDispatch::Http::Parameters::ParseError) do
post "/parse", params: json, headers: { "CONTENT_TYPE" => "application/json", "action_dispatch.show_exceptions" => false }
end
assert_equal JSON::ParserError, exception.cause.class
assert_equal exception.cause.message, exception.message
ensure
$stderr = STDERR
end
end
end
test "raw_post is not empty for JSON request" do
with_test_routing do
post "/parse", params: '{"posts": [{"title": "Post Title"}]}', headers: { "CONTENT_TYPE" => "application/json" }
assert_equal '{"posts": [{"title": "Post Title"}]}', request.raw_post
end
end
private
def assert_parses(expected, actual, headers = {})
with_test_routing do
post "/parse", params: actual, headers: headers
assert_response :ok
assert_equal(expected, TestController.last_request_parameters)
end
end
def with_test_routing
with_routing do |set|
set.draw do
ActiveSupport::Deprecation.silence do
post ":action", to: ::JsonParamsParsingTest::TestController
end
end
yield
end
end
end
class RootLessJSONParamsParsingTest < ActionDispatch::IntegrationTest
class UsersController < ActionController::Base
wrap_parameters format: :json
class << self
attr_accessor :last_request_parameters, :last_parameters
end
def parse
self.class.last_request_parameters = request.request_parameters
self.class.last_parameters = params.to_unsafe_h
head :ok
end
end
def teardown
UsersController.last_request_parameters = nil
end
test "parses json params for application json" do
assert_parses(
{ "user" => { "username" => "sikachu" }, "username" => "sikachu" },
"{\"username\": \"sikachu\"}", "CONTENT_TYPE" => "application/json"
)
end
test "parses json params for application jsonrequest" do
assert_parses(
{ "user" => { "username" => "sikachu" }, "username" => "sikachu" },
"{\"username\": \"sikachu\"}", "CONTENT_TYPE" => "application/jsonrequest"
)
end
test "parses json with non-object JSON content" do
assert_parses(
{ "user" => { "_json" => "string content" }, "_json" => "string content" },
"\"string content\"", "CONTENT_TYPE" => "application/json"
)
end
test "parses json params after custom json mime type registered" do
begin
Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w(application/vnd.rails+json)
assert_parses(
{ "user" => { "username" => "meinac" }, "username" => "meinac" },
"{\"username\": \"meinac\"}", "CONTENT_TYPE" => "application/json"
)
ensure
Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
end
end
test "parses json params after custom json mime type registered with synonym" do
begin
Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w(application/vnd.rails+json)
assert_parses(
{ "user" => { "username" => "meinac" }, "username" => "meinac" },
"{\"username\": \"meinac\"}", "CONTENT_TYPE" => "application/vnd.rails+json"
)
ensure
Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w( text/x-json application/jsonrequest )
end
end
private
def assert_parses(expected, actual, headers = {})
with_test_routing(UsersController) do
post "/parse", params: actual, headers: headers
assert_response :ok
assert_equal(expected, UsersController.last_request_parameters)
assert_equal(expected.merge("action" => "parse"), UsersController.last_parameters)
end
end
def with_test_routing(controller)
with_routing do |set|
set.draw do
ActiveSupport::Deprecation.silence do
post ":action", to: controller
end
end
yield
end
end
end
|