Skip to content

Commit

Permalink
more improvements to #114 test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Aug 1, 2013
1 parent 8bb260f commit 4b77ea0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
55 changes: 20 additions & 35 deletions src/mochiweb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,31 @@ all_loaded(Base) ->
end,
lists:foldl(F, [], code:all_loaded()).

%% See the erlang:decode_packet/3 docs for the full type
-spec uri(HttpUri :: term()) -> string().
uri({abs_path, Uri}) ->
Uri;
%% TODO:
%% This makes it hard to implement certain kinds of proxies with mochiweb,
%% perhaps a field could be added to the mochiweb_request record to preserve
%% this information in raw_path.
uri({absoluteURI, _Protocol, _Host, _Port, Uri}) ->
Uri;
%% From http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2
uri('*') ->
"*";
%% Erlang decode_packet will return this for requests like `CONNECT host:port`
uri({scheme, Hostname, Port}) ->
Hostname ++ ":" ++ Port;
uri(HttpString) when is_list(HttpString) ->
HttpString.

%% @spec new_request({Socket, Request, Headers}) -> MochiWebRequest
%% @doc Return a mochiweb_request data structure.
new_request({Socket, {Method, {abs_path, Uri}, Version}, Headers}) ->
mochiweb_request:new(Socket,
Method,
Uri,
Version,
mochiweb_headers:make(Headers));
% this case probably doesn't "exist".
new_request({Socket, {Method, {absoluteURI, _Protocol, _Host, _Port, Uri},
Version}, Headers}) ->
mochiweb_request:new(Socket,
Method,
Uri,
Version,
mochiweb_headers:make(Headers));
%% Request-URI is "*"
%% From http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2
new_request({Socket, {Method, '*'=Uri, Version}, Headers}) ->
mochiweb_request:new(Socket,
Method,
Uri,
Version,
mochiweb_headers:make(Headers));
%% Request URI is a scheme
%% Erlang decode_package will return this for requests like `CONNECT example:port`
new_request({Socket, {Method, {scheme, Hostname, Port}, Version}, Headers}) ->
Uri = Hostname ++ ":" ++ Port,
mochiweb_request:new(Socket,
Method,
Uri,
Version,
mochiweb_headers:make(Headers));
%% Request is an HTTP string
%% Erlang decode_package will return this for requests like `GET example`
new_request({Socket, {Method, HttpString, Version}, Headers}) when is_list(HttpString) ->
new_request({Socket, {Method, HttpUri, Version}, Headers}) ->
mochiweb_request:new(Socket,
Method,
HttpString,
uri(HttpUri),
Version,
mochiweb_headers:make(Headers)).

Expand Down
22 changes: 21 additions & 1 deletion test/mochiweb_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,34 @@ hundred_128_https_POST_test_() -> % note the underscore
{timeout, ?LARGE_TIMEOUT,
fun() -> ?assertEqual(ok, do_POST(ssl, 128, 100)) end}.

single_GET_relative_test_() ->
single_GET_scheme_test_() ->
[{"ssl", ?_assertEqual(ok, do_GET("derp", ssl, 1))},
{"plain", ?_assertEqual(ok, do_GET("derp", plain, 1))}].

single_GET_absoluteURI_test_() ->
Uri = "https://example.com:123/x/",
ServerFun = fun (Req) ->
Req:ok({"text/plain", Req:get(path)})
end,
%% Note that all the scheme/host/port information is discarded from path
ClientFun = new_client_fun('GET', [#treq{path = Uri, xreply = <<"/x/">>}]),
[{atom_to_list(Transport),
?_assertEqual(ok, with_server(Transport, ServerFun, ClientFun))}
|| Transport <- [ssl, plain]].

single_CONNECT_test_() ->
[{"ssl", ?_assertEqual(ok, do_CONNECT(ssl, 1))},
{"plain", ?_assertEqual(ok, do_CONNECT(plain, 1))}].

single_GET_any_test_() ->
ServerFun = fun (Req) ->
Req:ok({"text/plain", Req:get(path)})
end,
ClientFun = new_client_fun('GET', [#treq{path = "*", xreply = <<"*">>}]),
[{atom_to_list(Transport),
?_assertEqual(ok, with_server(Transport, ServerFun, ClientFun))}
|| Transport <- [ssl, plain]].

do_CONNECT(Transport, Times) ->
PathPrefix = "example.com:",
ReplyPrefix = "You requested: ",
Expand Down

0 comments on commit 4b77ea0

Please sign in to comment.