From d2adbf3de66f15dc0b654c76ee7bee7bd9c8c778 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sun, 25 Aug 2013 02:13:53 +0900 Subject: add some tests for Host header parser --- test/http_SUITE.erl | 44 ++++++++++++++++++++++++++++++++++ test/http_SUITE_data/http_req_attr.erl | 19 +++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/http_SUITE_data/http_req_attr.erl (limited to 'test') diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 2135fdc..f784c50 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -51,6 +51,7 @@ -export([onresponse_capitalize/1]). -export([onresponse_crash/1]). -export([onresponse_reply/1]). +-export([parse_host/1]). -export([pipeline/1]). -export([pipeline_long_polling/1]). -export([rest_bad_accept/1]). @@ -103,6 +104,7 @@ all() -> {group, onrequest}, {group, onresponse}, {group, onresponse_capitalize}, + {group, parse_host}, {group, set_env} ]. @@ -184,6 +186,9 @@ groups() -> {onresponse_capitalize, [parallel], [ onresponse_capitalize ]}, + {parse_host, [], [ + parse_host + ]}, {set_env, [], [ set_env_dispatch ]} @@ -297,6 +302,22 @@ init_per_group(onresponse_capitalize, Config) -> {ok, Client} = cowboy_client:init([]), [{scheme, <<"http">>}, {port, Port}, {opts, []}, {transport, Transport}, {client, Client}|Config]; +init_per_group(parse_host, Config) -> + Transport = ranch_tcp, + Dispatch = cowboy_router:compile([ + {'_', [ + {"/req_attr", http_req_attr, []} + ]} + ]), + {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [ + {env, [{dispatch, Dispatch}]}, + {max_keepalive, 50}, + {timeout, 500} + ]), + Port = ranch:get_port(http), + {ok, Client} = cowboy_client:init([]), + [{scheme, <<"http">>}, {port, Port}, {opts, []}, + {transport, Transport}, {client, Client}|Config]; init_per_group(set_env, Config) -> Transport = ranch_tcp, {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [ @@ -802,6 +823,29 @@ onresponse_hook(_, Headers, _, Req) -> <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), Req2. +parse_host(Config) -> + Tests = [ + {<<"example.org\n8080">>, <<"example.org:8080">>}, + {<<"example.org\n80">>, <<"example.org">>}, + {<<"192.0.2.1\n8080">>, <<"192.0.2.1:8080">>}, + {<<"192.0.2.1\n80">>, <<"192.0.2.1">>}, + {<<"[2001:db8::1]\n8080">>, <<"[2001:db8::1]:8080">>}, + {<<"[2001:db8::1]\n80">>, <<"[2001:db8::1]">>}, + {<<"[::ffff:192.0.2.1]\n8080">>, <<"[::ffff:192.0.2.1]:8080">>}, + {<<"[::ffff:192.0.2.1]\n80">>, <<"[::ffff:192.0.2.1]">>} + ], + [begin + Client = ?config(client, Config), + {ok, Client2} = cowboy_client:request(<<"GET">>, + build_url("/req_attr?attr=host_and_port", Config), + [{<<"host">>, Host}], + Client), + {ok, 200, _, Client3} = cowboy_client:response(Client2), + {ok, Value, Client4} = cowboy_client:response_body(Client3), + {error, closed} = cowboy_client:response(Client4), + Value + end || {Value, Host} <- Tests]. + pipeline(Config) -> Client = ?config(client, Config), {ok, Client2} = cowboy_client:request(<<"GET">>, diff --git a/test/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl new file mode 100644 index 0000000..eb5e70e --- /dev/null +++ b/test/http_SUITE_data/http_req_attr.erl @@ -0,0 +1,19 @@ +%% Feel free to use, reuse and abuse the code in this file. + +-module(http_req_attr). +-behaviour(cowboy_http_handler). +-export([init/3, handle/2, terminate/3]). + +init({_, http}, Req, _) -> + {Attr, Req2} = cowboy_req:qs_val(<<"attr">>, Req), + {ok, Req2, Attr}. + +handle(Req, <<"host_and_port">> = Attr) -> + {Host, Req2} = cowboy_req:host(Req), + {Port, Req3} = cowboy_req:port(Req2), + Value = [Host, "\n", integer_to_list(Port)], + {ok, Req4} = cowboy_req:reply(200, [], Value, Req3), + {ok, Req4, Attr}. + +terminate(_, _, _) -> + ok. -- cgit v1.2.3