From a55679b231b61e7d2269142efa337ae6face2343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 14 Jun 2016 11:30:42 +0200 Subject: Fix rest_basic_auth example --- test/examples_SUITE.erl | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'test') 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) -> - << "", _/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), + << "", _/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, <<"", _/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), -- cgit v1.2.3