Skip to content

Commit

Permalink
Added check for CONTENT_TYPE header that has to be present for wrappi…
Browse files Browse the repository at this point in the history
…ng to work
  • Loading branch information
alex-rogachev committed Jul 19, 2024
1 parent 6035ae3 commit cc2162f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 52 deletions.
23 changes: 13 additions & 10 deletions lib/rage/controller/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,20 @@ def __register_action(action)

wrap_parameters_chunk = if __wrap_parameters_key
<<~RUBY
options = self.class.__wrap_parameters_options
wrapped_params = if options[:include]
@__params.slice(*[options[:include]].flatten)
elsif options[:exclude]
@__params.except(*[options[:exclude]].flatten)
else
@__params
wrap_key = self.class.__wrap_parameters_key
if !@__params.key?(wrap_key) && @__env['CONTENT_TYPE']
wrap_options = self.class.__wrap_parameters_options
wrapped_params = if wrap_options[:include]
@__params.slice(*[wrap_options[:include]].flatten)
elsif wrap_options[:exclude]
@__params.except(*[wrap_options[:exclude]].flatten)
else
@__params
end
@__params = @__params.merge({wrap_key => wrapped_params})
end
@__params = @__params.merge({self.class.__wrap_parameters_key => wrapped_params})
RUBY
end

Expand Down
124 changes: 82 additions & 42 deletions spec/controller/api/wrap_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ def index
initial_params = {param: :value}
expected_result = {param: :value}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end

Expand All @@ -31,24 +30,40 @@ def index
end

context "and wrapping root doesn't conflict with parameter key" do
it 'wraps the parameters into a nested hash' do
initial_params = {param: :value}
expected_result = {param: :value, root: {param: :value}}
context 'and CONTENT_TYPE header is blank' do
it "doesn't wrap the parameters into a nested hash" do
initial_params = {param: :value}
expected_result = {param: :value}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params)
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end

context 'and CONTENT_TYPE header is present' do
it 'wraps the parameters into a nested hash' do
initial_params = {param: :value}
expected_result = {param: :value, root: {param: :value}}

response = run_action(
controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end

context 'and wrapping root conflicts with parameter key' do
it 'wraps the parameters into a nested hash and overrides the conflicting key' do
it "doesn't wrap the parameters into a nested hash" do
initial_params = {root: :value, param: :value}
expected_result = {root: {root: :value, param: :value}, param: :value}
expected_result = {root: :value, param: :value}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand All @@ -69,9 +84,8 @@ def index
initial_params = {param_a: :value, param_b: :value}
expected_result = {param_a: :value, param_b: :value, root: {param_a: :value}}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end

Expand All @@ -95,9 +109,8 @@ def index
root: {param_a: :value, param_b: :value}
}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand All @@ -118,9 +131,8 @@ def index
initial_params = {param_a: :value, param_b: :value}
expected_result = {param_a: :value, param_b: :value, root: {param_b: :value}}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end

Expand All @@ -139,9 +151,8 @@ def index
initial_params = {param_a: :value, param_b: :value, param_c: :value}
expected_result = {param_a: :value, param_b: :value, param_c: :value, root: {param_c: :value}}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand All @@ -161,9 +172,8 @@ def index
initial_params = {param_a: :value, param_b: :value}
expected_result = {param_a: :value, param_b: :value, root: {param_a: :value}}

expect(run_action(controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(controller, :index, params: initial_params, env: {'CONTENT_TYPE' => "application/json"})
expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand Down Expand Up @@ -210,15 +220,25 @@ def index
end

it 'wraps params of child controller using wrapping key of child controller without options' do
expect(run_action(child_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
response = run_action(
child_controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end

it 'wraps params of grandchild controller using wrapping key of child controller without options' do
expect(run_action(grandchild_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
response = run_action(
grandchild_controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end

Expand All @@ -237,15 +257,25 @@ def index
let(:expected_result) { {parent_param: :value, child_param: :value, child_root: {child_param: :value}} }

it 'wraps params of child controller using wrapping key and options of child controller' do
expect(run_action(child_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
response = run_action(
child_controller,
:index,
params: initial_params,
env: { 'CONTENT_TYPE' => "application/json" }
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end

it 'wraps params of grandchild controller using wrapping key and options of child controller' do
expect(run_action(grandchild_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
response = run_action(
grandchild_controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand All @@ -263,15 +293,25 @@ def index
let(:expected_result) { {parent_param: :value, child_param: :value, parent_root: {parent_param: :value}} }

it 'wraps params of child controller using wrapping key and options of parent controller' do
expect(run_action(child_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
)
response = run_action(
child_controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end

it 'wraps params of grandchild controller using wrapping key and options of parent controller' do
expect(run_action(child_controller, :index, params: initial_params)).to match(
[200, instance_of(Hash), [expected_result.to_json]]
response = run_action(
child_controller,
:index,
params: initial_params,
env: {'CONTENT_TYPE' => "application/json"}
)

expect(response).to match([200, instance_of(Hash), [expected_result.to_json]])
end
end
end
Expand Down

0 comments on commit cc2162f

Please sign in to comment.