Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip leading and trailing whitespace from params.get, add get_raw. #1144

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 55 additions & 14 deletions spec/lucky/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,44 @@ describe Lucky::Params do
end

describe "get" do
it "strips whitespace around values" do
request = build_request body: "", content_type: ""
request.query = "email= [email protected] &name= Paul "

params = Lucky::Params.new(request)

params.get?(:email).should eq "[email protected]"
params.get?(:name).should eq "Paul"
end

it "raises if missing a param and using get version" do
request = build_request body: "", content_type: "application/x-www-form-urlencoded"

params = Lucky::Params.new(request)

expect_raises Lucky::MissingParamError do
params.get(:missing)
end
end

it "returns nil if using get? version" do
request = build_request body: "", content_type: "application/x-www-form-urlencoded"

params = Lucky::Params.new(request)

params.get?(:missing).should be_nil
end
end

describe "get_raw" do
it "parses form encoded params" do
request = build_request body: "page=1&foo=bar",
content_type: "application/x-www-form-urlencoded"

params = Lucky::Params.new(request)

params.get?(:page).should eq "1"
params.get?(:foo).should eq "bar"
params.get_raw?(:page).should eq "1"
params.get_raw?(:foo).should eq "bar"
end

it "parses JSON params" do
Expand All @@ -150,8 +180,8 @@ describe Lucky::Params do

params = Lucky::Params.new(request)

params.get?(:page).should eq "1"
params.get?(:foo).should eq "bar"
params.get_raw?(:page).should eq "1"
params.get_raw?(:foo).should eq "bar"
end

it "handles empty JSON body" do
Expand All @@ -161,7 +191,7 @@ describe Lucky::Params do
params = Lucky::Params.new(request)

# Should not raise
params.get?(:anything)
params.get_raw?(:anything)
end

it "handles JSON with charset directive in Content-Type header" do
Expand All @@ -170,8 +200,8 @@ describe Lucky::Params do

params = Lucky::Params.new(request)

params.get?(:page).should eq "1"
params.get?(:foo).should eq "bar"
params.get_raw?(:page).should eq "1"
params.get_raw?(:foo).should eq "bar"
end

it "parses query params" do
Expand All @@ -180,34 +210,45 @@ describe Lucky::Params do

params = Lucky::Params.new(request)

params.get?(:page).should eq "1"
params.get?(:id).should eq "1"
params.get_raw?(:page).should eq "1"
params.get_raw?(:id).should eq "1"
end

it "parses params in multipart requests" do
request = build_multipart_request form_parts: {"from" => "multipart"}

params = Lucky::Params.new(request)

params.get(:from).should eq "multipart"
params.get_raw(:from).should eq "multipart"
end

it "raises if missing a param and using get! version" do
it "raises if missing a param and using get_raw version" do
request = build_request body: "", content_type: "application/x-www-form-urlencoded"

params = Lucky::Params.new(request)

expect_raises Lucky::MissingParamError do
params.get(:missing)
params.get_raw(:missing)
end
end

it "returns nil if using get version" do
it "returns nil if using get_raw? version" do
request = build_request body: "", content_type: "application/x-www-form-urlencoded"

params = Lucky::Params.new(request)

params.get?(:missing).should be_nil
params.get_raw?(:missing).should be_nil
end

it "does not strip whitespace around values" do
request = build_request body: "", content_type: ""
request.query = "email= [email protected] &name= Paul &age=28 "

params = Lucky::Params.new(request)

params.get_raw?(:email).should eq " [email protected] "
params.get_raw?(:name).should eq " Paul "
params.get_raw?(:age).should eq "28 "
end
end

Expand Down
39 changes: 34 additions & 5 deletions src/lucky/params.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,54 @@ class Lucky::Params
parse_multipart_request
end

# Retrieve a value from the params hash, raise if key is absent
# Retrieve a trimmed value from the params hash, raise if key is absent
#
# If no key is found a `Lucky::MissingParamError` will be raised:
#
# ```crystal
# params.get("page") # 1 : String
# params.get("name") # "Paul" : String
# params.get("page") # "1" : String
# params.get("missing") # Missing parameter: missing
# ```
def get(key) : String
get?(key) || raise Lucky::MissingParamError.new(key.to_s)
get_raw(key).strip
end

# Retrieve a value from the params hash, return nil if key is absent
# Retrieve a trimmed value from the params hash, return nil if key is absent
#
# ```crystal
# params.get?("missing") # nil : (String | Nil)
# params.get?("page") # 1 : (String | Nil)
# params.get?("page") # "1" : (String | Nil)
# params.get?("name") # "Paul" : (String | Nil)
# ```
def get?(key : String | Symbol) : String?
if value = get_raw?(key)
value.strip
end
end

# Retrieve a raw, untrimmed value from the params hash, raise if key is absent
#
# If no key is found a `Lucky::MissingParamError` will be raised:
#
# ```crystal
# params.get_raw("name") # " Paul " : String
# params.get_raw("page") # "1" : String
# params.get_raw("missing") # Missing parameter: missing
# ```
def get_raw(key) : String
get_raw?(key) || raise Lucky::MissingParamError.new(key.to_s)
end

# Retrieve a raw, untrimmed value from the params hash, return nil if key is
# absent
#
# ```crystal
# params.get_raw?("missing") # nil : (String | Nil)
# params.get_raw?("page") # "1" : (String | Nil)
# params.get_raw?("name") # " Paul " : (String | Nil)
# ```
def get_raw?(key : String | Symbol) : String?
route_params[key.to_s]? || body_param(key.to_s) || query_params[key.to_s]?
end

Expand Down