1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/common/tests/unit/head_http.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,29 @@ 1.4 + /* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-common/utils.js"); 1.8 + 1.9 +function basic_auth_header(user, password) { 1.10 + return "Basic " + btoa(user + ":" + CommonUtils.encodeUTF8(password)); 1.11 +} 1.12 + 1.13 +function basic_auth_matches(req, user, password) { 1.14 + if (!req.hasHeader("Authorization")) { 1.15 + return false; 1.16 + } 1.17 + 1.18 + let expected = basic_auth_header(user, CommonUtils.encodeUTF8(password)); 1.19 + return req.getHeader("Authorization") == expected; 1.20 +} 1.21 + 1.22 +function httpd_basic_auth_handler(body, metadata, response) { 1.23 + if (basic_auth_matches(metadata, "guest", "guest")) { 1.24 + response.setStatusLine(metadata.httpVersion, 200, "OK, authorized"); 1.25 + response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); 1.26 + } else { 1.27 + body = "This path exists and is protected - failed"; 1.28 + response.setStatusLine(metadata.httpVersion, 401, "Unauthorized"); 1.29 + response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); 1.30 + } 1.31 + response.bodyOutputStream.write(body, body.length); 1.32 +}