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.
5 #include "content/renderer/media/cache_util.h"
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"
17 using blink::WebString
;
18 using blink::WebURLResponse
;
22 // Inputs & expected output for GetReasonsForUncacheability.
24 WebURLResponse::HTTPVersion version
;
27 uint32 expected_reasons
;
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)));
47 TEST(CacheUtilTest
, GetReasonsForUncacheability
) {
48 enum { kNoReasons
= 0 };
50 const GRFUTestCase tests
[] = {
52 WebURLResponse::HTTP_1_1
, 206, "ETag: 'fooblort'", kNoReasons
55 WebURLResponse::HTTP_1_1
, 206, "", kNoStrongValidatorOnPartialResponse
58 WebURLResponse::HTTP_1_0
, 206, "",
59 kPre11PartialResponse
| kNoStrongValidatorOnPartialResponse
62 WebURLResponse::HTTP_1_1
, 200, "cache-control: max-Age=42", kShortMaxAge
65 WebURLResponse::HTTP_1_1
, 200, "cache-control: max-Age=4200", kNoReasons
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
73 WebURLResponse::HTTP_1_1
, 200, "cache-control: must-revalidate",
77 WebURLResponse::HTTP_1_1
, 200, "cache-control: no-cache", kNoCache
80 WebURLResponse::HTTP_1_1
, 200, "cache-control: no-store", kNoStore
83 WebURLResponse::HTTP_1_1
, 200,
84 "cache-control: no-cache\ncache-control: no-store", kNoCache
| kNoStore
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
,
92 EXPECT_EQ(GetReasonsForUncacheability(CreateResponse(tests
[i
])),
93 tests
[i
].expected_reasons
);
97 } // namespace content