From bdf2ed642ceaae1ea34e1ef6abfaa259ef9ed5a7 Mon Sep 17 00:00:00 2001 From: wout Date: Sun, 17 May 2020 11:59:05 +0100 Subject: [PATCH] Strip leading and trailing whitespace from params.get, add get_raw. --- spec/lucky/params_spec.cr | 69 +++++++++++++++++++++++++++++++-------- src/lucky/params.cr | 39 +++++++++++++++++++--- 2 files changed, 89 insertions(+), 19 deletions(-) diff --git a/spec/lucky/params_spec.cr b/spec/lucky/params_spec.cr index 1192cf2d4..f42f1f2b0 100644 --- a/spec/lucky/params_spec.cr +++ b/spec/lucky/params_spec.cr @@ -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= paul@luckyframework.org &name= Paul " + + params = Lucky::Params.new(request) + + params.get?(:email).should eq "paul@luckyframework.org" + 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 @@ -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 @@ -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 @@ -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 @@ -180,8 +210,8 @@ 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 @@ -189,25 +219,36 @@ describe Lucky::Params do 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= paul@luckyframework.org &name= Paul &age=28 " + + params = Lucky::Params.new(request) + + params.get_raw?(:email).should eq " paul@luckyframework.org " + params.get_raw?(:name).should eq " Paul " + params.get_raw?(:age).should eq "28 " end end diff --git a/src/lucky/params.cr b/src/lucky/params.cr index 4d4f52cb9..78984bdb7 100644 --- a/src/lucky/params.cr +++ b/src/lucky/params.cr @@ -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