1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 Test that a response to HEAD method should not have a body.
9 1. Create a GET request and write the response into cache.
10 2. Create the second GET request with the same URI and see if the response is
12 3. Create a HEAD request and test if we got a response with an empty body.
18 const { HttpServer } = ChromeUtils.importESModule(
19 "resource://testing-common/httpd.sys.mjs"
22 const responseContent = "response body";
24 function test_handler(metadata, response) {
25 response.setHeader("Content-Type", "text/html", false);
26 response.setHeader("Cache-control", "max-age=9999", false);
27 response.setStatusLine(metadata.httpVersion, 200, "OK");
28 if (metadata.method != "HEAD") {
29 response.bodyOutputStream.write(responseContent, responseContent.length);
33 function make_channel(url, method) {
34 let channel = NetUtil.newChannel({
36 loadUsingSystemPrincipal: true,
37 }).QueryInterface(Ci.nsIHttpChannel);
38 channel.requestMethod = method;
42 async function get_response(channel, fromCache) {
43 return new Promise(resolve => {
45 new ChannelListener((request, buffer, ctx, isFromCache) => {
46 ok(fromCache == isFromCache, `got response from cache = ${fromCache}`);
53 async function stop_server(httpserver) {
54 return new Promise(resolve => {
55 httpserver.stop(resolve);
59 add_task(async function () {
60 let httpserver = new HttpServer();
61 httpserver.registerPathHandler("/testdir", test_handler);
63 const PORT = httpserver.identity.primaryPort;
64 const URI = `http://localhost:${PORT}/testdir`;
68 response = await get_response(make_channel(URI, "GET"), false);
69 ok(response === responseContent, "got response body");
71 response = await get_response(make_channel(URI, "GET"), true);
72 ok(response === responseContent, "got response body from cache");
74 response = await get_response(make_channel(URI, "HEAD"), false);
75 ok(response === "", "should have empty body");
77 await stop_server(httpserver);