Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: net/http/http_response_headers_unittest.cc

Issue 391763002: Initial implementation of Chrome-Freshness header. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove const from directive_size. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_response_headers.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <limits>
6 7
7 #include "base/basictypes.h" 8 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "base/pickle.h" 10 #include "base/pickle.h"
10 #include "base/time/time.h" 11 #include "base/time/time.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "net/http/http_byte_range.h" 13 #include "net/http/http_byte_range.h"
13 #include "net/http/http_response_headers.h" 14 #include "net/http/http_response_headers.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
(...skipping 20 matching lines...) Expand all
36 }; 37 };
37 38
38 // Transform "normal"-looking headers (\n-separated) to the appropriate 39 // Transform "normal"-looking headers (\n-separated) to the appropriate
39 // input format for ParseRawHeaders (\0-separated). 40 // input format for ParseRawHeaders (\0-separated).
40 void HeadersToRaw(std::string* headers) { 41 void HeadersToRaw(std::string* headers) {
41 std::replace(headers->begin(), headers->end(), '\n', '\0'); 42 std::replace(headers->begin(), headers->end(), '\n', '\0');
42 if (!headers->empty()) 43 if (!headers->empty())
43 *headers += '\0'; 44 *headers += '\0';
44 } 45 }
45 46
47 class HttpResponseHeadersCacheControlTest : public HttpResponseHeadersTest {
48 protected:
49 // Make tests less verbose.
50 typedef base::TimeDelta TimeDelta;
51
52 // Initilise the headers() value with a Cache-Control header set to
53 // |cache_control|. |cache_control| is copied and so can safely be a
54 // temporary.
55 void InitializeHeadersWithCacheControl(const char* cache_control) {
56 std::string raw_headers("HTTP/1.1 200 OK\n");
57 raw_headers += "Cache-Control: ";
58 raw_headers += cache_control;
59 raw_headers += "\n";
60 HeadersToRaw(&raw_headers);
61 headers_ = new net::HttpResponseHeaders(raw_headers);
62 }
63
64 const scoped_refptr<net::HttpResponseHeaders>& headers() { return headers_; }
65
66 // Return a pointer to a TimeDelta object. For use when the value doesn't
67 // matter.
68 TimeDelta* TimeDeltaPointer() { return &delta_; }
69
70 // Get the max-age value. This should only be used in tests where a valid
71 // max-age parameter is expected to be present.
72 TimeDelta GetMaxAgeValue() {
73 DCHECK(headers_) << "Call InitializeHeadersWithCacheControl() first";
74 TimeDelta max_age_value;
75 EXPECT_TRUE(headers()->GetMaxAgeValue(&max_age_value));
76 return max_age_value;
77 }
78
79 // Get the stale-while-revalidate value. This should only be used in tests
80 // where a valid max-age parameter is expected to be present.
81 TimeDelta GetStaleWhileRevalidateValue() {
82 DCHECK(headers_) << "Call InitializeHeadersWithCacheControl() first";
83 TimeDelta stale_while_revalidate_value;
84 EXPECT_TRUE(
85 headers()->GetStaleWhileRevalidateValue(&stale_while_revalidate_value));
86 return stale_while_revalidate_value;
87 }
88
89 private:
90 scoped_refptr<net::HttpResponseHeaders> headers_;
91 TimeDelta delta_;
92 };
93
46 void TestCommon(const TestData& test) { 94 void TestCommon(const TestData& test) {
47 std::string raw_headers(test.raw_headers); 95 std::string raw_headers(test.raw_headers);
48 HeadersToRaw(&raw_headers); 96 HeadersToRaw(&raw_headers);
49 std::string expected_headers(test.expected_headers); 97 std::string expected_headers(test.expected_headers);
50 98
51 std::string headers; 99 std::string headers;
52 scoped_refptr<net::HttpResponseHeaders> parsed( 100 scoped_refptr<net::HttpResponseHeaders> parsed(
53 new net::HttpResponseHeaders(raw_headers)); 101 new net::HttpResponseHeaders(raw_headers));
54 parsed->GetNormalizedHeaders(&headers); 102 parsed->GetNormalizedHeaders(&headers);
55 103
(...skipping 1872 matching lines...) Expand 10 before | Expand all | Expand 10 after
1928 EXPECT_EQ(parsed->response_code(), recreated->response_code()); 1976 EXPECT_EQ(parsed->response_code(), recreated->response_code());
1929 EXPECT_EQ(parsed->GetContentLength(), recreated->GetContentLength()); 1977 EXPECT_EQ(parsed->GetContentLength(), recreated->GetContentLength());
1930 EXPECT_EQ(parsed->IsKeepAlive(), recreated->IsKeepAlive()); 1978 EXPECT_EQ(parsed->IsKeepAlive(), recreated->IsKeepAlive());
1931 1979
1932 std::string normalized_parsed; 1980 std::string normalized_parsed;
1933 parsed->GetNormalizedHeaders(&normalized_parsed); 1981 parsed->GetNormalizedHeaders(&normalized_parsed);
1934 std::string normalized_recreated; 1982 std::string normalized_recreated;
1935 parsed->GetNormalizedHeaders(&normalized_recreated); 1983 parsed->GetNormalizedHeaders(&normalized_recreated);
1936 EXPECT_EQ(normalized_parsed, normalized_recreated); 1984 EXPECT_EQ(normalized_parsed, normalized_recreated);
1937 } 1985 }
1986
1987 TEST_F(HttpResponseHeadersCacheControlTest, AbsentMaxAgeReturnsFalse) {
1988 InitializeHeadersWithCacheControl("nocache");
1989 EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
1990 }
1991
1992 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithNoParameterRejected) {
1993 InitializeHeadersWithCacheControl("max-age=,private");
1994 EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
1995 }
1996
1997 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeWithSpaceParameterRejected) {
1998 InitializeHeadersWithCacheControl("max-age= ,private");
1999 EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
2000 }
2001
2002 TEST_F(HttpResponseHeadersCacheControlTest,
2003 MaxAgeWithSpaceBeforeEqualsIsRejected) {
2004 InitializeHeadersWithCacheControl("max-age = 7");
2005 EXPECT_FALSE(headers()->GetMaxAgeValue(TimeDeltaPointer()));
2006 }
2007
2008 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeFirstMatchUsed) {
2009 InitializeHeadersWithCacheControl("max-age=10, max-age=20");
2010 EXPECT_EQ(TimeDelta::FromSeconds(10), GetMaxAgeValue());
2011 }
2012
2013 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeBogusFirstMatchUsed) {
2014 // max-age10 isn't parsed as max-age; max-age=now is parsed as max-age=0 and
2015 // so max-age=20 is not used.
2016 InitializeHeadersWithCacheControl("max-age10, max-age=now, max-age=20");
2017 EXPECT_EQ(TimeDelta::FromSeconds(0), GetMaxAgeValue());
2018 }
2019
2020 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeCaseInsensitive) {
2021 InitializeHeadersWithCacheControl("Max-aGe=15");
2022 EXPECT_EQ(TimeDelta::FromSeconds(15), GetMaxAgeValue());
2023 }
2024
2025 TEST_F(HttpResponseHeadersCacheControlTest, MaxAgeEdgeCases) {
2026 // This test doesn't use TEST_P() for consistency with the rest of the tests
2027 // in this file.
2028 // TODO(ricea): Port the tests in this file to use TEST_P().
2029 const struct {
2030 const char* max_age_string;
2031 int64 expected_seconds;
2032 } tests[] = {
2033 {" 1 ", 1}, // Spaces are ignored
2034 {"-1", -1}, // Negative numbers are passed through
2035 {"--1", 0}, // Leading junk gives 0
2036 {"2s", 2}, // trailing junk is ignored
2037 {"3 days", 3},
2038 {"'4'", 0}, // single quotes don't work
2039 {"\"5\"", 0}, // double quotes don't work
2040 {"0x6", 0}, // hex not parsed as hex
2041 {"7F", 7}, // hex without 0x still not parsed as hex
2042 {"010", 10}, // octal not parsed as octal
2043 {"9223372036854", 9223372036854},
2044 // {"9223372036855", -9223372036854}, // undefined behaviour
2045 // {"9223372036854775806", -2}, // undefined behaviour
2046 {"9223372036854775807", 9223372036854775807},
2047 {"20000000000000000000",
2048 std::numeric_limits<int64>::max()}, // overflow int64
2049 };
2050 std::string max_age = "max-age=";
2051 for (size_t i = 0; i < arraysize(tests); ++i) {
2052 InitializeHeadersWithCacheControl(
2053 (max_age + tests[i].max_age_string).c_str());
2054 EXPECT_EQ(tests[i].expected_seconds, GetMaxAgeValue().InSeconds())
2055 << " for max-age=" << tests[i].max_age_string;
2056 }
2057 }
2058
2059 TEST_F(HttpResponseHeadersCacheControlTest,
2060 AbsentStaleWhileRevalidateReturnsFalse) {
2061 InitializeHeadersWithCacheControl("max-age=3600");
2062 EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue(TimeDeltaPointer()));
2063 }
2064
2065 TEST_F(HttpResponseHeadersCacheControlTest,
2066 StaleWhileRevalidateWithoutValueRejected) {
2067 InitializeHeadersWithCacheControl("max-age=3600,stale-while-revalidate=");
2068 EXPECT_FALSE(headers()->GetStaleWhileRevalidateValue(TimeDeltaPointer()));
2069 }
2070
2071 TEST_F(HttpResponseHeadersCacheControlTest,
2072 StaleWhileRevalidateWithInvalidValueTreatedAsZero) {
2073 InitializeHeadersWithCacheControl("max-age=3600,stale-while-revalidate=true");
2074 EXPECT_EQ(TimeDelta(), GetStaleWhileRevalidateValue());
2075 }
2076
2077 TEST_F(HttpResponseHeadersCacheControlTest, StaleWhileRevalidateValueReturned) {
2078 InitializeHeadersWithCacheControl("max-age=3600,stale-while-revalidate=7200");
2079 EXPECT_EQ(TimeDelta::FromSeconds(7200), GetStaleWhileRevalidateValue());
2080 }
2081
2082 TEST_F(HttpResponseHeadersCacheControlTest,
2083 FirstStaleWhileRevalidateValueUsed) {
2084 InitializeHeadersWithCacheControl(
2085 "stale-while-revalidate=1,stale-while-revalidate=7200");
2086 EXPECT_EQ(TimeDelta::FromSeconds(1), GetStaleWhileRevalidateValue());
2087 }
OLDNEW
« no previous file with comments | « net/http/http_response_headers.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698