Skip to content

Commit

Permalink
fix support for fetch_all in form params (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joakim Reinert authored and eliasjpr committed Mar 18, 2018
1 parent bc28e64 commit ef8cf75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 21 additions & 1 deletion spec/amber/router/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,28 @@ module Amber::Router
end

describe "#fetch_all" do
fetch_all_headers = HTTP::Headers.new
fetch_all_headers["Content-Type"] = "application/x-www-form-urlencoded"
encoded_form = HTTP::Params.build do |form|
form.add "test_form", "test1"
form.add "test_form", "test2"
form.add "test_both", "form1"
form.add "test_both", "form2"
end

fetch_all_request = HTTP::Request.new("POST", "/?test=test&test=test2&test_both=query&test_both=query1&#{HTTP::Request::METHOD}=put&status=1234", fetch_all_headers, encoded_form)
fetch_all_params = Params.new(fetch_all_request)

it "returns an array of params" do
params.fetch_all("test").should eq %w(test test2)
fetch_all_params.fetch_all("test").should eq %w(test test2)
end

it "works for form params" do
fetch_all_params.fetch_all("test_form").should eq %w(test1 test2)
end

it "prefers query params" do
fetch_all_params.fetch_all("test_both").should eq %w(query query1)
end
end

Expand Down
6 changes: 5 additions & 1 deletion src/amber/router/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ module Amber::Router

def fetch_all(key : Types::Key) : Array
_key = key.to_s
query.fetch_all(_key) || form.fetch_all(_key)
if query.has_key?(_key)
query.fetch_all(_key)
else
form.fetch_all(_key)
end
end

def json(key : Types::Key)
Expand Down

0 comments on commit ef8cf75

Please sign in to comment.