aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/http_SUITE.erl31
-rw-r--r--test/http_handler_multipart.erl29
-rw-r--r--test/rest_forbidden_resource.erl5
3 files changed, 53 insertions, 12 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 74d24b6..bad91a8 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, rest_keepalive_post]}].
@@ -146,6 +147,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, []}
]}
].
@@ -238,6 +240,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").
@@ -576,23 +596,18 @@ rest_keepalive_post(Config) ->
rest_keepalive_post_loop(_Socket, 0, _) ->
ok;
rest_keepalive_post_loop(Socket, N, simple_post) ->
- ct:print("simple_post~n"),
ok = gen_tcp:send(Socket, "POST /simple_post HTTP/1.1\r\n"
"Host: localhost\r\nConnection: keep-alive\r\n"
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
- ct:print("data ~p~n", [Data]),
- {0, 12} = binary:match(Data, <<"HTTP/1.1 200">>),
+ {0, 12} = binary:match(Data, <<"HTTP/1.1 303">>),
nomatch = binary:match(Data, <<"Connection: close">>),
rest_keepalive_post_loop(Socket, N - 1, forbidden_post);
rest_keepalive_post_loop(Socket, N, forbidden_post) ->
- ct:print("forbidden~n"),
ok = gen_tcp:send(Socket, "POST /forbidden_post HTTP/1.1\r\n"
"Host: localhost\r\nConnection: keep-alive\r\n"
"Content-Length: 5\r\nContent-Type: text/plain\r\n\r\n12345"),
{ok, Data} = gen_tcp:recv(Socket, 0, 6000),
- ct:print("data ~p~n", [Data]),
{0, 12} = binary:match(Data, <<"HTTP/1.1 403">>),
nomatch = binary:match(Data, <<"Connection: close">>),
rest_keepalive_post_loop(Socket, N - 1, simple_post).
-
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}.
diff --git a/test/rest_forbidden_resource.erl b/test/rest_forbidden_resource.erl
index 5c6aca0..90dee84 100644
--- a/test/rest_forbidden_resource.erl
+++ b/test/rest_forbidden_resource.erl
@@ -7,10 +7,7 @@ init(_Transport, _Req, _Opts) ->
{upgrade, protocol, cowboy_http_rest}.
rest_init(Req, [Forbidden]) ->
- {Headers, Req2} = cowboy_http_req:headers(Req),
- {Method, Req3} = cowboy_http_req:method(Req2),
- ct:print("method ~p headers ~p", [Method, Headers]),
- {ok, Req3, Forbidden}.
+ {ok, Req, Forbidden}.
allowed_methods(Req, State) ->
{['GET', 'HEAD', 'POST'], Req, State}.