aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2016-06-14 11:30:42 +0200
committerLoïc Hoguin <[email protected]>2016-06-14 11:30:42 +0200
commita55679b231b61e7d2269142efa337ae6face2343 (patch)
treed001d12e8601113bd3f354ba9f442941f5f2926f /test
parenta4fb56018ec396e4d889f76cf7ef5c73fbe82cc1 (diff)
downloadcowboy-a55679b231b61e7d2269142efa337ae6face2343.tar.gz
cowboy-a55679b231b61e7d2269142efa337ae6face2343.tar.bz2
cowboy-a55679b231b61e7d2269142efa337ae6face2343.zip
Fix rest_basic_auth example
Diffstat (limited to 'test')
-rw-r--r--test/examples_SUITE.erl41
1 files changed, 34 insertions, 7 deletions
diff --git a/test/examples_SUITE.erl b/test/examples_SUITE.erl
index 42d0be8..b946625 100644
--- a/test/examples_SUITE.erl
+++ b/test/examples_SUITE.erl
@@ -185,17 +185,25 @@ rest_hello_world(Config) ->
end.
do_rest_hello_world(Transport, Protocol, Config) ->
- << "<html>", _/bits >> = do_rest_get(Transport, Protocol, "/", undefined, Config),
- << "REST Hello World as text!" >> = do_rest_get(Transport, Protocol, "/", <<"text/plain">>, Config),
- << "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
- not_acceptable = do_rest_get(Transport, Protocol, "/", <<"text/css">>, Config),
+ << "<html>", _/bits >> = do_rest_get(Transport, Protocol,
+ "/", undefined, undefined, Config),
+ << "REST Hello World as text!" >> = do_rest_get(Transport, Protocol,
+ "/", <<"text/plain">>, undefined, Config),
+ << "{\"rest\": \"Hello World!\"}" >> = do_rest_get(Transport, Protocol,
+ "/", <<"application/json">>, undefined, Config),
+ not_acceptable = do_rest_get(Transport, Protocol,
+ "/", <<"text/css">>, undefined, Config),
ok.
-do_rest_get(Transport, Protocol, Path, Accept, Config) ->
- ReqHeaders = case Accept of
+do_rest_get(Transport, Protocol, Path, Accept, Auth, Config) ->
+ ReqHeaders0 = case Accept of
undefined -> [];
_ -> [{<<"accept">>, Accept}]
end,
+ ReqHeaders = case Auth of
+ undefined -> ReqHeaders0;
+ _ -> [{<<"authorization">>, [<<"Basic ">>, base64:encode(Auth)]}|ReqHeaders0]
+ end,
case do_get(Transport, Protocol, Path, ReqHeaders, Config) of
{200, RespHeaders, Body} ->
Accept = case Accept of
@@ -205,10 +213,29 @@ do_rest_get(Transport, Protocol, Path, Accept, Config) ->
ContentType
end,
Body;
+ {401, _, _} ->
+ unauthorized;
{406, _, _} ->
not_acceptable
end.
+%% REST basic auth.
+
+rest_basic_auth(Config) ->
+ doc("REST basic authorization example."),
+ try
+ do_compile_and_start(rest_basic_auth),
+ do_rest_basic_auth(tcp, http, Config),
+ do_rest_basic_auth(tcp, http2, Config)
+ after
+ do_stop(rest_basic_auth)
+ end.
+
+do_rest_basic_auth(Transport, Protocol, Config) ->
+ unauthorized = do_rest_get(Transport, Protocol, "/", undefined, undefined, Config),
+ <<"Hello, Alladin!\n">> = do_rest_get(Transport, Protocol, "/", undefined, "Alladin:open sesame", Config),
+ ok.
+
%% File server.
file_server(Config) ->
@@ -225,7 +252,7 @@ do_file_server(Transport, Protocol, Config) ->
%% Directory.
{200, DirHeaders, <<"<!DOCTYPE html><html>", _/bits >>} = do_get(Transport, Protocol, "/", Config),
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, DirHeaders),
- _ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, Config),
+ _ = do_rest_get(Transport, Protocol, "/", <<"application/json">>, undefined, Config),
%% Files.
{200, _, _} = do_get(Transport, Protocol, "/small.mp4", Config),
{200, _, _} = do_get(Transport, Protocol, "/small.ogv", Config),