Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create a separate method decoded_get_args in elli_request

  • Loading branch information...
commit 0fdc6a2391e9ce40da824a3cca2098ecdb0c16a4 1 parent 7a2cfa7
@zucaritask zucaritask authored
View
5 src/elli_example_callback.erl
@@ -46,6 +46,11 @@ handle('GET', [<<"hello">>, <<"iolist">>], Req) ->
Name = elli_request:get_arg(<<"name">>, Req),
{ok, [], [<<"Hello ">>, Name]};
+handle('GET', [<<"decoded-hello">>], Req) ->
+ %% Fetch a URI decoded GET argument from the URL.
+ Name = elli_request:get_arg_decoded(<<"name">>, Req, <<"undefined">>),
+ {ok, [], <<"Hello ", Name/binary>>};
+
handle('GET', [<<"type">>], Req) ->
Name = elli_request:get_arg(<<"name">>, Req),
%% Fetch a header.
View
8 src/elli_request.erl
@@ -10,6 +10,8 @@
, raw_path/1
, query_str/1
, get_header/2
+ , get_arg_decoded/2
+ , get_arg_decoded/3
, get_arg/2
, get_arg/3
, get_args/1
@@ -62,6 +64,12 @@ get_arg(Key, #req{} = Req) ->
get_arg(Key, Req, undefined).
get_arg(Key, #req{args = Args}, Default) ->
+ proplists:get_value(Key, Args, Default).
+
+get_arg_decoded(Key, #req{} = Req) ->
+ get_arg(Key, Req, undefined).
+
+get_arg_decoded(Key, #req{args = Args}, Default) ->
EncodedValue = proplists:get_value(Key, Args, Default),
list_to_binary(http_uri:decode(binary_to_list(EncodedValue))).
View
6 test/elli_tests.erl
@@ -19,7 +19,7 @@ elli_test_() ->
?_test(accept_content_type()),
?_test(user_connection()),
?_test(get_args()),
- ?_test(get_encoded_args()),
+ ?_test(decoded_get_args()),
?_test(post_args()),
?_test(shorthand()),
?_test(too_many_headers()),
@@ -125,8 +125,8 @@ get_args() ->
{ok, Response} = httpc:request("http://localhost:3001/hello?name=knut"),
?assertEqual("Hello knut", body(Response)).
-get_encoded_args() ->
- {ok, Response} = httpc:request("http://localhost:3001/hello?name=knut%3D"),
+decoded_get_args() ->
+ {ok, Response} = httpc:request("http://localhost:3001/decoded-hello?name=knut%3D"),
?assertEqual("Hello knut=", body(Response)).
post_args() ->
Please sign in to comment.
Something went wrong with that request. Please try again.