From 19468d0503f80a4a2ef4d40abe1b91bdb3516988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 21 Jun 2016 19:04:52 +0200 Subject: Add cowboy_req:uri/1,2 Along with more cowboy_req tests. This commit also removes cowboy_req:url/1 and cowboy_req:host_url/1 in favor of the much more powerful new set of functions. --- test/handlers/echo_h.erl | 47 +++++++++++++++++++++++----- test/req_SUITE.erl | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl index cdb9be4..802d537 100644 --- a/test/handlers/echo_h.erl +++ b/test/handlers/echo_h.erl @@ -5,15 +5,48 @@ -export([init/2]). init(Req, Opts) -> - echo(cowboy_req:binding(key, Req), Req, Opts). + case cowboy_req:binding(arg, Req) of + undefined -> + echo(cowboy_req:binding(key, Req), Req, Opts); + Arg -> + echo_arg(Arg, Req, Opts) + end. +echo(<<"body">>, Req0, Opts) -> + {ok, Body, Req} = cowboy_req:read_body(Req0), + cowboy_req:reply(200, #{}, Body, Req), + {ok, Req, Opts}; +echo(<<"uri">>, Req, Opts) -> + Value = case cowboy_req:path_info(Req) of + [<<"origin">>] -> cowboy_req:uri(Req, #{host => undefined}); + [<<"protocol-relative">>] -> cowboy_req:uri(Req, #{scheme => undefined}); + [<<"no-qs">>] -> cowboy_req:uri(Req, #{qs => undefined}); + [<<"no-path">>] -> cowboy_req:uri(Req, #{path => undefined, qs => undefined}); + [<<"set-port">>] -> cowboy_req:uri(Req, #{port => 123}); + [] -> cowboy_req:uri(Req) + end, + cowboy_req:reply(200, #{}, Value, Req), + {ok, Req, Opts}; echo(What, Req, Opts) -> F = binary_to_atom(What, latin1), - Value = case cowboy_req:F(Req) of - V when is_integer(V) -> integer_to_binary(V); - V when is_atom(V) -> atom_to_binary(V, latin1); - V when is_list(V); is_tuple(V) -> io_lib:format("~p", [V]); - V -> V + Value = cowboy_req:F(Req), + cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), + {ok, Req, Opts}. + +echo_arg(Arg0, Req, Opts) -> + F = binary_to_atom(cowboy_req:binding(key, Req), latin1), + Arg = case F of + binding -> binary_to_atom(Arg0, latin1); + _ -> Arg0 end, - cowboy_req:reply(200, #{}, Value, Req), + Value = case cowboy_req:binding(default, Req) of + undefined -> cowboy_req:F(Arg, Req); + Default -> cowboy_req:F(Arg, Req, Default) + end, + cowboy_req:reply(200, #{}, value_to_iodata(Value), Req), {ok, Req, Opts}. + +value_to_iodata(V) when is_integer(V) -> integer_to_binary(V); +value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1); +value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~p", [V]); +value_to_iodata(V) -> V. diff --git a/test/req_SUITE.erl b/test/req_SUITE.erl index 39c975c..4d9502f 100644 --- a/test/req_SUITE.erl +++ b/test/req_SUITE.erl @@ -73,6 +73,32 @@ do_get_body(Path, Headers, Config) -> %% Tests. +binding(Config) -> + doc("Value bound from request URI path with/without default."), + <<"binding">> = do_get_body("/args/binding/key", Config), + <<"binding">> = do_get_body("/args/binding/key/default", Config), + <<"default">> = do_get_body("/args/binding/undefined/default", Config), + ok. + +%% @todo Do we really want a key/value list here instead of a map? +bindings(Config) -> + doc("Values bound from request URI path."), + <<"[{key,<<\"bindings\">>}]">> = do_get_body("/bindings", Config), + ok. + +header(Config) -> + doc("Request header with/without default."), + <<"value">> = do_get_body("/args/header/defined", [{<<"defined">>, "value"}], Config), + <<"value">> = do_get_body("/args/header/defined/default", [{<<"defined">>, "value"}], Config), + <<"default">> = do_get_body("/args/header/undefined/default", [{<<"defined">>, "value"}], Config), + ok. + +headers(Config) -> + doc("Request headers."), + << "#{<<\"header\">> => <<\"value\">>", _/bits >> + = do_get_body("/headers", [{<<"header">>, "value"}], Config), + ok. + host(Config) -> doc("Request URI host."), <<"localhost">> = do_get_body("/host", Config), @@ -94,6 +120,30 @@ method(Config) -> <<"ZZZZZZZZ">> = do_body("ZZZZZZZZ", "/method", Config), ok. +%% @todo Do we really want a key/value list here instead of a map? +parse_cookies(Config) -> + doc("Request cookies."), + <<"[]">> = do_get_body("/parse_cookies", Config), + <<"[{<<\"cake\">>,<<\"strawberry\">>}]">> + = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry"}], Config), + <<"[{<<\"cake\">>,<<\"strawberry\">>},{<<\"color\">>,<<\"blue\">>}]">> + = do_get_body("/parse_cookies", [{<<"cookie">>, "cake=strawberry; color=blue"}], Config), + ok. + +parse_header(Config) -> + doc("Parsed request header with/without default."), + <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">> + = do_get_body("/args/parse_header/accept", [{<<"accept">>, "text/html"}], Config), + <<"[{{<<\"text\">>,<<\"html\">>,[]},1000,[]}]">> + = do_get_body("/args/parse_header/accept/default", [{<<"accept">>, "text/html"}], Config), + %% Header not in request but with default defined by Cowboy. + <<"0">> = do_get_body("/args/parse_header/content-length", Config), + %% Header not in request and no default from Cowboy. + <<"undefined">> = do_get_body("/args/parse_header/upgrade", Config), + %% Header in request and with default provided. + <<"100-continue">> = do_get_body("/args/parse_header/expect/100-continue", Config), + ok. + %% @todo Do we really want a key/value list here instead of a map? parse_qs(Config) -> doc("Parsed request URI query string."), @@ -147,6 +197,35 @@ scheme(Config) -> <<"https">> when Transport =:= ssl -> ok end. +uri(Config) -> + doc("Request URI building/modification."), + Scheme = case config(type, Config) of + tcp -> <<"http">>; + ssl -> <<"https">> + end, + SLen = byte_size(Scheme), + Port = integer_to_binary(config(port, Config)), + PLen = byte_size(Port), + %% Absolute form. + << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri?qs" >> + = do_get_body("/uri?qs", Config), + %% Origin form. + << "/uri/origin?qs" >> = do_get_body("/uri/origin?qs", Config), + %% Protocol relative. + << "//localhost:", Port:PLen/binary, "/uri/protocol-relative?qs" >> + = do_get_body("/uri/protocol-relative?qs", Config), + %% No query string. + << Scheme:SLen/binary, "://localhost:", Port:PLen/binary, "/uri/no-qs" >> + = do_get_body("/uri/no-qs?qs", Config), + %% No path or query string. + << Scheme:SLen/binary, "://localhost:", Port:PLen/binary >> + = do_get_body("/uri/no-path?qs", Config), + %% Changed port. + << Scheme:SLen/binary, "://localhost:123/uri/set-port?qs" >> + = do_get_body("/uri/set-port?qs", Config), + %% This function is tested more extensively through unit tests. + ok. + version(Config) -> doc("Request HTTP version."), Protocol = config(protocol, Config), -- cgit v1.2.3