Browse Source

Remove cowboy_client; use gun for the HTTP test suite

Loïc Hoguin 11 years ago
parent
commit
704f61c9d1
3 changed files with 551 additions and 1061 deletions
  1. 0 275
      src/cowboy_client.erl
  2. 534 762
      test/http_SUITE.erl
  3. 17 24
      test/http_SUITE_data/http_loop_stream_recv.erl

+ 0 - 275
src/cowboy_client.erl

@@ -1,275 +0,0 @@
-%% Copyright (c) 2012-2014, Loïc Hoguin <essen@ninenines.eu>
-%%
-%% Permission to use, copy, modify, and/or distribute this software for any
-%% purpose with or without fee is hereby granted, provided that the above
-%% copyright notice and this permission notice appear in all copies.
-%%
-%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-%% @private
--module(cowboy_client).
-
--export([init/1]).
--export([state/1]).
--export([transport/1]).
-
--export([connect/4]).
--export([raw_request/2]).
--export([request/3]).
--export([request/4]).
--export([request/5]).
--export([response/1]).
--export([response_body/1]).
--export([skip_body/1]).
--export([stream_status/1]).
--export([stream_headers/1]).
--export([stream_header/1]).
--export([stream_body/1]).
-
--record(client, {
-	state = wait :: wait | request | response | response_body,
-	opts = [] :: [any()],
-	socket = undefined :: undefined | inet:socket(),
-	transport = undefined :: module(),
-	timeout = 5000 :: timeout(), %% @todo Configurable.
-	buffer = <<>> :: binary(),
-	connection = keepalive :: keepalive | close,
-	version = 'HTTP/1.1' :: cowboy:http_version(),
-	response_body = undefined :: undefined | non_neg_integer()
-}).
-
-init(Opts) ->
-	{ok, #client{opts=Opts}}.
-
-state(#client{state=State}) ->
-	State.
-
-transport(#client{socket=undefined}) ->
-	{error, notconnected};
-transport(#client{transport=Transport, socket=Socket}) ->
-	{ok, Transport, Socket}.
-
-connect(Transport, Host, Port, Client)
-		when is_binary(Host) ->
-	connect(Transport, binary_to_list(Host), Port, Client);
-connect(Transport, Host, Port, Client=#client{state=State, opts=Opts})
-		when is_atom(Transport), is_list(Host),
-			is_integer(Port), is_record(Client, client),
-			State =:= wait ->
-	{ok, Socket} = Transport:connect(Host, Port, Opts),
-	{ok, Client#client{state=request, socket=Socket, transport=Transport}}.
-
-raw_request(Data, Client=#client{state=response_body}) ->
-	{done, Client2} = skip_body(Client),
-	raw_request(Data, Client2);
-raw_request(Data, Client=#client{
-		state=State, socket=Socket, transport=Transport})
-		when State =:= request ->
-	ok = Transport:send(Socket, Data),
-	{ok, Client}.
-
-request(Method, URL, Client) ->
-	request(Method, URL, [], <<>>, Client).
-
-request(Method, URL, Headers, Client) ->
-	request(Method, URL, Headers, <<>>, Client).
-
-request(Method, URL, Headers, Body, Client=#client{state=response_body}) ->
-	{done, Client2} = skip_body(Client),
-	request(Method, URL, Headers, Body, Client2);
-request(Method, URL, Headers, Body, Client=#client{
-		state=State, version=Version})
-		when State =:= wait; State =:= request ->
-	{Transport, FullHost, Host, Port, Path} = parse_url(URL),
-	{ok, Client2} = case State of
-		wait -> connect(Transport, Host, Port, Client);
-		request -> {ok, Client}
-	end,
-	VersionBin = atom_to_binary(Version, latin1),
-	%% @todo do keepalive too, allow override...
-	Headers2 = case lists:keyfind(<<"host">>, 1, Headers) of
-		false -> [{<<"host">>, FullHost}|Headers];
-		_ -> Headers
-	end,
-	Headers3 = [
-		{<<"user-agent">>, <<"Cow">>}
-	|Headers2],
-	Headers4 = case iolist_size(Body) of
-		0 -> Headers3;
-		Length -> [{<<"content-length">>, integer_to_list(Length)}|Headers3]
-	end,
-	HeadersData = [[Name, <<": ">>, Value, <<"\r\n">>]
-		|| {Name, Value} <- Headers4],
-	Data = [Method, <<" ">>, Path, <<" ">>, VersionBin, <<"\r\n">>,
-		HeadersData, <<"\r\n">>, Body],
-	raw_request(Data, Client2).
-
-parse_url(<< "https://", Rest/binary >>) ->
-	parse_url(Rest, ranch_ssl);
-parse_url(<< "http://", Rest/binary >>) ->
-	parse_url(Rest, ranch_tcp);
-parse_url(URL) ->
-	parse_url(URL, ranch_tcp).
-
-parse_url(URL, Transport) ->
-	case binary:split(URL, <<"/">>) of
-		[Peer] ->
-			{Host, Port} = parse_peer(Peer, Transport),
-			{Transport, Peer, Host, Port, <<"/">>};
-		[Peer, Path] ->
-			{Host, Port} = parse_peer(Peer, Transport),
-			{Transport, Peer, Host, Port, [<<"/">>, Path]}
-	end.
-
-parse_peer(Peer, Transport) ->
-	case binary:split(Peer, <<":">>) of
-		[Host] when Transport =:= ranch_tcp ->
-			{binary_to_list(Host), 80};
-		[Host] when Transport =:= ranch_ssl ->
-			{binary_to_list(Host), 443};
-		[Host, Port] ->
-			{binary_to_list(Host), list_to_integer(binary_to_list(Port))}
-	end.
-
-response(Client=#client{state=response_body}) ->
-	{done, Client2} = skip_body(Client),
-	response(Client2);
-response(Client=#client{state=request}) ->
-	case stream_status(Client) of
-		{ok, Status, _, Client2} ->
-			case stream_headers(Client2) of
-				{ok, Headers, Client3} ->
-					{ok, Status, Headers, Client3};
-				{error, Reason} ->
-					{error, Reason}
-			end;
-		{error, Reason} ->
-			{error, Reason}
-	end.
-
-response_body(Client=#client{state=response_body}) ->
-	response_body_loop(Client, <<>>).
-
-response_body_loop(Client, Acc) ->
-	case stream_body(Client) of
-		{ok, Data, Client2} ->
-			response_body_loop(Client2, << Acc/binary, Data/binary >>);
-		{done, Client2} ->
-			{ok, Acc, Client2};
-		{error, Reason} ->
-			{error, Reason}
-	end.
-
-skip_body(Client=#client{state=response_body}) ->
-	case stream_body(Client) of
-		{ok, _, Client2} -> skip_body(Client2);
-		Done -> Done
-	end.
-
-stream_status(Client=#client{state=State, buffer=Buffer})
-		when State =:= request ->
-	case binary:split(Buffer, <<"\r\n">>) of
-		[Line, Rest] ->
-			parse_version(Client#client{state=response, buffer=Rest}, Line);
-		_ ->
-			case recv(Client) of
-				{ok, Data} ->
-					Buffer2 = << Buffer/binary, Data/binary >>,
-					stream_status(Client#client{buffer=Buffer2});
-				{error, Reason} ->
-					{error, Reason}
-			end
-	end.
-
-parse_version(Client, << "HTTP/1.1 ", Rest/binary >>) ->
-	parse_status(Client, Rest, 'HTTP/1.1');
-parse_version(Client, << "HTTP/1.0 ", Rest/binary >>) ->
-	parse_status(Client, Rest, 'HTTP/1.0').
-
-parse_status(Client, << S3, S2, S1, " ", StatusStr/binary >>, Version)
-		when S3 >= $0, S3 =< $9, S2 >= $0, S2 =< $9, S1 >= $0, S1 =< $9 ->
-	Status = (S3 - $0) * 100 + (S2 - $0) * 10 + S1 - $0,
-	{ok, Status, StatusStr, Client#client{version=Version}}.
-
-stream_headers(Client=#client{state=State})
-		when State =:= response ->
-	stream_headers(Client, []).
-
-stream_headers(Client, Acc) ->
-	case stream_header(Client) of
-		{ok, Name, Value, Client2} ->
-			stream_headers(Client2, [{Name, Value}|Acc]);
-		{done, Client2} ->
-			{ok, Acc, Client2};
-		{error, Reason} ->
-			{error, Reason}
-	end.
-
-stream_header(Client=#client{state=State, buffer=Buffer,
-		response_body=RespBody}) when State =:= response ->
-	case binary:split(Buffer, <<"\r\n">>) of
-		[<<>>, Rest] ->
-			%% If we have a body, set response_body.
-			Client2 = case RespBody of
-				undefined -> Client#client{state=request};
-				0 -> Client#client{state=request};
-				_ -> Client#client{state=response_body}
-			end,
-			{done, Client2#client{buffer=Rest}};
-		[Line, Rest] ->
-			%% @todo Do a better parsing later on.
-			[Name, Value] = binary:split(Line, <<": ">>),
-			Name2 = cowboy_bstr:to_lower(Name),
-			Client2 = case Name2 of
-				<<"content-length">> ->
-					Length = list_to_integer(binary_to_list(Value)),
-					if Length >= 0 -> ok end,
-					Client#client{response_body=Length};
-				_ ->
-					Client
-			end,
-			{ok, Name2, Value, Client2#client{buffer=Rest}};
-		_ ->
-			case recv(Client) of
-				{ok, Data} ->
-					Buffer2 = << Buffer/binary, Data/binary >>,
-					stream_header(Client#client{buffer=Buffer2});
-				{error, Reason} ->
-					{error, Reason}
-			end
-	end.
-
-stream_body(Client=#client{state=response_body, response_body=RespBody})
-		when RespBody =:= undefined; RespBody =:= 0 ->
-	{done, Client#client{state=request, response_body=undefined}};
-stream_body(Client=#client{state=response_body, buffer=Buffer,
-		response_body=Length}) when is_integer(Length) ->
-	case byte_size(Buffer) of
-		0 ->
-			case recv(Client) of
-				{ok, Body} when byte_size(Body) =< Length ->
-					Length2 = Length - byte_size(Body),
-					{ok, Body, Client#client{response_body=Length2}};
-				{ok, Data} ->
-					<< Body:Length/binary, Rest/binary >> = Data,
-					{ok, Body, Client#client{buffer=Rest,
-						response_body=undefined}};
-				{error, Reason} ->
-					{error, Reason}
-			end;
-		N when N =< Length ->
-			Length2 = Length - N,
-			{ok, Buffer, Client#client{buffer= <<>>, response_body=Length2}};
-		_ ->
-			<< Body:Length/binary, Rest/binary >> = Buffer,
-			{ok, Body, Client#client{buffer=Rest, response_body=undefined}}
-	end.
-
-recv(#client{socket=Socket, transport=Transport, timeout=Timeout}) ->
-	Transport:recv(Socket, 0, Timeout).

+ 534 - 762
test/http_SUITE.erl

@@ -14,93 +14,11 @@
 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
 
 -module(http_SUITE).
 -module(http_SUITE).
+-compile(export_all).
 
 
 -include_lib("common_test/include/ct.hrl").
 -include_lib("common_test/include/ct.hrl").
 
 
 %% ct.
 %% ct.
--export([all/0]).
--export([groups/0]).
--export([init_per_suite/1]).
--export([end_per_suite/1]).
--export([init_per_group/2]).
--export([end_per_group/2]).
-
-%% Callbacks.
--export([etag_gen/3]).
--export([mimetypes_text_html/1]).
-
-%% Tests.
--export([check_raw_status/1]).
--export([check_status/1]).
--export([chunked_response/1]).
--export([echo_body/1]).
--export([echo_body_max_length/1]).
--export([echo_body_qs/1]).
--export([echo_body_qs_max_length/1]).
--export([error_chain_handle_after_reply/1]).
--export([error_chain_handle_before_reply/1]).
--export([error_handle_after_reply/1]).
--export([error_init_after_reply/1]).
--export([error_init_reply_handle_error/1]).
--export([headers_dupe/1]).
--export([http10_chunkless/1]).
--export([http10_hostless/1]).
--export([keepalive_max/1]).
--export([keepalive_nl/1]).
--export([keepalive_stream_loop/1]).
--export([multipart/1]).
--export([multipart_large/1]).
--export([nc_rand/1]).
--export([nc_zero/1]).
--export([onrequest/1]).
--export([onrequest_reply/1]).
--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]).
--export([rest_bad_content_type/1]).
--export([rest_expires/1]).
--export([rest_keepalive/1]).
--export([rest_keepalive_post/1]).
--export([rest_missing_get_callbacks/1]).
--export([rest_missing_put_callbacks/1]).
--export([rest_nodelete/1]).
--export([rest_options_default/1]).
--export([rest_param_all/1]).
--export([rest_patch/1]).
--export([rest_post_charset/1]).
--export([rest_postonly/1]).
--export([rest_resource_etags/1]).
--export([rest_resource_etags_if_none_match/1]).
--export([set_env_dispatch/1]).
--export([set_resp_body/1]).
--export([set_resp_header/1]).
--export([set_resp_overwrite/1]).
--export([slowloris/1]).
--export([slowloris2/1]).
--export([static_attribute_etag/1]).
--export([static_function_etag/1]).
--export([static_mimetypes_function/1]).
--export([static_specify_file/1]).
--export([static_specify_file_catchall/1]).
--export([static_test_file/1]).
--export([static_test_file_css/1]).
--export([stream_body_set_resp/1]).
--export([stream_body_set_resp_close/1]).
--export([stream_body_set_resp_chunked/1]).
--export([stream_body_set_resp_chunked10/1]).
--export([streamed_response/1]).
--export([te_chunked/1]).
--export([te_chunked_chopped/1]).
--export([te_chunked_delayed/1]).
--export([te_chunked_split_body/1]).
--export([te_chunked_split_crlf/1]).
--export([te_identity/1]).
-
-%% ct.
 
 
 all() ->
 all() ->
 	[
 	[
@@ -206,8 +124,12 @@ groups() ->
 
 
 init_per_suite(Config) ->
 init_per_suite(Config) ->
 	application:start(crypto),
 	application:start(crypto),
-	application:start(cowlib),
+	application:start(asn1),
+	application:start(public_key),
+	application:start(ssl),
 	application:start(ranch),
 	application:start(ranch),
+	application:start(gun),
+	application:start(cowlib),
 	application:start(cowboy),
 	application:start(cowboy),
 	Dir = ?config(priv_dir, Config) ++ "/static",
 	Dir = ?config(priv_dir, Config) ++ "/static",
 	ct_helper:create_static_dir(Dir),
 	ct_helper:create_static_dir(Dir),
@@ -217,67 +139,46 @@ end_per_suite(Config) ->
 	Dir = ?config(static_dir, Config),
 	Dir = ?config(static_dir, Config),
 	ct_helper:delete_static_dir(Dir),
 	ct_helper:delete_static_dir(Dir),
 	application:stop(cowboy),
 	application:stop(cowboy),
-	application:stop(ranch),
 	application:stop(cowlib),
 	application:stop(cowlib),
+	application:stop(gun),
+	application:stop(ranch),
+	application:stop(ssl),
+	application:stop(public_key),
+	application:stop(asn1),
 	application:stop(crypto),
 	application:stop(crypto),
 	ok.
 	ok.
 
 
-init_per_group(http, Config) ->
+init_tcp_group(Ref, ProtoOpts, Config) ->
 	Transport = ranch_tcp,
 	Transport = ranch_tcp,
-	{ok, _} = cowboy:start_http(http, 100, [{port, 0}], [
+	{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
 		{timeout, 500}
 		{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(https, Config) ->
+		|ProtoOpts]),
+	Port = ranch:get_port(Ref),
+	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
+
+init_ssl_group(Ref, ProtoOpts, Config) ->
 	Transport = ranch_ssl,
 	Transport = ranch_ssl,
 	{_, Cert, Key} = ct_helper:make_certs(),
 	{_, Cert, Key} = ct_helper:make_certs(),
 	Opts = [{cert, Cert}, {key, Key}],
 	Opts = [{cert, Cert}, {key, Key}],
-	application:start(asn1),
-	application:start(public_key),
-	application:start(ssl),
-	{ok, _} = cowboy:start_https(https, 100, Opts ++ [{port, 0}], [
+	{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
 		{timeout, 500}
 		{timeout, 500}
-	]),
-	Port = ranch:get_port(https),
-	{ok, Client} = cowboy_client:init(Opts),
-	[{scheme, <<"https">>}, {port, Port}, {opts, Opts},
-		{transport, Transport}, {client, Client}|Config];
+		|ProtoOpts]),
+	Port = ranch:get_port(Ref),
+	[{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
+
+init_per_group(http, Config) ->
+	init_tcp_group(http, [], Config);
+init_per_group(https, Config) ->
+	init_ssl_group(https, [], Config);
 init_per_group(http_compress, Config) ->
 init_per_group(http_compress, Config) ->
-	Transport = ranch_tcp,
-	{ok, _} = cowboy:start_http(http_compress, 100, [{port, 0}], [
-		{compress, true},
-		{env, [{dispatch, init_dispatch(Config)}]},
-		{max_keepalive, 50},
-		{timeout, 500}
-	]),
-	Port = ranch:get_port(http_compress),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config];
+	init_tcp_group(http_compress, [{compress, true}], Config);
 init_per_group(https_compress, Config) ->
 init_per_group(https_compress, Config) ->
-	Transport = ranch_ssl,
-	{_, Cert, Key} = ct_helper:make_certs(),
-	Opts = [{cert, Cert}, {key, Key}],
-	application:start(asn1),
-	application:start(public_key),
-	application:start(ssl),
-	{ok, _} = cowboy:start_https(https_compress, 100, Opts ++ [{port, 0}], [
-		{compress, true},
-		{env, [{dispatch, init_dispatch(Config)}]},
-		{max_keepalive, 50},
-		{timeout, 500}
-	]),
-	Port = ranch:get_port(https_compress),
-	{ok, Client} = cowboy_client:init(Opts),
-	[{scheme, <<"https">>}, {port, Port}, {opts, Opts},
-		{transport, Transport}, {client, Client}|Config];
+	init_ssl_group(https_compress, [{compress, true}], Config);
+%% Most, if not all of these, should be in separate test suites.
 init_per_group(onrequest, Config) ->
 init_per_group(onrequest, Config) ->
 	Transport = ranch_tcp,
 	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
@@ -287,9 +188,8 @@ init_per_group(onrequest, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onrequest),
 	Port = ranch:get_port(onrequest),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config];
+	[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+		{transport, Transport}|Config];
 init_per_group(onresponse, Config) ->
 init_per_group(onresponse, Config) ->
 	Transport = ranch_tcp,
 	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
@@ -299,9 +199,8 @@ init_per_group(onresponse, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onresponse),
 	Port = ranch:get_port(onresponse),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config];
+	[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+		{transport, Transport}|Config];
 init_per_group(onresponse_capitalize, Config) ->
 init_per_group(onresponse_capitalize, Config) ->
 	Transport = ranch_tcp,
 	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
@@ -311,9 +210,8 @@ init_per_group(onresponse_capitalize, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onresponse_capitalize),
 	Port = ranch:get_port(onresponse_capitalize),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config];
+	[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+		{transport, Transport}|Config];
 init_per_group(parse_host, Config) ->
 init_per_group(parse_host, Config) ->
 	Transport = ranch_tcp,
 	Transport = ranch_tcp,
 	Dispatch = cowboy_router:compile([
 	Dispatch = cowboy_router:compile([
@@ -327,9 +225,8 @@ init_per_group(parse_host, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(http),
 	Port = ranch:get_port(http),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config];
+	[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+		{transport, Transport}|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}], [
@@ -338,16 +235,9 @@ init_per_group(set_env, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(set_env),
 	Port = ranch:get_port(set_env),
-	{ok, Client} = cowboy_client:init([]),
-	[{scheme, <<"http">>}, {port, Port}, {opts, []},
-		{transport, Transport}, {client, Client}|Config].
+	[{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []},
+		{transport, Transport}|Config].
 
 
-end_per_group(Name, _) when Name =:= https; Name =:= https_compress ->
-	cowboy:stop_listener(Name),
-	application:stop(ssl),
-	application:stop(public_key),
-	application:stop(asn1),
-	ok;
 end_per_group(Name, _) ->
 end_per_group(Name, _) ->
 	cowboy:stop_listener(Name),
 	cowboy:stop_listener(Name),
 	ok.
 	ok.
@@ -424,46 +314,83 @@ etag_gen(_, _, _) ->
 mimetypes_text_html(_) ->
 mimetypes_text_html(_) ->
 	<<"text/html">>.
 	<<"text/html">>.
 
 
+%% Support functions for testing using Gun.
+
+gun_open(Config) ->
+	gun_open(Config, []).
+
+gun_open(Config, Opts) ->
+	{_, Port} = lists:keyfind(port, 1, Config),
+	{_, Type} = lists:keyfind(type, 1, Config),
+	{ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}, {type, Type}|Opts]),
+	ConnPid.
+
+gun_monitor_open(Config) ->
+	gun_monitor_open(Config, []).
+
+gun_monitor_open(Config, Opts) ->
+	ConnPid = gun_open(Config, Opts),
+	{ConnPid, monitor(process, ConnPid)}.
+
+gun_is_gone(ConnPid) ->
+	gun_is_gone(ConnPid, monitor(process, ConnPid)).
+
+gun_is_gone(ConnPid, MRef) ->
+	receive {'DOWN', MRef, process, ConnPid, gone} -> ok
+	after 500 -> error(timeout) end.
+
+%% Support functions for testing using a raw socket.
+
+raw_open(Config) ->
+	{_, Port} = lists:keyfind(port, 1, Config),
+	{_, Type} = lists:keyfind(type, 1, Config),
+	Transport = case Type of
+		tcp -> gen_tcp;
+		ssl -> ssl
+	end,
+	{_, Opts} = lists:keyfind(opts, 1, Config),
+	{ok, Socket} = Transport:connect("localhost", Port,
+		[binary, {active, false}, {packet, raw},
+			{reuseaddr, true}, {nodelay, true}|Opts]),
+	{raw_client, Socket, Transport}.
+
+raw_send({raw_client, Socket, Transport}, Data) ->
+	Transport:send(Socket, Data).
+
+raw_recv_head({raw_client, Socket, Transport}) ->
+	{ok, Data} = Transport:recv(Socket, 0, 5000),
+	raw_recv_head(Socket, Transport, Data).
+
+raw_recv_head(Socket, Transport, Buffer) ->
+	case binary:match(Buffer, <<"\r\n\r\n">>) of
+		nomatch ->
+			{ok, Data} = Transport:recv(Socket, 0, 5000),
+			raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
+		{_, _} ->
+			Buffer
+	end.
+
+raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
+	{ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000),
+	ok.
+
 %% Convenience functions.
 %% Convenience functions.
 
 
 quick_raw(Data, Config) ->
 quick_raw(Data, Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
-	{ok, Client3} = cowboy_client:raw_request(Data, Client2),
-	case cowboy_client:response(Client3) of
-		{ok, Status, _, _} -> Status;
-		{error, _} -> closed
+	Client = raw_open(Config),
+	ok = raw_send(Client, Data),
+	case catch raw_recv_head(Client) of
+		{'EXIT', _} -> closed;
+		Resp -> element(2, cow_http:parse_status_line(Resp))
 	end.
 	end.
 
 
-build_url(Path, Config) ->
-	{scheme, Scheme} = lists:keyfind(scheme, 1, Config),
-	{port, Port} = lists:keyfind(port, 1, Config),
-	PortBin = list_to_binary(integer_to_list(Port)),
-	PathBin = list_to_binary(Path),
-	<< Scheme/binary, "://localhost:", PortBin/binary, PathBin/binary >>.
-
-quick_get(URL, Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url(URL, Config), Client),
-	{ok, Status, _, _} = cowboy_client:response(Client2),
+quick_get(Path, Config) ->
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, Path),
+	{response, _, Status, _} = gun:await(ConnPid, Ref),
+	gun:close(ConnPid),
 	Status.
 	Status.
 
 
-body_to_chunks(_, <<>>, Acc) ->
-	lists:reverse([<<"0\r\n\r\n">>|Acc]);
-body_to_chunks(ChunkSize, Body, Acc) ->
-	BodySize = byte_size(Body),
-	ChunkSize2 = case BodySize < ChunkSize of
-		true -> BodySize;
-		false -> ChunkSize
-	end,
-	<< Chunk:ChunkSize2/binary, Rest/binary >> = Body,
-	ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
-	body_to_chunks(ChunkSize, Rest,
-		[<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
-
 %% Tests.
 %% Tests.
 
 
 check_raw_status(Config) ->
 check_raw_status(Config) ->
@@ -548,135 +475,95 @@ check_status(Config) ->
 	end || {Status, URL} <- Tests].
 	end || {Status, URL} <- Tests].
 
 
 chunked_response(Config) ->
 chunked_response(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/chunked_response", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/chunked_response"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
 	true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
 	true = lists:keymember(<<"transfer-encoding">>, 1, Headers),
-	{ok, Transport, Socket} = cowboy_client:transport(Client3),
-	{ok, <<"11\r\nchunked_handler\r\n\r\nB\r\nworks fine!\r\n0\r\n\r\n">>}
-		= Transport:recv(Socket, 44, 1000),
-	{error, closed} = cowboy_client:response(Client3).
+	{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 %% Check if sending requests whose size is around the MTU breaks something.
 %% Check if sending requests whose size is around the MTU breaks something.
 echo_body(Config) ->
 echo_body(Config) ->
-	Client = ?config(client, Config),
 	MTU = ct_helper:get_loopback_mtu(),
 	MTU = ct_helper:get_loopback_mtu(),
 	_ = [begin
 	_ = [begin
 		Body = list_to_binary(lists:duplicate(Size, $a)),
 		Body = list_to_binary(lists:duplicate(Size, $a)),
-		{ok, Client2} = cowboy_client:request(<<"POST">>,
-			build_url("/echo/body", Config),
-			[{<<"connection">>, <<"close">>}],
-			Body, Client),
-		{ok, 200, _, Client3} = cowboy_client:response(Client2),
-		{ok, Body, _} = cowboy_client:response_body(Client3)
+		ConnPid = gun_open(Config),
+		Ref = gun:post(ConnPid, "/echo/body", [], Body),
+		{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+		{ok, Body} = gun:await_body(ConnPid, Ref)
 	end || Size <- lists:seq(MTU - 500, MTU)],
 	end || Size <- lists:seq(MTU - 500, MTU)],
 	ok.
 	ok.
 
 
 %% Check if sending request whose size is bigger than 1000000 bytes causes 413
 %% Check if sending request whose size is bigger than 1000000 bytes causes 413
 echo_body_max_length(Config) ->
 echo_body_max_length(Config) ->
-	Client = ?config(client, Config),
-	Body = <<$a:8000008>>,
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/echo/body", Config),
-		[{<<"connection">>, <<"close">>}],
-		Body, Client),
-	{ok, 413, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body", [], << 0:8000008 >>),
+	{response, nofin, 413, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 % check if body_qs echo's back results
 % check if body_qs echo's back results
 echo_body_qs(Config) ->
 echo_body_qs(Config) ->
-	Client = ?config(client, Config),
-	Body = <<"echo=67890">>,
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/echo/body_qs", Config),
-		[{<<"connection">>, <<"close">>}],
-		Body, Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, <<"67890">>, _} = cowboy_client:response_body(Client3).
-
-%% Check if sending request whose size is bigger 16000 bytes causes 413
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body_qs", [], <<"echo=67890">>),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, <<"67890">>} = gun:await_body(ConnPid, Ref),
+	ok.
+
 echo_body_qs_max_length(Config) ->
 echo_body_qs_max_length(Config) ->
-	Client = ?config(client, Config),
-	DefaultMaxBodyQsLength = 16000,
-	% subtract "echo=" minus 1 byte from max to hit the limit
-	Bits = (DefaultMaxBodyQsLength - 4) * 8,
-	AppendedBody = <<$a:Bits>>,
-	Body = <<"echo=", AppendedBody/binary>>,
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/echo/body_qs", Config),
-		[{<<"connection">>, <<"close">>}],
-		Body, Client),
-	{ok, 413, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body_qs", [], << "echo=", 0:15996/unit:8 >>),
+	{response, nofin, 413, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 error_chain_handle_after_reply(Config) ->
 error_chain_handle_after_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=handle_after_reply", Config), Client2),
-	{ok, 200, _, Client4} = cowboy_client:response(Client3),
-	{ok, 200, _, Client5} = cowboy_client:response(Client4),
-	{error, closed} = cowboy_client:response(Client5).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref1 = gun:get(ConnPid, "/"),
+	Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 error_chain_handle_before_reply(Config) ->
 error_chain_handle_before_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=handle_before_reply", Config), Client2),
-	{ok, 200, _, Client4} = cowboy_client:response(Client3),
-	{ok, 500, _, Client5} = cowboy_client:response(Client4),
-	{error, closed} = cowboy_client:response(Client5).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref1 = gun:get(ConnPid, "/"),
+	Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
+	{response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 error_handle_after_reply(Config) ->
 error_handle_after_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=handle_after_reply", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{error, closed} = cowboy_client:response(Client3).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 error_init_after_reply(Config) ->
 error_init_after_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=init_after_reply", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{error, closed} = cowboy_client:response(Client3).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 error_init_reply_handle_error(Config) ->
 error_init_reply_handle_error(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=init_reply_handle_error", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{error, closed} = cowboy_client:response(Client3).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 headers_dupe(Config) ->
 headers_dupe(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/headers/dupe", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
-	{<<"connection">>, <<"close">>}
-		= lists:keyfind(<<"connection">>, 1, Headers),
-	Connections = [H || H = {Name, _} <- Headers, Name =:= <<"connection">>],
-	1 = length(Connections),
-	{error, closed} = cowboy_client:response(Client3).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref = gun:get(ConnPid, "/headers/dupe"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+	%% Ensure that only one connection header was received.
+	[<<"close">>] = [V || {Name, V} <- Headers, Name =:= <<"connection">>],
+	gun_is_gone(ConnPid, MRef).
 
 
 http10_chunkless(Config) ->
 http10_chunkless(Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
-	Data = "GET /chunked_response HTTP/1.0\r\nHost: localhost\r\n\r\n",
-	{ok, Client3} = cowboy_client:raw_request(Data, Client2),
-	{ok, 200, Headers, Client4} = cowboy_client:response(Client3),
+	{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+	Ref = gun:get(ConnPid, "/chunked_response"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
 	false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
 	false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
-	%% Hack: we just try to get 28 bytes and compare.
-	{ok, Transport, Socket} = cowboy_client:transport(Client4),
-	Buffer = element(7, Client4),
-	Buffer2 = case Transport:recv(Socket, 28 - byte_size(Buffer), 1000) of
-		{ok, Recv} -> << Buffer/binary, Recv/binary >>;
-		_ -> Buffer
-	end,
-	<<"chunked_handler\r\nworks fine!">> = Buffer2.
+	{ok, <<"chunked_handler\r\nworks fine!">>} = gun:await_body(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 http10_hostless(Config) ->
 http10_hostless(Config) ->
 	Port10 = ?config(port, Config) + 10,
 	Port10 = ?config(port, Config) + 10,
@@ -694,66 +581,48 @@ http10_hostless(Config) ->
 	cowboy:stop_listener(http10).
 	cowboy:stop_listener(http10).
 
 
 keepalive_max(Config) ->
 keepalive_max(Config) ->
-	Client = ?config(client, Config),
-	URL = build_url("/", Config),
-	ok = keepalive_max_loop(Client, URL, 50).
-
-keepalive_max_loop(Client, _, 0) ->
-	{error, closed} = cowboy_client:response(Client),
-	ok;
-keepalive_max_loop(Client, URL, N) ->
-	Headers = [{<<"connection">>, <<"keep-alive">>}],
-	{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
-	{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
-	Expected = case N of
-		1 -> <<"close">>;
-		N -> <<"keep-alive">>
-	end,
-	{<<"connection">>, Expected}
-		= lists:keyfind(<<"connection">>, 1, RespHeaders),
-	keepalive_max_loop(Client3, URL, N - 1).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
+		|| _ <- lists:seq(1, 49)],
+	CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
+	_ = [begin
+		{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+		{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+	end || Ref <- Refs],
+	{response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
+	{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
+	gun_is_gone(ConnPid, MRef).
 
 
 keepalive_nl(Config) ->
 keepalive_nl(Config) ->
-	Client = ?config(client, Config),
-	URL = build_url("/", Config),
-	ok = keepalive_nl_loop(Client, URL, 10).
-
-keepalive_nl_loop(Client, _, 0) ->
-	{error, closed} = cowboy_client:response(Client),
-	ok;
-keepalive_nl_loop(Client, URL, N) ->
-	Headers = [{<<"connection">>, <<"keep-alive">>}],
-	{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
-	{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
-	{<<"connection">>, <<"keep-alive">>}
-		= lists:keyfind(<<"connection">>, 1, RespHeaders),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
-	ok = Transport:send(Socket, <<"\r\n">>), %% empty line
-	keepalive_nl_loop(Client3, URL, N - 1).
+	ConnPid = gun_open(Config),
+	Refs = [begin
+		Ref = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
+		gun:dbg_send_raw(ConnPid, <<"\r\n">>),
+		Ref
+	end || _ <- lists:seq(1, 10)],
+	_ = [begin
+		{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+		{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+	end || Ref <- Refs],
+	ok.
 
 
 keepalive_stream_loop(Config) ->
 keepalive_stream_loop(Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
-	keepalive_stream_loop(Client2, 10).
-
-keepalive_stream_loop(Client, 0) ->
-	{error, closed} = cowboy_client:response(Client),
-	ok;
-keepalive_stream_loop(Client, N) ->
-	{ok, _} = cowboy_client:raw_request("POST /loop_stream_recv HTTP/1.1\r\n"
-		"Host: localhost\r\n"
-		"Connection: keepalive\r\n"
-		"Transfer-Encoding: chunked\r\n\r\n", Client),
-	_ = [{ok, _} = cowboy_client:raw_request(<<"4\r\n",Id:32,"\r\n">>, Client) ||
-		Id <- lists:seq(1, 250)],
-	{ok, _} = cowboy_client:raw_request(<<"0\r\n\r\n">>, Client),
-	{ok, 200, _, _} = cowboy_client:response(Client),
-	keepalive_stream_loop(Client, N-1).
+	ConnPid = gun_open(Config),
+	Refs = [begin
+		Ref = gun:post(ConnPid, "/loop_stream_recv",
+			[{<<"transfer-encoding">>, <<"chunked">>}]),
+		_ = [gun:data(ConnPid, Ref, nofin, << ID:32 >>)
+			|| ID <- lists:seq(1, 250)],
+		gun:data(ConnPid, Ref, fin, <<>>),
+		Ref
+	end || _ <- lists:seq(1, 10)],
+	_ = [begin
+		{response, fin, 200, _} = gun:await(ConnPid, Ref)
+	end || Ref <- Refs],
+	ok.
 
 
 multipart(Config) ->
 multipart(Config) ->
-	Client = ?config(client, Config),
+	ConnPid = gun_open(Config),
 	Body = <<
 	Body = <<
 		"This is a preamble."
 		"This is a preamble."
 		"\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
 		"\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
@@ -761,31 +630,30 @@ multipart(Config) ->
 		"\r\n--OHai--\r\n"
 		"\r\n--OHai--\r\n"
 		"This is an epilogue."
 		"This is an epilogue."
 	>>,
 	>>,
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/multipart", Config),
+	Ref = gun:post(ConnPid, "/multipart",
 		[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
 		[{<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}],
-		Body, Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, RespBody, _} = cowboy_client:response_body(Client3),
+		Body),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, RespBody} = gun:await_body(ConnPid, Ref),
 	Parts = binary_to_term(RespBody),
 	Parts = binary_to_term(RespBody),
 	Parts = [
 	Parts = [
 		{[{<<"x-name">>, <<"answer">>}], <<"42">>},
 		{[{<<"x-name">>, <<"answer">>}], <<"42">>},
 		{[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
 		{[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>}
-	].
+	],
+	ok.
 
 
 multipart_large(Config) ->
 multipart_large(Config) ->
-	Client = ?config(client, Config),
+	ConnPid = gun_open(Config),
 	Boundary = "----------",
 	Boundary = "----------",
 	Big = << 0:9000000/unit:8 >>,
 	Big = << 0:9000000/unit:8 >>,
 	Bigger = << 0:9999999/unit:8 >>,
 	Bigger = << 0:9999999/unit:8 >>,
 	Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
 	Body = ["--", Boundary, "\r\ncontent-length: 9000000\r\n\r\n", Big, "\r\n",
 		"--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
 		"--", Boundary, "\r\ncontent-length: 9999999\r\n\r\n", Bigger, "\r\n",
 		"--", Boundary, "--\r\n"],
 		"--", Boundary, "--\r\n"],
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/multipart/large", Config),
+	Ref = gun:post(ConnPid, "/multipart/large",
 		[{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
 		[{<<"content-type">>, ["multipart/x-large; boundary=", Boundary]}],
-		Body, Client),
-	{ok, 200, _, _} = cowboy_client:response(Client2),
+		Body),
+	{response, fin, 200, _} = gun:await(ConnPid, Ref),
 	ok.
 	ok.
 
 
 nc_reqs(Config, Input) ->
 nc_reqs(Config, Input) ->
@@ -812,20 +680,20 @@ nc_zero(Config) ->
 	nc_reqs(Config, "/dev/zero").
 	nc_reqs(Config, "/dev/zero").
 
 
 onrequest(Config) ->
 onrequest(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
 	{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
 	{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
-	{ok, <<"http_handler">>, _} = cowboy_client:response_body(Client3).
+	{ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 onrequest_reply(Config) ->
 onrequest_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/?reply=1", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/?reply=1"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
 	{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
 	{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
-	{ok, <<"replied!">>, _} = cowboy_client:response_body(Client3).
+	{ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 %% Hook for the above onrequest tests.
 %% Hook for the above onrequest tests.
 onrequest_hook(Req) ->
 onrequest_hook(Req) ->
@@ -839,12 +707,11 @@ onrequest_hook(Req) ->
 	end.
 	end.
 
 
 onresponse_capitalize(Config) ->
 onresponse_capitalize(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
-	{ok, Data} = Transport:recv(Socket, 0, 1000),
-	false = nomatch =:= binary:match(Data, <<"Content-Length">>).
+	Client = raw_open(Config),
+	ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),
+	Data = raw_recv_head(Client),
+	false = nomatch =:= binary:match(Data, <<"Content-Length">>),
+	ok.
 
 
 %% Hook for the above onresponse_capitalize test.
 %% Hook for the above onresponse_capitalize test.
 onresponse_capitalize_hook(Status, Headers, Body, Req) ->
 onresponse_capitalize_hook(Status, Headers, Body, Req) ->
@@ -854,20 +721,17 @@ onresponse_capitalize_hook(Status, Headers, Body, Req) ->
 	Req2.
 	Req2.
 
 
 onresponse_crash(Config) ->
 onresponse_crash(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/handler_errors?case=init_before_reply", Config), Client),
-	{ok, 777, Headers, _} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/handler_errors?case=init_before_reply"),
+	{response, fin, 777, Headers} = gun:await(ConnPid, Ref),
 	{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
 	{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers).
 
 
 onresponse_reply(Config) ->
 onresponse_reply(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, 777, Headers, Client3} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/"),
+	{response, nofin, 777, Headers} = gun:await(ConnPid, Ref),
 	{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
 	{<<"x-hook">>, <<"onresponse">>} = lists:keyfind(<<"x-hook">>, 1, Headers),
-	%% Make sure we don't get the body initially sent.
-	{error, closed} = cowboy_client:response_body(Client3).
+	ok.
 
 
 %% Hook for the above onresponse tests.
 %% Hook for the above onresponse tests.
 onresponse_hook(_, Headers, _, Req) ->
 onresponse_hook(_, Headers, _, Req) ->
@@ -876,192 +740,145 @@ onresponse_hook(_, Headers, _, Req) ->
 	Req2.
 	Req2.
 
 
 parse_host(Config) ->
 parse_host(Config) ->
+	ConnPid = gun_open(Config),
 	Tests = [
 	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]">>}
+		{<<"example.org:8080">>, <<"example.org\n8080">>},
+		{<<"example.org">>, <<"example.org\n80">>},
+		{<<"192.0.2.1:8080">>, <<"192.0.2.1\n8080">>},
+		{<<"192.0.2.1">>, <<"192.0.2.1\n80">>},
+		{<<"[2001:db8::1]:8080">>, <<"[2001:db8::1]\n8080">>},
+		{<<"[2001:db8::1]">>, <<"[2001:db8::1]\n80">>},
+		{<<"[::ffff:192.0.2.1]:8080">>, <<"[::ffff:192.0.2.1]\n8080">>},
+		{<<"[::ffff:192.0.2.1]">>, <<"[::ffff:192.0.2.1]\n80">>}
 	],
 	],
 	[begin
 	[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].
+		Ref = gun:get(ConnPid, "/req_attr?attr=host_and_port",
+			[{<<"host">>, Host}]),
+		{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+		{ok, Body} = gun:await_body(ConnPid, Ref)
+	end || {Host, Body} <- Tests],
+	ok.
 
 
 pipeline(Config) ->
 pipeline(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client2),
-	{ok, Client4} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client3),
-	{ok, Client5} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client4),
-	{ok, Client6} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), [{<<"connection">>, <<"close">>}], Client5),
-	{ok, 200, _, Client7} = cowboy_client:response(Client6),
-	{ok, 200, _, Client8} = cowboy_client:response(Client7),
-	{ok, 200, _, Client9} = cowboy_client:response(Client8),
-	{ok, 200, _, Client10} = cowboy_client:response(Client9),
-	{ok, 200, _, Client11} = cowboy_client:response(Client10),
-	{error, closed} = cowboy_client:response(Client11).
+	ConnPid = gun_open(Config),
+	Refs = [gun:get(ConnPid, "/") || _ <- lists:seq(1, 5)],
+	_ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
+	ok.
 
 
 pipeline_long_polling(Config) ->
 pipeline_long_polling(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/long_polling", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/long_polling", Config), Client2),
-	{ok, 102, _, Client4} = cowboy_client:response(Client3),
-	{ok, 102, _, Client5} = cowboy_client:response(Client4),
-	{error, closed} = cowboy_client:response(Client5).
+	ConnPid = gun_open(Config),
+	Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)],
+	_ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs],
+	ok.
 
 
 rest_param_all(Config) ->
 rest_param_all(Config) ->
-	Client = ?config(client, Config),
-	URL = build_url("/param_all", Config),
-	% Accept without param
-	{ok, Client2} = cowboy_client:request(<<"GET">>, URL,
-		[{<<"accept">>, <<"text/plain">>}], Client),
-	Client3 = check_response(Client2, <<"[]">>),
-	% Accept with param
-	{ok, Client4} = cowboy_client:request(<<"GET">>, URL,
-		[{<<"accept">>, <<"text/plain;level=1">>}], Client3),
-	Client5 = check_response(Client4, <<"level=1">>),
-	% Accept with param and quality
-	{ok, Client6} = cowboy_client:request(<<"GET">>, URL,
-		[{<<"accept">>,
-			<<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}],
-		Client5),
-	Client7 = check_response(Client6, <<"level=1">>),
-	{ok, Client8} = cowboy_client:request(<<"GET">>, URL,
-		[{<<"accept">>,
-			<<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}],
-		Client7),
-	Client9 = check_response(Client8, <<"level=2">>),
-	% Without Accept
-	{ok, Client10} = cowboy_client:request(<<"GET">>, URL, [], Client9),
-	Client11 = check_response(Client10, <<"'*'">>),
-	% Content-Type without param
-	{ok, Client12} = cowboy_client:request(<<"PUT">>, URL,
-		[{<<"content-type">>, <<"text/plain">>}], Client11),
-	{ok, 204, _, Client13} = cowboy_client:response(Client12),
-	% Content-Type with param
-	{ok, Client14} = cowboy_client:request(<<"PUT">>, URL,
-		[{<<"content-type">>, <<"text/plain; charset=utf-8">>}], Client13),
-	{ok, 204, _, _} = cowboy_client:response(Client14).
-
-check_response(Client, Body) ->
-	{ok, 200, _, Client2} = cowboy_client:response(Client),
-	{ok, Body, Client3} = cowboy_client:response_body(Client2),
-	Client3.
+	ConnPid = gun_open(Config),
+	%% Accept without param.
+	Ref1 = gun:get(ConnPid, "/param_all",
+		[{<<"accept">>, <<"text/plain">>}]),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref1),
+	{ok, <<"[]">>} = gun:await_body(ConnPid, Ref1),
+	%% Accept with param.
+	Ref2 = gun:get(ConnPid, "/param_all",
+		[{<<"accept">>, <<"text/plain;level=1">>}]),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref2),
+	{ok, <<"level=1">>} = gun:await_body(ConnPid, Ref2),
+	%% Accept with param and quality.
+	Ref3 = gun:get(ConnPid, "/param_all",
+		[{<<"accept">>, <<"text/plain;level=1;q=0.8, text/plain;level=2;q=0.5">>}]),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref3),
+	{ok, <<"level=1">>} = gun:await_body(ConnPid, Ref3),
+	Ref4 = gun:get(ConnPid, "/param_all",
+		[{<<"accept">>, <<"text/plain;level=1;q=0.5, text/plain;level=2;q=0.8">>}]),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref4),
+	{ok, <<"level=2">>} = gun:await_body(ConnPid, Ref4),
+	%% Without Accept.
+	Ref5 = gun:get(ConnPid, "/param_all"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref5),
+	{ok, <<"'*'">>} = gun:await_body(ConnPid, Ref5),
+	%% Content-Type without param.
+	Ref6 = gun:put(ConnPid, "/param_all",
+		[{<<"content-type">>, <<"text/plain">>}]),
+	{response, fin, 204, _} = gun:await(ConnPid, Ref6),
+	%% Content-Type with param.
+	Ref7 = gun:put(ConnPid, "/param_all",
+		[{<<"content-type">>, <<"text/plain; charset=utf-8">>}]),
+	{response, fin, 204, _} = gun:await(ConnPid, Ref7),
+	ok.
 
 
 rest_bad_accept(Config) ->
 rest_bad_accept(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/bad_accept", Config),
-		[{<<"accept">>, <<"1">>}],
-		Client),
-	{ok, 400, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/bad_accept",
+		[{<<"accept">>, <<"1">>}]),
+	{response, fin, 400, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_bad_content_type(Config) ->
 rest_bad_content_type(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"PATCH">>,
-		build_url("/bad_content_type", Config),
-		[{<<"content-type">>, <<"text/plain, text/html">>}],
-		<<"Whatever">>, Client),
-	{ok, 415, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:patch(ConnPid, "/bad_content_type",
+		[{<<"content-type">>, <<"text/plain, text/html">>}], <<"Whatever">>),
+	{response, fin, 415, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_expires(Config) ->
 rest_expires(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/rest_expires", Config), Client),
-	{ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
-	{_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
-	{_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/rest_expires"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, Expires} = lists:keyfind(<<"expires">>, 1, Headers),
+	{_, LastModified} = lists:keyfind(<<"last-modified">>, 1, Headers),
 	Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
 	Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
 	ok.
 	ok.
 
 
 rest_keepalive(Config) ->
 rest_keepalive(Config) ->
-	Client = ?config(client, Config),
-	URL = build_url("/simple", Config),
-	ok = rest_keepalive_loop(Client, URL, 10).
-
-rest_keepalive_loop(_, _, 0) ->
-	ok;
-rest_keepalive_loop(Client, URL, N) ->
-	Headers = [{<<"connection">>, <<"keep-alive">>}],
-	{ok, Client2} = cowboy_client:request(<<"GET">>, URL, Headers, Client),
-	{ok, 200, RespHeaders, Client3} = cowboy_client:response(Client2),
-	{<<"connection">>, <<"keep-alive">>}
-		= lists:keyfind(<<"connection">>, 1, RespHeaders),
-	rest_keepalive_loop(Client3, URL, N - 1).
+	ConnPid = gun_open(Config),
+	Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
+	_ = [begin
+		{response, nofin, 200, Headers} =  gun:await(ConnPid, Ref),
+		{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+	end || Ref <- Refs],
+	ok.
 
 
 rest_keepalive_post(Config) ->
 rest_keepalive_post(Config) ->
-	Client = ?config(client, Config),
-	ok = rest_keepalive_post_loop(Config, Client, forbidden_post, 10).
-
-rest_keepalive_post_loop(_, _, _, 0) ->
-	ok;
-rest_keepalive_post_loop(Config, Client, simple_post, N) ->
-	Headers = [
-		{<<"connection">>, <<"keep-alive">>},
-		{<<"content-type">>, <<"text/plain">>}
-	],
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/simple_post", Config), Headers, "12345", Client),
-	{ok, 303, RespHeaders, Client3} = cowboy_client:response(Client2),
-	{<<"connection">>, <<"keep-alive">>}
-		= lists:keyfind(<<"connection">>, 1, RespHeaders),
-	rest_keepalive_post_loop(Config, Client3, forbidden_post, N - 1);
-rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
-	Headers = [
-		{<<"connection">>, <<"keep-alive">>},
-		{<<"content-type">>, <<"text/plain">>}
-	],
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/forbidden_post", Config), Headers, "12345", Client),
-	{ok, 403, RespHeaders, Client3} = cowboy_client:response(Client2),
-	{<<"connection">>, <<"keep-alive">>}
-		= lists:keyfind(<<"connection">>, 1, RespHeaders),
-	rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
+	ConnPid = gun_open(Config),
+	Refs = [{
+		gun:post(ConnPid, "/forbidden_post",
+			[{<<"content-type">>, <<"text/plain">>}]),
+		gun:post(ConnPid, "/simple_post",
+			[{<<"content-type">>, <<"text/plain">>}])
+	} || _ <- lists:seq(1, 5)],
+	_ = [begin
+		{response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
+		{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
+		{response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
+		{_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
+	end || {Ref1, Ref2} <- Refs],
+	ok.
 
 
 rest_missing_get_callbacks(Config) ->
 rest_missing_get_callbacks(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/missing_get_callbacks", Config), Client),
-	{ok, 500, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/missing_get_callbacks"),
+	{response, fin, 500, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_missing_put_callbacks(Config) ->
 rest_missing_put_callbacks(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"PUT">>,
-		build_url("/missing_put_callbacks", Config),
-		[{<<"content-type">>, <<"application/json">>}],
-		<<"{}">>, Client),
-	{ok, 500, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:put(ConnPid, "/missing_put_callbacks",
+		[{<<"content-type">>, <<"application/json">>}], <<"{}">>),
+	{response, fin, 500, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_nodelete(Config) ->
 rest_nodelete(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"DELETE">>,
-		build_url("/nodelete", Config), Client),
-	{ok, 500, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:delete(ConnPid, "/nodelete"),
+	{response, fin, 500, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_options_default(Config) ->
 rest_options_default(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"OPTIONS">>,
-		build_url("/rest_empty_resource", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:options(ConnPid, "/rest_empty_resource"),
+	{response, fin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"HEAD, GET, OPTIONS">>} = lists:keyfind(<<"allow">>, 1, Headers),
+	ok.
 
 
 rest_patch(Config) ->
 rest_patch(Config) ->
 	Tests = [
 	Tests = [
@@ -1070,40 +887,34 @@ rest_patch(Config) ->
 		{400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
 		{400, [{<<"content-type">>, <<"text/plain">>}], <<"halt">>},
 		{415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
 		{415, [{<<"content-type">>, <<"application/json">>}], <<"bad_content_type">>}
 	],
 	],
-	Client = ?config(client, Config),
+	ConnPid = gun_open(Config),
 	_ = [begin
 	_ = [begin
-		{ok, Client2} = cowboy_client:request(<<"PATCH">>,
-			build_url("/patch", Config), Headers, Body, Client),
-		{ok, Status, _, _} = cowboy_client:response(Client2),
-		ok
-	end || {Status, Headers, Body} <- Tests].
+		Ref = gun:patch(ConnPid, "/patch", Headers, Body),
+		{response, fin, Status, _} = gun:await(ConnPid, Ref)
+	end || {Status, Headers, Body} <- Tests],
+	ok.
 
 
 rest_post_charset(Config) ->
 rest_post_charset(Config) ->
-	Client = ?config(client, Config),
-	Headers = [
-		{<<"content-type">>, <<"text/plain;charset=UTF-8">>}
-	],
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/post_charset", Config), Headers, "12345", Client),
-	{ok, 204, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/post_charset",
+		[{<<"content-type">>, <<"text/plain;charset=UTF-8">>}], "12345"),
+	{response, fin, 204, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_postonly(Config) ->
 rest_postonly(Config) ->
-	Client = ?config(client, Config),
-	Headers = [
-		{<<"content-type">>, <<"text/plain">>}
-	],
-	{ok, Client2} = cowboy_client:request(<<"POST">>,
-		build_url("/postonly", Config), Headers, "12345", Client),
-	{ok, 204, _, _} = cowboy_client:response(Client2).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/postonly",
+		[{<<"content-type">>, <<"text/plain">>}], "12345"),
+	{response, fin, 204, _} = gun:await(ConnPid, Ref),
+	ok.
 
 
 rest_resource_get_etag(Config, Type) ->
 rest_resource_get_etag(Config, Type) ->
 	rest_resource_get_etag(Config, Type, []).
 	rest_resource_get_etag(Config, Type, []).
 
 
 rest_resource_get_etag(Config, Type, Headers) ->
 rest_resource_get_etag(Config, Type, Headers) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/resetags?type=" ++ Type, Config), Headers, Client),
-	{ok, Status, RespHeaders, _} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/resetags?type=" ++ Type, Headers),
+	{response, _, Status, RespHeaders} = gun:await(ConnPid, Ref),
 	case lists:keyfind(<<"etag">>, 1, RespHeaders) of
 	case lists:keyfind(<<"etag">>, 1, RespHeaders) of
 		false -> {Status, false};
 		false -> {Status, false};
 		{<<"etag">>, ETag} -> {Status, ETag}
 		{<<"etag">>, ETag} -> {Status, ETag}
@@ -1137,48 +948,44 @@ rest_resource_etags_if_none_match(Config) ->
 	end || {Status, ETag, Type} <- Tests].
 	end || {Status, ETag, Type} <- Tests].
 
 
 set_env_dispatch(Config) ->
 set_env_dispatch(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, 400, _, _} = cowboy_client:response(Client2),
+	ConnPid1 = gun_open(Config),
+	Ref1 = gun:get(ConnPid1, "/"),
+	{response, fin, 400, _} = gun:await(ConnPid1, Ref1),
 	ok = cowboy:set_env(set_env, dispatch,
 	ok = cowboy:set_env(set_env, dispatch,
 		cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
 		cowboy_router:compile([{'_', [{"/", http_handler, []}]}])),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/", Config), Client),
-	{ok, 200, _, _} = cowboy_client:response(Client3).
+	ConnPid2 = gun_open(Config),
+	Ref2 = gun:get(ConnPid2, "/"),
+	{response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
+	ok.
 
 
 set_resp_body(Config) ->
 set_resp_body(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/set_resp/body", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, <<"A flameless dance does not equal a cycle">>, _}
-		= cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/set_resp/body"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, <<"A flameless dance does not equal a cycle">>}
+		= gun:await_body(ConnPid, Ref),
+	ok.
 
 
 set_resp_header(Config) ->
 set_resp_header(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/set_resp/header", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{<<"vary">>, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
-	{<<"set-cookie">>, _} = lists:keyfind(<<"set-cookie">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/set_resp/header"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"Accept">>} = lists:keyfind(<<"vary">>, 1, Headers),
+	{_, _} = lists:keyfind(<<"set-cookie">>, 1, Headers),
+	ok.
 
 
 set_resp_overwrite(Config) ->
 set_resp_overwrite(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/set_resp/overwrite", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{<<"server">>, <<"DesireDrive/1.0">>}
-		= lists:keyfind(<<"server">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/set_resp/overwrite"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"DesireDrive/1.0">>} = lists:keyfind(<<"server">>, 1, Headers),
+	ok.
 
 
 slowloris(Config) ->
 slowloris(Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
+	Client = raw_open(Config),
 	try
 	try
 		[begin
 		[begin
-			{ok, _} = cowboy_client:raw_request([C], Client2),
+			ok = raw_send(Client, [C]),
 			receive after 25 -> ok end
 			receive after 25 -> ok end
 		end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
 		end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
 			"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
 			"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
@@ -1189,249 +996,214 @@ slowloris(Config) ->
 	end.
 	end.
 
 
 slowloris2(Config) ->
 slowloris2(Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
-	{ok, _} = cowboy_client:raw_request("GET / HTTP/1.1\r\n", Client2),
+	Client = raw_open(Config),
+	ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
 	receive after 300 -> ok end,
 	receive after 300 -> ok end,
-	{ok, _} = cowboy_client:raw_request("Host: localhost\r\n", Client2),
+	ok = raw_send(Client, "Host: localhost\r\n"),
 	receive after 300 -> ok end,
 	receive after 300 -> ok end,
-	{ok, 408, _, _} = cowboy_client:response(Client2).
+	Data = raw_recv_head(Client),
+	{_, 408, _, _} = cow_http:parse_status_line(Data),
+	ok.
 
 
 static_attribute_etag(Config) ->
 static_attribute_etag(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static_attribute_etag/index.html", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/static_attribute_etag/index.html", Config), Client2),
-	{ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
-	{ok, 200, Headers2, _} = cowboy_client:response(Client4),
-	{<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
-	{<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
-	false = ETag1 =:= undefined,
-	ETag1 = ETag2.
+	ConnPid = gun_open(Config),
+	Ref1 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
+	Ref2 = gun:get(ConnPid, "/static_attribute_etag/index.html"),
+	{response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
+	{response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
+	{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
+	{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
+	true = ETag =/= undefined,
+	ok.
 
 
 static_function_etag(Config) ->
 static_function_etag(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static_function_etag/index.html", Config), Client),
-	{ok, Client3} = cowboy_client:request(<<"GET">>,
-		build_url("/static_function_etag/index.html", Config), Client2),
-	{ok, 200, Headers1, Client4} = cowboy_client:response(Client3),
-	{ok, 200, Headers2, _} = cowboy_client:response(Client4),
-	{<<"etag">>, ETag1} = lists:keyfind(<<"etag">>, 1, Headers1),
-	{<<"etag">>, ETag2} = lists:keyfind(<<"etag">>, 1, Headers2),
-	false = ETag1 =:= undefined,
-	ETag1 = ETag2.
+	ConnPid = gun_open(Config),
+	Ref1 = gun:get(ConnPid, "/static_function_etag/index.html"),
+	Ref2 = gun:get(ConnPid, "/static_function_etag/index.html"),
+	{response, nofin, 200, Headers1} = gun:await(ConnPid, Ref1),
+	{response, nofin, 200, Headers2} = gun:await(ConnPid, Ref2),
+	{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers1),
+	{_, ETag} = lists:keyfind(<<"etag">>, 1, Headers2),
+	true = ETag =/= undefined,
+	ok.
 
 
 static_mimetypes_function(Config) ->
 static_mimetypes_function(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static_mimetypes_function/index.html", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{<<"content-type">>, <<"text/html">>}
-		= lists:keyfind(<<"content-type">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/static_mimetypes_function/index.html"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+	ok.
 
 
 static_specify_file(Config) ->
 static_specify_file(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static_specify_file", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
-	{<<"content-type">>, <<"text/css">>}
-		= lists:keyfind(<<"content-type">>, 1, Headers),
-	{ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/static_specify_file"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+	{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 static_specify_file_catchall(Config) ->
 static_specify_file_catchall(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static_specify_file/none", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
-	{<<"content-type">>, <<"text/css">>}
-		= lists:keyfind(<<"content-type">>, 1, Headers),
-	{ok, <<"body{color:red}\n">>, _} = cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/static_specify_file/none"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+	{ok, <<"body{color:red}\n">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 static_test_file(Config) ->
 static_test_file(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static/unknown", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{<<"content-type">>, <<"application/octet-stream">>}
-		= lists:keyfind(<<"content-type">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/static/unknown"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"application/octet-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+	ok.
 
 
 static_test_file_css(Config) ->
 static_test_file_css(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/static/style.css", Config), Client),
-	{ok, 200, Headers, _} = cowboy_client:response(Client2),
-	{<<"content-type">>, <<"text/css">>}
-		= lists:keyfind(<<"content-type">>, 1, Headers).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/static/style.css"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
+	{_, <<"text/css">>} = lists:keyfind(<<"content-type">>, 1, Headers),
+	ok.
 
 
 stream_body_set_resp(Config) ->
 stream_body_set_resp(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/stream_body/set_resp", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, <<"stream_body_set_resp">>, _}
-		= cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/stream_body/set_resp"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, <<"stream_body_set_resp">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 stream_body_set_resp_close(Config) ->
 stream_body_set_resp_close(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/stream_body/set_resp_close", Config), Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Transport, Socket} = cowboy_client:transport(Client3),
-	case element(7, Client3) of
-		<<"stream_body_set_resp_close">> ->
-			ok;
-		Buffer ->
-			{ok, Rest} = Transport:recv(Socket, 26 - byte_size(Buffer), 1000),
-			<<"stream_body_set_resp_close">> = << Buffer/binary, Rest/binary >>,
-			ok
-	end,
-	{error, closed} = Transport:recv(Socket, 0, 1000).
+	{ConnPid, MRef} = gun_monitor_open(Config),
+	Ref = gun:get(ConnPid, "/stream_body/set_resp_close"),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
+	{ok, <<"stream_body_set_resp_close">>} = gun:await_body(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
 stream_body_set_resp_chunked(Config) ->
 stream_body_set_resp_chunked(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/stream_body/set_resp_chunked", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
 	{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
 	{_, <<"chunked">>} = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
-	{ok, Transport, Socket} = cowboy_client:transport(Client3),
-	case element(7, Client3) of
-		<<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">> ->
-			ok;
-		Buffer ->
-			{ok, Rest} = Transport:recv(Socket, 44 - byte_size(Buffer), 1000),
-			<<"B\r\nstream_body\r\n11\r\n_set_resp_chunked\r\n0\r\n\r\n">>
-				= <<Buffer/binary, Rest/binary>>,
-			ok
-	end.
+	{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 stream_body_set_resp_chunked10(Config) ->
 stream_body_set_resp_chunked10(Config) ->
-	Client = ?config(client, Config),
-	Transport = ?config(transport, Config),
-	{ok, Client2} = cowboy_client:connect(
-		Transport, "localhost", ?config(port, Config), Client),
-	Data = ["GET /stream_body/set_resp_chunked HTTP/1.0\r\n",
-		"Host: localhost\r\n\r\n"],
-	{ok, Client3} = cowboy_client:raw_request(Data, Client2),
-	{ok, 200, Headers, Client4} = cowboy_client:response(Client3),
-	false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
-	{ok, Transport, Socket} = cowboy_client:transport(Client4),
-	case element(7, Client4) of
-		<<"stream_body_set_resp_chunked">> ->
-			ok;
-		Buffer ->
-			{ok, Rest} = Transport:recv(Socket, 28 - byte_size(Buffer), 1000),
-			<<"stream_body_set_resp_chunked">>
-				= <<Buffer/binary, Rest/binary>>,
-			ok
-	end,
-	{error, closed} = Transport:recv(Socket, 0, 1000).
+	{ConnPid, MRef} = gun_monitor_open(Config, [{http, [{version, 'HTTP/1.0'}]}]),
+	Ref = gun:get(ConnPid, "/stream_body/set_resp_chunked"),
+	{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
+	false = lists:keyfind(<<"transfer-encoding">>, 1, Headers),
+	{ok, <<"stream_body_set_resp_chunked">>} = gun:await_body(ConnPid, Ref, MRef),
+	gun_is_gone(ConnPid, MRef).
 
 
+%% Undocumented hack: force chunked response to be streamed as HTTP/1.1.
 streamed_response(Config) ->
 streamed_response(Config) ->
-	Client = ?config(client, Config),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/streamed_response", Config), Client),
-	{ok, 200, Headers, Client3} = cowboy_client:response(Client2),
+	Client = raw_open(Config),
+	ok = raw_send(Client, "GET /streamed_response HTTP/1.1\r\nHost: localhost\r\n\r\n"),
+	Data = raw_recv_head(Client),
+	{'HTTP/1.1', 200, _, Rest} = cow_http:parse_status_line(Data),
+	{Headers, Rest2} = cow_http:parse_headers(Rest),
 	false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
 	false = lists:keymember(<<"transfer-encoding">>, 1, Headers),
-	{ok, Transport, Socket} = cowboy_client:transport(Client3),
-	{ok, <<"streamed_handler\r\nworks fine!">>}
-		= Transport:recv(Socket, 29, 1000),
-	{error, closed} = cowboy_client:response(Client3).
+	Rest2Size = byte_size(Rest2),
+	ok = case <<"streamed_handler\r\nworks fine!">> of
+		Rest2 -> ok;
+		<< Rest2:Rest2Size/binary, Expect/bits >> -> raw_expect_recv(Client, Expect)
+	end.
 
 
 te_chunked(Config) ->
 te_chunked(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
-	Chunks = body_to_chunks(50, Body, []),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config),
-		[{<<"transfer-encoding">>, <<"chunked">>}],
-		Chunks, Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body",
+		[{<<"transfer-encoding">>, <<"chunked">>}], Body),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.
+
+body_to_chunks(_, <<>>, Acc) ->
+	lists:reverse([<<"0\r\n\r\n">>|Acc]);
+body_to_chunks(ChunkSize, Body, Acc) ->
+	BodySize = byte_size(Body),
+	ChunkSize2 = case BodySize < ChunkSize of
+		true -> BodySize;
+		false -> ChunkSize
+	end,
+	<< Chunk:ChunkSize2/binary, Rest/binary >> = Body,
+	ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)),
+	body_to_chunks(ChunkSize, Rest,
+		[<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]).
 
 
 te_chunked_chopped(Config) ->
 te_chunked_chopped(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
 	Body2 = iolist_to_binary(body_to_chunks(50, Body, [])),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config),
-		[{<<"transfer-encoding">>, <<"chunked">>}], Client),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body",
+		[{<<"transfer-encoding">>, <<"chunked">>}]),
 	_ = [begin
 	_ = [begin
-		ok = Transport:send(Socket, << C >>),
-		ok = timer:sleep(10)
+		ok = gun:dbg_send_raw(ConnPid, << C >>),
+		receive after 10 -> ok end
 	end || << C >> <= Body2],
 	end || << C >> <= Body2],
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 te_chunked_delayed(Config) ->
 te_chunked_delayed(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Chunks = body_to_chunks(50, Body, []),
 	Chunks = body_to_chunks(50, Body, []),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config),
-		[{<<"transfer-encoding">>, <<"chunked">>}], Client),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body",
+		[{<<"transfer-encoding">>, <<"chunked">>}]),
 	_ = [begin
 	_ = [begin
-		ok = Transport:send(Socket, Chunk),
-		ok = timer:sleep(10)
+		ok = gun:dbg_send_raw(ConnPid, Chunk),
+		receive after 10 -> ok end
 	end || Chunk <- Chunks],
 	end || Chunk <- Chunks],
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 te_chunked_split_body(Config) ->
 te_chunked_split_body(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Chunks = body_to_chunks(50, Body, []),
 	Chunks = body_to_chunks(50, Body, []),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config),
-		[{<<"transfer-encoding">>, <<"chunked">>}], Client),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body",
+		[{<<"transfer-encoding">>, <<"chunked">>}]),
 	_ = [begin
 	_ = [begin
 		case Chunk of
 		case Chunk of
-			%% Final chunk.
 			<<"0\r\n\r\n">> ->
 			<<"0\r\n\r\n">> ->
-				ok = Transport:send(Socket, Chunk);
+				ok = gun:dbg_send_raw(ConnPid, Chunk);
 			_ ->
 			_ ->
-				%% Chunk of form <<"9\r\nChunkBody\r\n">>.
 				[Size, ChunkBody, <<>>] =
 				[Size, ChunkBody, <<>>] =
 					binary:split(Chunk, [<<"\r\n">>], [global]),
 					binary:split(Chunk, [<<"\r\n">>], [global]),
 				PartASize = random:uniform(byte_size(ChunkBody)),
 				PartASize = random:uniform(byte_size(ChunkBody)),
 				<<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
 				<<PartA:PartASize/binary, PartB/binary>> = ChunkBody,
-				ok = Transport:send(Socket, [Size, <<"\r\n">>, PartA]),
-				ok = timer:sleep(10),
-				ok = Transport:send(Socket, [PartB, <<"\r\n">>])
+				ok = gun:dbg_send_raw(ConnPid, [Size, <<"\r\n">>, PartA]),
+				receive after 10 -> ok end,
+				ok = gun:dbg_send_raw(ConnPid, [PartB, <<"\r\n">>])
 		end
 		end
 	end || Chunk <- Chunks],
 	end || Chunk <- Chunks],
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 te_chunked_split_crlf(Config) ->
 te_chunked_split_crlf(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Chunks = body_to_chunks(50, Body, []),
 	Chunks = body_to_chunks(50, Body, []),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config),
-		[{<<"transfer-encoding">>, <<"chunked">>}], Client),
-	{ok, Transport, Socket} = cowboy_client:transport(Client2),
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body",
+		[{<<"transfer-encoding">>, <<"chunked">>}]),
 	_ = [begin
 	_ = [begin
-		%% <<"\r\n">> is last 2 bytes of Chunk split before or after <<"\r">>.
+		%% Split in the newline just before the end of the chunk.
 		Len = byte_size(Chunk) - (random:uniform(2) - 1),
 		Len = byte_size(Chunk) - (random:uniform(2) - 1),
-		<<Chunk2:Len/binary, End/binary>> = Chunk,
-		ok = Transport:send(Socket, Chunk2),
-		ok = timer:sleep(10),
-		ok = Transport:send(Socket, End)
+		<< Chunk2:Len/binary, End/binary >> = Chunk,
+		ok = gun:dbg_send_raw(ConnPid, Chunk2),
+		receive after 10 -> ok end,
+		ok = gun:dbg_send_raw(ConnPid, End)
 	end || Chunk <- Chunks],
 	end || Chunk <- Chunks],
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.
 
 
 te_identity(Config) ->
 te_identity(Config) ->
-	Client = ?config(client, Config),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
 	Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])),
-	{ok, Client2} = cowboy_client:request(<<"GET">>,
-		build_url("/echo/body", Config), [], Body, Client),
-	{ok, 200, _, Client3} = cowboy_client:response(Client2),
-	{ok, Body, _} = cowboy_client:response_body(Client3).
+	ConnPid = gun_open(Config),
+	Ref = gun:post(ConnPid, "/echo/body", [], Body),
+	{response, nofin, 200, _} = gun:await(ConnPid, Ref),
+	{ok, Body} = gun:await_body(ConnPid, Ref),
+	ok.

+ 17 - 24
test/http_SUITE_data/http_loop_stream_recv.erl

@@ -8,34 +8,27 @@
 init({_, http}, Req, _) ->
 init({_, http}, Req, _) ->
 	receive after 100 -> ok end,
 	receive after 100 -> ok end,
 	self() ! stream,
 	self() ! stream,
-	{loop, Req, 1, 100}.
+	{loop, Req, undefined, 100}.
 
 
-info(stream, Req, Id) ->
-	case stream_next(Req) of
-		{ok, Id, Req2} ->
-			info(stream, Req2, Id+1);
+info(stream, Req, undefined) ->
+	stream(Req, 1, <<>>).
+
+stream(Req, ID, Acc) ->
+	case cowboy_req:stream_body(Req) of
+		{ok, Data, Req2} ->
+			parse_id(Req2, ID, << Acc/binary, Data/binary >>);
 		{done, Req2} ->
 		{done, Req2} ->
 			{ok, Req3} = cowboy_req:reply(200, Req2),
 			{ok, Req3} = cowboy_req:reply(200, Req2),
-			{ok, Req3, Id}
+			{ok, Req3, undefined}
+	end.
+
+parse_id(Req, ID, Data) ->
+	case Data of
+		<< ID:32, Rest/bits >> ->
+			parse_id(Req, ID + 1, Rest);
+		_ ->
+			stream(Req, ID, Data)
 	end.
 	end.
 
 
 terminate({normal, shutdown}, _, _) ->
 terminate({normal, shutdown}, _, _) ->
 	ok.
 	ok.
-
-stream_next(Req) ->
-	stream_next(<<>>, Req).
-
-stream_next(Buffer, Req) ->
-	case cowboy_req:stream_body(Req) of
-		{ok, Packet, Req2} ->
-			case <<Buffer/binary, Packet/binary>> of
-				<<Id:32>> ->
-					{ok, Id, Req2};
-				Buffer2 when byte_size(Buffer2) < 4 ->
-					stream_next(Buffer2, Req2);
-				_InvalidBuffer ->
-					{error, invalid_chunk}
-			end;
-		Other ->
-			Other
-	end.