aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/http_SUITE.erl24
-rw-r--r--test/http_handler_multipart.erl29
2 files changed, 51 insertions, 2 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 002ac4d..22ebb51 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -1,4 +1,5 @@
%% Copyright (c) 2011, Loïc Hoguin <[email protected]>
+%% Copyright (c) 2011, Anthony Ramine <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
@@ -23,7 +24,7 @@
pipeline/1, raw/1, set_resp_header/1, set_resp_overwrite/1,
set_resp_body/1, stream_body_set_resp/1, response_as_req/1,
static_mimetypes_function/1, static_attribute_etag/1,
- static_function_etag/1]). %% http.
+ static_function_etag/1, multipart/1]). %% http.
-export([http_200/1, http_404/1, handler_errors/1,
file_200/1, file_403/1, dir_403/1, file_404/1,
file_400/1]). %% http and https.
@@ -43,7 +44,7 @@ groups() ->
set_resp_header, set_resp_overwrite,
set_resp_body, response_as_req, stream_body_set_resp,
static_mimetypes_function, static_attribute_etag,
- static_function_etag] ++ BaseTests},
+ static_function_etag, multipart] ++ BaseTests},
{https, [], BaseTests},
{misc, [], [http_10_hostless]},
{rest, [], [rest_simple, rest_keepalive]}].
@@ -144,6 +145,7 @@ init_http_dispatch(Config) ->
{[<<"static_function_etag">>, '...'], cowboy_http_static,
[{directory, ?config(static_dir, Config)},
{etag, {fun static_function_etag/2, etag_data}}]},
+ {[<<"multipart">>], http_handler_multipart, []},
{[], http_handler, []}
]}
].
@@ -236,6 +238,24 @@ max_keepalive_loop(Socket, N) ->
end,
keepalive_nl_loop(Socket, N - 1).
+multipart(Config) ->
+ Url = build_url("/multipart", Config),
+ Body = <<
+ "This is a preamble."
+ "\r\n--OHai\r\nX-Name:answer\r\n\r\n42"
+ "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n"
+ "\r\n--OHai--"
+ "This is an epiloque."
+ >>,
+ Request = {Url, [], "multipart/x-makes-no-sense; boundary=OHai", Body},
+ {ok, {{"HTTP/1.1", 200, "OK"}, _Headers, Response}} =
+ httpc:request(post, Request, [], [{body_format, binary}]),
+ Parts = binary_to_term(Response),
+ Parts = [
+ {[{<<"X-Name">>, <<"answer">>}], <<"42">>},
+ {[{'Server', <<"Cowboy">>}], <<"It rocks!\r\n">>}
+ ].
+
nc_rand(Config) ->
nc_reqs(Config, "/dev/urandom").
diff --git a/test/http_handler_multipart.erl b/test/http_handler_multipart.erl
new file mode 100644
index 0000000..f5f7919
--- /dev/null
+++ b/test/http_handler_multipart.erl
@@ -0,0 +1,29 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(http_handler_multipart).
+-behaviour(cowboy_http_handler).
+-export([init/3, handle/2, terminate/2]).
+
+init({_Transport, http}, Req, []) ->
+ {ok, Req, {}}.
+
+handle(Req, State) ->
+ {Result, Req2} = acc_multipart(Req, []),
+ {ok, Req3} = cowboy_http_req:reply(200, [], term_to_binary(Result), Req2),
+ {ok, Req3, State}.
+
+terminate(_Req, _State) ->
+ ok.
+
+acc_multipart(Req, Acc) ->
+ {Result, Req2} = cowboy_http_req:multipart_data(Req),
+ acc_multipart(Req2, Acc, Result).
+
+acc_multipart(Req, Acc, {headers, Headers}) ->
+ acc_multipart(Req, [{Headers, []}|Acc]);
+acc_multipart(Req, [{Headers, BodyAcc}|Acc], {body, Data}) ->
+ acc_multipart(Req, [{Headers, [Data|BodyAcc]}|Acc]);
+acc_multipart(Req, [{Headers, BodyAcc}|Acc], end_of_part) ->
+ acc_multipart(Req, [{Headers, list_to_binary(lists:reverse(BodyAcc))}|Acc]);
+acc_multipart(Req, Acc, eof) ->
+ {lists:reverse(Acc), Req}.