Home | History | Annotate | Download | only in media
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/renderer/media/cache_util.h"
      6 
      7 #include <string>
      8 
      9 #include "base/format_macros.h"
     10 #include "base/strings/string_number_conversions.h"
     11 #include "base/strings/string_util.h"
     12 #include "base/strings/stringprintf.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "third_party/WebKit/public/platform/WebString.h"
     15 #include "third_party/WebKit/public/platform/WebURLResponse.h"
     16 
     17 using blink::WebString;
     18 using blink::WebURLResponse;
     19 
     20 namespace content {
     21 
     22 // Inputs & expected output for GetReasonsForUncacheability.
     23 struct GRFUTestCase {
     24   WebURLResponse::HTTPVersion version;
     25   int status_code;
     26   const char* headers;
     27   uint32 expected_reasons;
     28 };
     29 
     30 // Create a new WebURLResponse object.
     31 static WebURLResponse CreateResponse(const GRFUTestCase& test) {
     32   WebURLResponse response;
     33   response.initialize();
     34   response.setHTTPVersion(test.version);
     35   response.setHTTPStatusCode(test.status_code);
     36   std::vector<std::string> lines;
     37   Tokenize(test.headers, "\n", &lines);
     38   for (size_t i = 0; i < lines.size(); ++i) {
     39     size_t colon = lines[i].find(": ");
     40     response.addHTTPHeaderField(
     41         WebString::fromUTF8(lines[i].substr(0, colon)),
     42         WebString::fromUTF8(lines[i].substr(colon + 2)));
     43   }
     44   return response;
     45 }
     46 
     47 TEST(CacheUtilTest, GetReasonsForUncacheability) {
     48   enum { kNoReasons = 0 };
     49 
     50   const GRFUTestCase tests[] = {
     51     {
     52       WebURLResponse::HTTP_1_1, 206, "ETag: 'fooblort'", kNoReasons
     53     },
     54     {
     55       WebURLResponse::HTTP_1_1, 206, "", kNoStrongValidatorOnPartialResponse
     56     },
     57     {
     58       WebURLResponse::HTTP_1_0, 206, "",
     59       kPre11PartialResponse | kNoStrongValidatorOnPartialResponse
     60     },
     61     {
     62       WebURLResponse::HTTP_1_1, 200, "cache-control: max-Age=42", kShortMaxAge
     63     },
     64     {
     65       WebURLResponse::HTTP_1_1, 200, "cache-control: max-Age=4200", kNoReasons
     66     },
     67     {
     68       WebURLResponse::HTTP_1_1, 200,
     69       "Date: Tue, 22 May 2012 23:46:08 GMT\n"
     70       "Expires: Tue, 22 May 2012 23:56:08 GMT", kExpiresTooSoon
     71     },
     72     {
     73       WebURLResponse::HTTP_1_1, 200, "cache-control: must-revalidate",
     74       kHasMustRevalidate
     75     },
     76     {
     77       WebURLResponse::HTTP_1_1, 200, "cache-control: no-cache", kNoCache
     78     },
     79     {
     80       WebURLResponse::HTTP_1_1, 200, "cache-control: no-store", kNoStore
     81     },
     82     {
     83       WebURLResponse::HTTP_1_1, 200,
     84       "cache-control: no-cache\ncache-control: no-store", kNoCache | kNoStore
     85     },
     86   };
     87   for (size_t i = 0; i < arraysize(tests); ++i) {
     88     SCOPED_TRACE(base::StringPrintf("case: %" PRIuS
     89                                     ", version: %d, code: %d, headers: %s",
     90                                     i, tests[i].version, tests[i].status_code,
     91                                     tests[i].headers));
     92     EXPECT_EQ(GetReasonsForUncacheability(CreateResponse(tests[i])),
     93               tests[i].expected_reasons);
     94   }
     95 }
     96 
     97 }  // namespace content
     98