Skip to content

Commit 12d55b6

Browse files
authored
Fix failed specs in Crystal v1.13 (#1909)
* Fix invalid query string for array params * Add spec expectation * Add check for correctness of values returned from array query params * Fix failed specs in Crystal v1.13 ``` Failures: 1) Lucky::CookieJar raises a nicer error for invalid cookie values Failure/Error: expect_raises(Lucky::InvalidCookieValueError, "Cookie value for 'cookie' is invalid") do Expected Lucky::InvalidCookieValueError but nothing was raised Error: # spec/lucky/cookies/cookie_jar_spec.cr:41 ``` Crystal v1.13 accepts space in cookie values as valid. See <crystal-lang/crystal#14455>.
1 parent 157ec4f commit 12d55b6

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

spec/lucky/base_http_client_spec.cr

+20
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,23 @@ require "../spec_helper"
33
class HelloWorldAction < TestAction
44
accepted_formats [:plain_text]
55

6+
param codes : Array(String)?
7+
68
post "/hello" do
79
plain_text "world"
810
end
911
end
1012

13+
class ArrayParamAction < TestAction
14+
accepted_formats [:plain_text]
15+
16+
param codes : Array(String)
17+
18+
post "/array_param" do
19+
plain_text codes.join("--")
20+
end
21+
end
22+
1123
class MyClient < Lucky::BaseHTTPClient
1224
app TestServer.new
1325
end
@@ -46,6 +58,14 @@ describe Lucky::BaseHTTPClient do
4658
request = TestServer.last_request
4759
request.body.to_s.should eq({foo: "bar"}.to_json)
4860
end
61+
62+
it "works with array query params" do
63+
response = MyClient.new.exec ArrayParamAction.with(codes: ["ab", "xy"])
64+
response.body.should eq "ab--xy"
65+
66+
request = TestServer.last_request
67+
request.query.should eq("codes%5B%5D=ab&codes%5B%5D=xy")
68+
end
4969
end
5070

5171
describe "with a Lucky::RouteHelper" do

spec/lucky/cookies/cookie_jar_spec.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe Lucky::CookieJar do
3535
end
3636

3737
it "raises a nicer error for invalid cookie values" do
38-
value = "Double Chocolate"
38+
value = "Double,Chocolate"
3939
jar = Lucky::CookieJar.empty_jar
4040

4141
expect_raises(Lucky::InvalidCookieValueError, "Cookie value for 'cookie' is invalid") do

src/lucky/routable.cr

+17-11
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,25 @@ module Lucky::Routable
298298
{{ param.gsub(/^\?:/, "").id }},
299299
{% end %}
300300
)
301-
query_params = {} of String => String
302-
{% for param in PARAM_DECLARATIONS %}
303-
# add query param if given and not nil
304-
query_params["{{ param.var }}"] = {{ param.var }}.to_s unless {{ param.var }}.nil?
305-
{% end %}
301+
302+
query_params = URI::Params.build do |builder|
303+
{% for param in PARAM_DECLARATIONS %}
304+
_param = {{ param.var }}
305+
306+
# add query param if given and not nil
307+
unless _param.nil?
308+
if _param.is_a?(Array)
309+
builder.add("{{ param.var }}[]", _param.map(&.to_s))
310+
else
311+
builder.add("{{ param.var }}", _param.to_s)
312+
end
313+
end
314+
{% end %}
315+
end
316+
306317
unless query_params.empty?
307318
io << '?'
308-
{% if compare_versions(Crystal::VERSION, "1.10.0") < 0 %}
309-
{% @type.warning("[Deprecated] Please update your Crystal version #{Crystal::VERSION}. Using Lucky with a version below 1.10.0 is deprecated.") %}
310-
io << HTTP::Params.encode(query_params)
311-
{% else %}
312-
HTTP::Params.encode(io, query_params)
313-
{% end %}
319+
io << query_params
314320
end
315321

316322
anchor.try do |value|

0 commit comments

Comments
 (0)