mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
add some tests for Host header parser
This commit is contained in:
parent
201c53cb9f
commit
d2adbf3de6
2 changed files with 63 additions and 0 deletions
|
@ -51,6 +51,7 @@
|
||||||
-export([onresponse_capitalize/1]).
|
-export([onresponse_capitalize/1]).
|
||||||
-export([onresponse_crash/1]).
|
-export([onresponse_crash/1]).
|
||||||
-export([onresponse_reply/1]).
|
-export([onresponse_reply/1]).
|
||||||
|
-export([parse_host/1]).
|
||||||
-export([pipeline/1]).
|
-export([pipeline/1]).
|
||||||
-export([pipeline_long_polling/1]).
|
-export([pipeline_long_polling/1]).
|
||||||
-export([rest_bad_accept/1]).
|
-export([rest_bad_accept/1]).
|
||||||
|
@ -103,6 +104,7 @@ all() ->
|
||||||
{group, onrequest},
|
{group, onrequest},
|
||||||
{group, onresponse},
|
{group, onresponse},
|
||||||
{group, onresponse_capitalize},
|
{group, onresponse_capitalize},
|
||||||
|
{group, parse_host},
|
||||||
{group, set_env}
|
{group, set_env}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
@ -184,6 +186,9 @@ groups() ->
|
||||||
{onresponse_capitalize, [parallel], [
|
{onresponse_capitalize, [parallel], [
|
||||||
onresponse_capitalize
|
onresponse_capitalize
|
||||||
]},
|
]},
|
||||||
|
{parse_host, [], [
|
||||||
|
parse_host
|
||||||
|
]},
|
||||||
{set_env, [], [
|
{set_env, [], [
|
||||||
set_env_dispatch
|
set_env_dispatch
|
||||||
]}
|
]}
|
||||||
|
@ -297,6 +302,22 @@ init_per_group(onresponse_capitalize, Config) ->
|
||||||
{ok, Client} = cowboy_client:init([]),
|
{ok, Client} = cowboy_client:init([]),
|
||||||
[{scheme, <<"http">>}, {port, Port}, {opts, []},
|
[{scheme, <<"http">>}, {port, Port}, {opts, []},
|
||||||
{transport, Transport}, {client, Client}|Config];
|
{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) ->
|
init_per_group(set_env, Config) ->
|
||||||
Transport = ranch_tcp,
|
Transport = ranch_tcp,
|
||||||
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
|
{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
|
||||||
|
@ -802,6 +823,29 @@ onresponse_hook(_, Headers, _, Req) ->
|
||||||
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
|
<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req),
|
||||||
Req2.
|
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) ->
|
pipeline(Config) ->
|
||||||
Client = ?config(client, Config),
|
Client = ?config(client, Config),
|
||||||
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
{ok, Client2} = cowboy_client:request(<<"GET">>,
|
||||||
|
|
19
test/http_SUITE_data/http_req_attr.erl
Normal file
19
test/http_SUITE_data/http_req_attr.erl
Normal file
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue