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 "media/blink/cache_util.h"
9 #include "base/format_macros.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/WebURLResponse.h"
18 using blink::WebString
;
19 using blink::WebURLResponse
;
23 // Inputs & expected output for GetReasonsForUncacheability.
25 WebURLResponse::HTTPVersion version
;
28 uint32 expected_reasons
;
31 // Create a new WebURLResponse object.
32 static WebURLResponse
CreateResponse(const GRFUTestCase
& test
) {
33 WebURLResponse response
;
34 response
.initialize();
35 response
.setHTTPVersion(test
.version
);
36 response
.setHTTPStatusCode(test
.status_code
);
37 for (const std::string
& line
:
38 base::SplitString(test
.headers
, "\n", base::KEEP_WHITESPACE
,
39 base::SPLIT_WANT_NONEMPTY
)) {
40 size_t colon
= line
.find(": ");
41 response
.addHTTPHeaderField(
42 WebString::fromUTF8(line
.substr(0, colon
)),
43 WebString::fromUTF8(line
.substr(colon
+ 2)));
48 TEST(CacheUtilTest
, GetReasonsForUncacheability
) {
49 enum { kNoReasons
= 0 };
51 const GRFUTestCase tests
[] = {
53 WebURLResponse::HTTP_1_1
, 206, "ETag: 'fooblort'", kNoReasons
56 WebURLResponse::HTTP_1_1
, 206, "", kNoStrongValidatorOnPartialResponse
59 WebURLResponse::HTTP_1_0
, 206, "",
60 kPre11PartialResponse
| kNoStrongValidatorOnPartialResponse
63 WebURLResponse::HTTP_1_1
, 200, "cache-control: max-Age=42", kShortMaxAge
66 WebURLResponse::HTTP_1_1
, 200, "cache-control: max-Age=4200", kNoReasons
69 WebURLResponse::HTTP_1_1
, 200,
70 "Date: Tue, 22 May 2012 23:46:08 GMT\n"
71 "Expires: Tue, 22 May 2012 23:56:08 GMT", kExpiresTooSoon
74 WebURLResponse::HTTP_1_1
, 200, "cache-control: must-revalidate",
78 WebURLResponse::HTTP_1_1
, 200, "cache-control: no-cache", kNoCache
81 WebURLResponse::HTTP_1_1
, 200, "cache-control: no-store", kNoStore
84 WebURLResponse::HTTP_1_1
, 200,
85 "cache-control: no-cache\ncache-control: no-store", kNoCache
| kNoStore
88 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
89 SCOPED_TRACE(base::StringPrintf("case: %" PRIuS
90 ", version: %d, code: %d, headers: %s",
91 i
, tests
[i
].version
, tests
[i
].status_code
,
93 EXPECT_EQ(GetReasonsForUncacheability(CreateResponse(tests
[i
])),
94 tests
[i
].expected_reasons
);