summaryrefslogtreecommitdiffstats
path: root/net/http/http_vary_data_unittest.cc
blob: 852d7d41f248c86eb8b788f7834be3b0f76e76d3 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2006-2008 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 <algorithm>

#include "net/http/http_request_info.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_vary_data.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

typedef testing::Test HttpVaryDataTest;

struct TestTransaction {
  net::HttpRequestInfo request;
  scoped_refptr<net::HttpResponseHeaders> response;

  void Init(const std::string& request_headers,
            const std::string& response_headers) {
    std::string temp(response_headers);
    std::replace(temp.begin(), temp.end(), '\n', '\0');
    response = new net::HttpResponseHeaders(temp);

    request.extra_headers = request_headers;
  }
};

}  // namespace

TEST(HttpVaryDataTest, IsValid) {
  // All of these responses should result in an invalid vary data object.
  const char* kTestResponses[] = {
    "HTTP/1.1 200 OK\n\n",
    "HTTP/1.1 200 OK\nVary: *\n\n",
    "HTTP/1.1 200 OK\nVary: cookie, *, bar\n\n",
    "HTTP/1.1 200 OK\nVary: cookie\nFoo: 1\nVary: *\n\n",
  };

  for (size_t i = 0; i < arraysize(kTestResponses); ++i) {
    TestTransaction t;
    t.Init("", kTestResponses[i]);

    net::HttpVaryData v;
    EXPECT_FALSE(v.Init(t.request, *t.response));
    EXPECT_FALSE(v.is_valid());
  }
}

TEST(HttpVaryDataTest, DoesVary) {
  TestTransaction a;
  a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");

  TestTransaction b;
  b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
}

TEST(HttpVaryDataTest, DoesVary2) {
  TestTransaction a;
  a.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");

  TestTransaction b;
  b.Init("Foo: 12\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
}

TEST(HttpVaryDataTest, DoesntVary) {
  TestTransaction a;
  a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");

  TestTransaction b;
  b.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_TRUE(v.MatchesRequest(b.request, *b.response));
}

TEST(HttpVaryDataTest, DoesntVary2) {
  TestTransaction a;
  a.Init("Foo: 1\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");

  TestTransaction b;
  b.Init("Foo: 1\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_TRUE(v.MatchesRequest(b.request, *b.response));
}

TEST(HttpVaryDataTest, ImplicitCookieForRedirect) {
  TestTransaction a;
  a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");

  TestTransaction b;
  b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
}

TEST(HttpVaryDataTest, ImplicitCookieForRedirect2) {
  // This should be no different than the test above

  TestTransaction a;
  a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n");

  TestTransaction b;
  b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n");

  net::HttpVaryData v;
  EXPECT_TRUE(v.Init(a.request, *a.response));

  EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
}