From 896f36d30c6fe8bea89f470b5d85967b682ae28d Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Thu, 18 Jul 2019 12:19:10 +0000 Subject: [PATCH] Bug 1555927 - Cache 204 response r=mayhemer Differential Revision: https://phabricator.services.mozilla.com/D35710 --HG-- extra : moz-landing-system : lando --- netwerk/protocol/http/nsHttpResponseHead.cpp | 1 + netwerk/test/unit/test_cache_204_response.js | 62 +++++++++++++++++++ netwerk/test/unit/xpcshell.ini | 2 +- .../meta/fetch/http-cache/heuristic.html.ini | 6 -- .../meta/fetch/http-cache/status.html.ini | 6 -- 5 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 netwerk/test/unit/test_cache_204_response.js diff --git a/netwerk/protocol/http/nsHttpResponseHead.cpp b/netwerk/protocol/http/nsHttpResponseHead.cpp index acc1576aa43d..d74e43ccbfbb 100644 --- a/netwerk/protocol/http/nsHttpResponseHead.cpp +++ b/netwerk/protocol/http/nsHttpResponseHead.cpp @@ -711,6 +711,7 @@ bool nsHttpResponseHead::MustValidate() { // Success codes case 200: case 203: + case 204: case 206: // Cacheable redirects case 300: diff --git a/netwerk/test/unit/test_cache_204_response.js b/netwerk/test/unit/test_cache_204_response.js new file mode 100644 index 000000000000..c5855210762b --- /dev/null +++ b/netwerk/test/unit/test_cache_204_response.js @@ -0,0 +1,62 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* +Test if 204 response is cached. +1. Make first http request and return a 204 response. +2. Check if the first response is not cached. +3. Make second http request and check if the response is cached. + +*/ + +"use strict"; + +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); + +function test_handler(metadata, response) { + response.setHeader("Content-Type", "text/html", false); + response.setHeader("Cache-control", "max-age=9999", false); + response.setStatusLine(metadata.httpVersion, 204, "No Content"); +} + +function make_channel(url) { + let channel = NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true, + }).QueryInterface(Ci.nsIHttpChannel); + return channel; +} + +async function get_response(channel, fromCache) { + return new Promise(resolve => { + channel.asyncOpen( + new ChannelListener((request, buffer, ctx, isFromCache) => { + ok(fromCache == isFromCache, `got response from cache = ${fromCache}`); + resolve(); + }) + ); + }); +} + +async function stop_server(httpserver) { + return new Promise(resolve => { + httpserver.stop(resolve); + }); +} + +add_task(async function() { + let httpserver = new HttpServer(); + httpserver.registerPathHandler("/testdir", test_handler); + httpserver.start(-1); + const PORT = httpserver.identity.primaryPort; + const URI = `http://localhost:${PORT}/testdir`; + + let response; + + await get_response(make_channel(URI, "GET"), false); + await get_response(make_channel(URI, "GET"), true); + + await stop_server(httpserver); +}); diff --git a/netwerk/test/unit/xpcshell.ini b/netwerk/test/unit/xpcshell.ini index b488ba32db91..ddf00d130e30 100644 --- a/netwerk/test/unit/xpcshell.ini +++ b/netwerk/test/unit/xpcshell.ini @@ -457,4 +457,4 @@ run-sequentially = one http2 node proxy is used for all tests, this test is usin skip-if = true [test_head_request_no_response_body.js] [test_disabled_ftp.js] - +[test_cache_204_response.js] diff --git a/testing/web-platform/meta/fetch/http-cache/heuristic.html.ini b/testing/web-platform/meta/fetch/http-cache/heuristic.html.ini index 17b2b48eba25..6984591b0ee9 100644 --- a/testing/web-platform/meta/fetch/http-cache/heuristic.html.ini +++ b/testing/web-platform/meta/fetch/http-cache/heuristic.html.ini @@ -2,9 +2,6 @@ [HTTP cache reuses an unknown response with Last-Modified based upon heuristic freshness when Cache-Control: public is present.] expected: FAIL - [HTTP cache reuses a 204 No Content response with Last-Modified based upon heuristic freshness.] - expected: FAIL - [HTTP cache reuses a 404 Not Found response with Last-Modified based upon heuristic freshness.] expected: FAIL @@ -20,9 +17,6 @@ [HTTP cache reuses an unknown response with Last-Modified based upon heuristic freshness when Cache-Control: public is present] expected: FAIL - [HTTP cache reuses a 204 No Content response with Last-Modified based upon heuristic freshness] - expected: FAIL - [HTTP cache reuses a 404 Not Found response with Last-Modified based upon heuristic freshness] expected: FAIL diff --git a/testing/web-platform/meta/fetch/http-cache/status.html.ini b/testing/web-platform/meta/fetch/http-cache/status.html.ini index 50255f73056e..44ba6a37c58c 100644 --- a/testing/web-platform/meta/fetch/http-cache/status.html.ini +++ b/testing/web-platform/meta/fetch/http-cache/status.html.ini @@ -1,7 +1,4 @@ [status.html] - [HTTP cache avoids going to the network if it has a fresh 204 response.] - expected: FAIL - [HTTP cache avoids going to the network if it has a fresh 299 response.] expected: FAIL @@ -29,9 +26,6 @@ [HTTP cache avoids going to the network if it has a fresh 599 response.] expected: FAIL - [HTTP cache avoids going to the network if it has a fresh 204 response] - expected: FAIL - [HTTP cache avoids going to the network if it has a fresh 299 response] expected: FAIL