| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 HeadersToRaw(&headers); | 282 HeadersToRaw(&headers); |
| 283 scoped_refptr<HttpResponseHeaders> parsed = new HttpResponseHeaders(headers); | 283 scoped_refptr<HttpResponseHeaders> parsed = new HttpResponseHeaders(headers); |
| 284 | 284 |
| 285 std::string value; | 285 std::string value; |
| 286 EXPECT_TRUE(parsed->GetNormalizedHeader("cache-control", &value)); | 286 EXPECT_TRUE(parsed->GetNormalizedHeader("cache-control", &value)); |
| 287 EXPECT_EQ("private, no-store", value); | 287 EXPECT_EQ("private, no-store", value); |
| 288 } | 288 } |
| 289 | 289 |
| 290 TEST(HttpResponseHeadersTest, Persist) { | 290 TEST(HttpResponseHeadersTest, Persist) { |
| 291 const struct { | 291 const struct { |
| 292 net::HttpResponseHeaders::PersistOptions options; |
| 292 const char* raw_headers; | 293 const char* raw_headers; |
| 293 const char* expected_headers; | 294 const char* expected_headers; |
| 294 } tests[] = { | 295 } tests[] = { |
| 295 { "HTTP/1.1 200 OK\n" | 296 { net::HttpResponseHeaders::PERSIST_ALL, |
| 297 "HTTP/1.1 200 OK\n" |
| 296 "Cache-control:private\n" | 298 "Cache-control:private\n" |
| 297 "cache-Control:no-store\n", | 299 "cache-Control:no-store\n", |
| 298 | 300 |
| 299 "HTTP/1.1 200 OK\n" | 301 "HTTP/1.1 200 OK\n" |
| 300 "Cache-control: private, no-store\n" | 302 "Cache-control: private, no-store\n" |
| 301 }, | 303 }, |
| 302 { "HTTP/1.1 200 OK\n" | 304 { net::HttpResponseHeaders::PERSIST_SANS_HOP_BY_HOP, |
| 305 "HTTP/1.1 200 OK\n" |
| 303 "connection: keep-alive\n" | 306 "connection: keep-alive\n" |
| 304 "server: blah\n", | 307 "server: blah\n", |
| 305 | 308 |
| 306 "HTTP/1.1 200 OK\n" | 309 "HTTP/1.1 200 OK\n" |
| 307 "server: blah\n" | 310 "server: blah\n" |
| 308 }, | 311 }, |
| 309 { "HTTP/1.1 200 OK\n" | 312 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE | |
| 313 net::HttpResponseHeaders::PERSIST_SANS_HOP_BY_HOP, |
| 314 "HTTP/1.1 200 OK\n" |
| 310 "fOo: 1\n" | 315 "fOo: 1\n" |
| 311 "Foo: 2\n" | 316 "Foo: 2\n" |
| 312 "Transfer-Encoding: chunked\n" | 317 "Transfer-Encoding: chunked\n" |
| 313 "CoNnection: keep-alive\n" | 318 "CoNnection: keep-alive\n" |
| 314 "cache-control: private, no-cache=\"foo\"\n", | 319 "cache-control: private, no-cache=\"foo\"\n", |
| 315 | 320 |
| 316 "HTTP/1.1 200 OK\n" | 321 "HTTP/1.1 200 OK\n" |
| 317 "cache-control: private, no-cache=\"foo\"\n" | 322 "cache-control: private, no-cache=\"foo\"\n" |
| 318 }, | 323 }, |
| 319 { "HTTP/1.1 200 OK\n" | 324 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 325 "HTTP/1.1 200 OK\n" |
| 320 "Foo: 2\n" | 326 "Foo: 2\n" |
| 321 "Cache-Control: private,no-cache=\"foo, bar\"\n" | 327 "Cache-Control: private,no-cache=\"foo, bar\"\n" |
| 322 "bar", | 328 "bar", |
| 323 | 329 |
| 324 "HTTP/1.1 200 OK\n" | 330 "HTTP/1.1 200 OK\n" |
| 325 "Cache-Control: private,no-cache=\"foo, bar\"\n" | 331 "Cache-Control: private,no-cache=\"foo, bar\"\n" |
| 326 }, | 332 }, |
| 327 // ignore bogus no-cache value | 333 // ignore bogus no-cache value |
| 328 { "HTTP/1.1 200 OK\n" | 334 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 335 "HTTP/1.1 200 OK\n" |
| 329 "Foo: 2\n" | 336 "Foo: 2\n" |
| 330 "Cache-Control: private,no-cache=foo\n", | 337 "Cache-Control: private,no-cache=foo\n", |
| 331 | 338 |
| 332 "HTTP/1.1 200 OK\n" | 339 "HTTP/1.1 200 OK\n" |
| 333 "Foo: 2\n" | 340 "Foo: 2\n" |
| 334 "Cache-Control: private,no-cache=foo\n" | 341 "Cache-Control: private,no-cache=foo\n" |
| 335 }, | 342 }, |
| 336 // ignore bogus no-cache value | 343 // ignore bogus no-cache value |
| 337 { "HTTP/1.1 200 OK\n" | 344 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 345 "HTTP/1.1 200 OK\n" |
| 338 "Foo: 2\n" | 346 "Foo: 2\n" |
| 339 "Cache-Control: private, no-cache=\n", | 347 "Cache-Control: private, no-cache=\n", |
| 340 | 348 |
| 341 "HTTP/1.1 200 OK\n" | 349 "HTTP/1.1 200 OK\n" |
| 342 "Foo: 2\n" | 350 "Foo: 2\n" |
| 343 "Cache-Control: private, no-cache=\n" | 351 "Cache-Control: private, no-cache=\n" |
| 344 }, | 352 }, |
| 345 // ignore empty no-cache value | 353 // ignore empty no-cache value |
| 346 { "HTTP/1.1 200 OK\n" | 354 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 355 "HTTP/1.1 200 OK\n" |
| 347 "Foo: 2\n" | 356 "Foo: 2\n" |
| 348 "Cache-Control: private, no-cache=\"\"\n", | 357 "Cache-Control: private, no-cache=\"\"\n", |
| 349 | 358 |
| 350 "HTTP/1.1 200 OK\n" | 359 "HTTP/1.1 200 OK\n" |
| 351 "Foo: 2\n" | 360 "Foo: 2\n" |
| 352 "Cache-Control: private, no-cache=\"\"\n" | 361 "Cache-Control: private, no-cache=\"\"\n" |
| 353 }, | 362 }, |
| 354 // ignore wrong quotes no-cache value | 363 // ignore wrong quotes no-cache value |
| 355 { "HTTP/1.1 200 OK\n" | 364 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 365 "HTTP/1.1 200 OK\n" |
| 356 "Foo: 2\n" | 366 "Foo: 2\n" |
| 357 "Cache-Control: private, no-cache=\'foo\'\n", | 367 "Cache-Control: private, no-cache=\'foo\'\n", |
| 358 | 368 |
| 359 "HTTP/1.1 200 OK\n" | 369 "HTTP/1.1 200 OK\n" |
| 360 "Foo: 2\n" | 370 "Foo: 2\n" |
| 361 "Cache-Control: private, no-cache=\'foo\'\n" | 371 "Cache-Control: private, no-cache=\'foo\'\n" |
| 362 }, | 372 }, |
| 363 // ignore unterminated quotes no-cache value | 373 // ignore unterminated quotes no-cache value |
| 364 { "HTTP/1.1 200 OK\n" | 374 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 375 "HTTP/1.1 200 OK\n" |
| 365 "Foo: 2\n" | 376 "Foo: 2\n" |
| 366 "Cache-Control: private, no-cache=\"foo\n", | 377 "Cache-Control: private, no-cache=\"foo\n", |
| 367 | 378 |
| 368 "HTTP/1.1 200 OK\n" | 379 "HTTP/1.1 200 OK\n" |
| 369 "Foo: 2\n" | 380 "Foo: 2\n" |
| 370 "Cache-Control: private, no-cache=\"foo\n" | 381 "Cache-Control: private, no-cache=\"foo\n" |
| 371 }, | 382 }, |
| 372 // accept sloppy LWS | 383 // accept sloppy LWS |
| 373 { "HTTP/1.1 200 OK\n" | 384 { net::HttpResponseHeaders::PERSIST_SANS_NON_CACHEABLE, |
| 385 "HTTP/1.1 200 OK\n" |
| 374 "Foo: 2\n" | 386 "Foo: 2\n" |
| 375 "Cache-Control: private, no-cache=\" foo\t, bar\"\n", | 387 "Cache-Control: private, no-cache=\" foo\t, bar\"\n", |
| 376 | 388 |
| 377 "HTTP/1.1 200 OK\n" | 389 "HTTP/1.1 200 OK\n" |
| 378 "Cache-Control: private, no-cache=\" foo\t, bar\"\n" | 390 "Cache-Control: private, no-cache=\" foo\t, bar\"\n" |
| 379 }, | 391 }, |
| 380 // header name appears twice, separated by another header | 392 // header name appears twice, separated by another header |
| 381 { "HTTP/1.1 200 OK\n" | 393 { net::HttpResponseHeaders::PERSIST_ALL, |
| 394 "HTTP/1.1 200 OK\n" |
| 382 "Foo: 1\n" | 395 "Foo: 1\n" |
| 383 "Bar: 2\n" | 396 "Bar: 2\n" |
| 384 "Foo: 3\n", | 397 "Foo: 3\n", |
| 385 | 398 |
| 386 "HTTP/1.1 200 OK\n" | 399 "HTTP/1.1 200 OK\n" |
| 387 "Foo: 1, 3\n" | 400 "Foo: 1, 3\n" |
| 388 "Bar: 2\n" | 401 "Bar: 2\n" |
| 389 }, | 402 }, |
| 390 // header name appears twice, separated by another header (type 2) | 403 // header name appears twice, separated by another header (type 2) |
| 391 { "HTTP/1.1 200 OK\n" | 404 { net::HttpResponseHeaders::PERSIST_ALL, |
| 405 "HTTP/1.1 200 OK\n" |
| 392 "Foo: 1, 3\n" | 406 "Foo: 1, 3\n" |
| 393 "Bar: 2\n" | 407 "Bar: 2\n" |
| 394 "Foo: 4\n", | 408 "Foo: 4\n", |
| 395 | 409 |
| 396 "HTTP/1.1 200 OK\n" | 410 "HTTP/1.1 200 OK\n" |
| 397 "Foo: 1, 3, 4\n" | 411 "Foo: 1, 3, 4\n" |
| 398 "Bar: 2\n" | 412 "Bar: 2\n" |
| 399 }, | 413 }, |
| 414 // Test filtering of cookie headers. |
| 415 { net::HttpResponseHeaders::PERSIST_SANS_COOKIES, |
| 416 "HTTP/1.1 200 OK\n" |
| 417 "Set-Cookie: foo=bar; httponly\n" |
| 418 "Set-Cookie: bar=foo\n" |
| 419 "Bar: 1\n" |
| 420 "Set-Cookie2: bar2=foo2\n", |
| 421 |
| 422 "HTTP/1.1 200 OK\n" |
| 423 "Bar: 1\n" |
| 424 }, |
| 400 }; | 425 }; |
| 401 | 426 |
| 402 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 427 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 403 std::string headers = tests[i].raw_headers; | 428 std::string headers = tests[i].raw_headers; |
| 404 HeadersToRaw(&headers); | 429 HeadersToRaw(&headers); |
| 405 scoped_refptr<HttpResponseHeaders> parsed1 = | 430 scoped_refptr<HttpResponseHeaders> parsed1 = |
| 406 new HttpResponseHeaders(headers); | 431 new HttpResponseHeaders(headers); |
| 407 | 432 |
| 408 Pickle pickle; | 433 Pickle pickle; |
| 409 parsed1->Persist(&pickle, true); | 434 parsed1->Persist(&pickle, tests[i].options); |
| 410 | 435 |
| 411 void* iter = NULL; | 436 void* iter = NULL; |
| 412 scoped_refptr<HttpResponseHeaders> parsed2 = | 437 scoped_refptr<HttpResponseHeaders> parsed2 = |
| 413 new HttpResponseHeaders(pickle, &iter); | 438 new HttpResponseHeaders(pickle, &iter); |
| 414 | 439 |
| 415 std::string h2; | 440 std::string h2; |
| 416 parsed2->GetNormalizedHeaders(&h2); | 441 parsed2->GetNormalizedHeaders(&h2); |
| 417 EXPECT_EQ(string(tests[i].expected_headers), h2); | 442 EXPECT_EQ(string(tests[i].expected_headers), h2); |
| 418 } | 443 } |
| 419 } | 444 } |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1084 } | 1109 } |
| 1085 | 1110 |
| 1086 TEST(HttpResponseHeadersTest, GetStatusBadStatusLine) { | 1111 TEST(HttpResponseHeadersTest, GetStatusBadStatusLine) { |
| 1087 std::string headers("Foo bar."); | 1112 std::string headers("Foo bar."); |
| 1088 HeadersToRaw(&headers); | 1113 HeadersToRaw(&headers); |
| 1089 scoped_refptr<HttpResponseHeaders> parsed = new HttpResponseHeaders(headers); | 1114 scoped_refptr<HttpResponseHeaders> parsed = new HttpResponseHeaders(headers); |
| 1090 // The bad status line would have gotten rewritten as | 1115 // The bad status line would have gotten rewritten as |
| 1091 // HTTP/1.0 200 OK. | 1116 // HTTP/1.0 200 OK. |
| 1092 EXPECT_EQ(std::string("OK"), parsed->GetStatusText()); | 1117 EXPECT_EQ(std::string("OK"), parsed->GetStatusText()); |
| 1093 } | 1118 } |
| OLD | NEW |