summaryrefslogtreecommitdiff
path: root/chromium/media/blink/cache_util_unittest.cc
blob: 2e0a6820cba934eee05089e11284108859684eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/blink/cache_util.h"

#include <stddef.h>
#include <stdint.h>

#include <string>

#include "base/format_macros.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/platform/web_url_response.h"

using blink::WebString;
using blink::WebURLResponse;

namespace media {

// Inputs & expected output for GetReasonsForUncacheability.
struct GRFUTestCase {
  WebURLResponse::HTTPVersion version;
  int status_code;
  const char* headers;
  uint32_t expected_reasons;
};

// Create a new WebURLResponse object.
static WebURLResponse CreateResponse(const GRFUTestCase& test) {
  WebURLResponse response;
  response.SetHttpVersion(test.version);
  response.SetHttpStatusCode(test.status_code);
  for (const std::string& line :
       base::SplitString(test.headers, "\n", base::KEEP_WHITESPACE,
                         base::SPLIT_WANT_NONEMPTY)) {
    size_t colon = line.find(": ");
    response.AddHttpHeaderField(WebString::FromUTF8(line.substr(0, colon)),
                                WebString::FromUTF8(line.substr(colon + 2)));
  }
  return response;
}

TEST(CacheUtilTest, GetReasonsForUncacheability) {
  enum { kNoReasons = 0 };

  const GRFUTestCase tests[] = {
      {WebURLResponse::kHTTPVersion_1_1, 206, "ETag: 'fooblort'", kNoReasons},
      {WebURLResponse::kHTTPVersion_1_1, 206, "",
       kNoStrongValidatorOnPartialResponse},
      {WebURLResponse::kHTTPVersion_1_0, 206, "",
       kPre11PartialResponse | kNoStrongValidatorOnPartialResponse},
      {WebURLResponse::kHTTPVersion_1_1, 200, "cache-control: max-Age=42",
       kShortMaxAge},
      {WebURLResponse::kHTTPVersion_1_1, 200, "cache-control: max-Age=4200",
       kNoReasons},
      {WebURLResponse::kHTTPVersion_1_1, 200,
       "Date: Tue, 22 May 2012 23:46:08 GMT\n"
       "Expires: Tue, 22 May 2012 23:56:08 GMT",
       kExpiresTooSoon},
      {WebURLResponse::kHTTPVersion_1_1, 200, "cache-control: must-revalidate",
       kHasMustRevalidate},
      {WebURLResponse::kHTTPVersion_1_1, 200, "cache-control: no-cache",
       kNoCache},
      {WebURLResponse::kHTTPVersion_1_1, 200, "cache-control: no-store",
       kNoStore},
      {WebURLResponse::kHTTPVersion_1_1, 200,
       "cache-control: no-cache\ncache-control: no-store", kNoCache | kNoStore},
  };
  for (size_t i = 0; i < base::size(tests); ++i) {
    SCOPED_TRACE(base::StringPrintf("case: %" PRIuS
                                    ", version: %d, code: %d, headers: %s",
                                    i, tests[i].version, tests[i].status_code,
                                    tests[i].headers));
    EXPECT_EQ(GetReasonsForUncacheability(CreateResponse(tests[i])),
              tests[i].expected_reasons);
  }
}

}  // namespace media