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 "ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // Test that we are able to discover the cache validator headers.
12 TEST(NaClHttpResponseHeadersTest
, TestGetValidators
) {
13 // Test a single (weak) ETag.
14 std::string
one_val_headers("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
15 "Server: Apache/2.0.52 (CentOS)\n"
16 "Content-Type: text/plain; charset=UTF-8\n"
18 "Accept-Ranges: bytes\n"
19 "ETag: w\"abcdefg\"\n"
20 "Content-Length: 2912652\n");
21 std::string
one_val_expected("etag:w\"abcdefg\"");
22 plugin::NaClHttpResponseHeaders parser_1
;
23 parser_1
.Parse(one_val_headers
);
24 EXPECT_EQ(one_val_expected
, parser_1
.GetCacheValidators());
25 EXPECT_EQ(std::string("w\"abcdefg\""), parser_1
.GetHeader("etag"));
26 EXPECT_EQ(std::string(), parser_1
.GetHeader("last-modified"));
28 // Test a Last-Modified Header.
29 std::string
mod_val_headers("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
30 "Server: Apache/2.0.52 (CentOS)\n"
31 "Content-Type: text/plain; charset=UTF-8\n"
33 "Accept-Ranges: bytes\n"
34 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
35 "Content-Length: 2912652\n");
36 std::string
mod_val_expected("last-modified:Wed, 15 Nov 1995 04:58:08 GMT");
37 plugin::NaClHttpResponseHeaders parser_1b
;
38 parser_1b
.Parse(mod_val_headers
);
39 EXPECT_EQ(mod_val_expected
, parser_1b
.GetCacheValidators());
40 EXPECT_EQ(std::string("Wed, 15 Nov 1995 04:58:08 GMT"),
41 parser_1b
.GetHeader("last-modified"));
43 // Test both (strong) ETag and Last-Modified, with some whitespace.
44 std::string
two_val_headers("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
45 "Last-modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
46 "Server: Apache/2.0.52 (CentOS)\n"
47 "etag \t :\t \"/abcdefg:A-Z0-9+/==\"\n"
48 "Content-Type: text/plain; charset=UTF-8\n"
49 "cache-control: no-cache\n"
51 "Accept-Ranges: bytes\n"
52 "Content-Length: 2912652\n");
53 // Note that the value can still have white-space.
54 std::string
two_val_expected("etag:\"/abcdefg:A-Z0-9+/==\"&"
55 "last-modified:Wed, 15 Nov 1995 04:58:08 GMT");
56 plugin::NaClHttpResponseHeaders parser_2
;
57 parser_2
.Parse(two_val_headers
);
58 EXPECT_EQ(two_val_expected
, parser_2
.GetCacheValidators());
59 EXPECT_EQ(std::string("\"/abcdefg:A-Z0-9+/==\""),
60 parser_2
.GetHeader("etag"));
62 // Some etag generators like python HTTP server use ' instead of "
63 std::string
single_q_headers("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
64 "Server: BaseHTTP/0.3 Python/2.7.3\n"
65 "ETag: '/usr/local/some_file.nmf'\n");
66 std::string
single_q_expected("etag:'/usr/local/some_file.nmf'");
67 plugin::NaClHttpResponseHeaders parser_3
;
68 parser_3
.Parse(single_q_headers
);
69 EXPECT_EQ(single_q_expected
, parser_3
.GetCacheValidators());
70 EXPECT_EQ(std::string("'/usr/local/some_file.nmf'"),
71 parser_3
.GetHeader("etag"));
73 // Keys w/ leading whitespace are invalid.
74 // See: HttpResponseHeadersTest.NormalizeHeadersLeadingWhitespace.
75 std::string
bad_headers("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
76 "Server: BaseHTTP/0.3 Python/2.7.3\n"
77 " ETag: '/usr/local/some_file.nmf'\n");
78 std::string
bad_expected("");
79 plugin::NaClHttpResponseHeaders parser_4
;
80 parser_4
.Parse(bad_headers
);
81 EXPECT_EQ(bad_expected
, parser_4
.GetCacheValidators());
82 EXPECT_EQ(bad_expected
, parser_4
.GetHeader("etag"));
85 // Test that we are able to determine when there is a no-store
86 // Cache-Control header, among all the Cache-Control headers.
87 TEST(NaClHttpResponseHeadersTest
, TestFindNoStore
) {
88 // Say that there isn't one, when there isn't one.
89 std::string
headers_0("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
90 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
91 "ETag: '/tmp/blah.nmf'\n"
92 "Cache-Control: max-age=3600\n");
93 plugin::NaClHttpResponseHeaders parser_0
;
94 parser_0
.Parse(headers_0
);
95 EXPECT_FALSE(parser_0
.CacheControlNoStore());
97 // Say that there is one, when there is one.
98 std::string
headers_1("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
99 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
100 "ETag: \"/abcdefgA-Z0-9+/\"\n"
101 "Cache-Control: no-store\n");
102 plugin::NaClHttpResponseHeaders parser_1
;
103 parser_1
.Parse(headers_1
);
104 EXPECT_TRUE(parser_1
.CacheControlNoStore());
106 // Say that there is one, when comma separated.
107 std::string
headers_2("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
108 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
109 "ETag: \"/abcdefgA-Z0-9+/\"\n"
110 "Cache-Control: no-store, no-cache\n");
111 plugin::NaClHttpResponseHeaders parser_2
;
112 parser_2
.Parse(headers_2
);
113 EXPECT_TRUE(parser_2
.CacheControlNoStore());
115 // Comma separated, in a different position.
116 std::string
headers_3("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
117 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
118 "ETag: \"/abcdefgA-Z0-9+/\"\n"
119 "Cache-control: no-cache, max-age=60, no-store\n");
120 plugin::NaClHttpResponseHeaders parser_3
;
121 parser_3
.Parse(headers_3
);
122 EXPECT_TRUE(parser_3
.CacheControlNoStore());
124 // Test multiple cache-control lines, plus extra space before colon.
125 std::string
headers_4("Date: Wed, 15 Nov 1995 06:25:24 GMT\n"
126 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT\n"
127 "ETag: \"/abcdefgA-Z0-9+/\"\n"
128 "cache-control: no-cache\n"
129 "cache-control \t : max-age=60, no-store, max-stale\n");
130 plugin::NaClHttpResponseHeaders parser_4
;
131 parser_4
.Parse(headers_4
);
132 EXPECT_TRUE(parser_4
.CacheControlNoStore());
134 // Test with extra whitespace, in the values.
135 std::string
headers_5("Date: Wed, 15 Nov 1995 06:25:24 GMT \n"
136 "Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT \n"
137 "ETag: \"/abcdefgA-Z0-9+/\" \n"
141 "Connection: close\n"
142 "cache-control:max-age=60, no-store \n"
143 "cache-control: no-cache\n");
144 plugin::NaClHttpResponseHeaders parser_5
;
145 parser_5
.Parse(headers_5
);
146 EXPECT_TRUE(parser_5
.CacheControlNoStore());