OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "net/quic/core/spdy_utils.h" | 4 #include "net/quic/core/spdy_utils.h" |
5 | 5 |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
| 8 #include "net/quic/platform/api/quic_text_utils.h" |
9 #include "net/test/gtest_util.h" | 9 #include "net/test/gtest_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 using base::StringPiece; | 12 using base::StringPiece; |
12 using std::string; | 13 using std::string; |
13 using testing::UnorderedElementsAre; | 14 using testing::UnorderedElementsAre; |
14 using testing::Pair; | 15 using testing::Pair; |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 namespace test { | 18 namespace test { |
18 | 19 |
19 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { | 20 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { |
20 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | 21 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then |
21 // parses the serialized output and verifies that the end result is the same | 22 // parses the serialized output and verifies that the end result is the same |
22 // as the headers that the test started with. | 23 // as the headers that the test started with. |
23 | 24 |
24 SpdyHeaderBlock input_headers; | 25 SpdyHeaderBlock input_headers; |
25 input_headers[":pseudo1"] = "pseudo value1"; | 26 input_headers[":pseudo1"] = "pseudo value1"; |
26 input_headers[":pseudo2"] = "pseudo value2"; | 27 input_headers[":pseudo2"] = "pseudo value2"; |
27 input_headers["key1"] = "value1"; | 28 input_headers["key1"] = "value1"; |
28 const int64_t kContentLength = 1234; | 29 const int64_t kContentLength = 1234; |
29 input_headers["content-length"] = base::Int64ToString(kContentLength); | 30 input_headers["content-length"] = |
| 31 QuicTextUtils::Uint64ToString(kContentLength); |
30 input_headers["key2"] = "value2"; | 32 input_headers["key2"] = "value2"; |
31 | 33 |
32 // Serialize the header block. | 34 // Serialize the header block. |
33 string serialized_headers = | 35 string serialized_headers = |
34 SpdyUtils::SerializeUncompressedHeaders(input_headers); | 36 SpdyUtils::SerializeUncompressedHeaders(input_headers); |
35 | 37 |
36 // Take the serialized header block, and parse back into SpdyHeaderBlock. | 38 // Take the serialized header block, and parse back into SpdyHeaderBlock. |
37 SpdyHeaderBlock output_headers; | 39 SpdyHeaderBlock output_headers; |
38 int64_t content_length = -1; | 40 int64_t content_length = -1; |
39 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), | 41 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), |
40 serialized_headers.size(), | 42 serialized_headers.size(), |
41 &content_length, &output_headers)); | 43 &content_length, &output_headers)); |
42 | 44 |
43 // Should be back to the original headers. | 45 // Should be back to the original headers. |
44 EXPECT_EQ(content_length, kContentLength); | 46 EXPECT_EQ(content_length, kContentLength); |
45 EXPECT_EQ(output_headers, input_headers); | 47 EXPECT_EQ(output_headers, input_headers); |
46 } | 48 } |
47 | 49 |
48 TEST(SpdyUtilsTest, SerializeAndParseHeadersLargeContentLength) { | 50 TEST(SpdyUtilsTest, SerializeAndParseHeadersLargeContentLength) { |
49 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | 51 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then |
50 // parses the serialized output and verifies that the end result is the same | 52 // parses the serialized output and verifies that the end result is the same |
51 // as the headers that the test started with. | 53 // as the headers that the test started with. |
52 | 54 |
53 SpdyHeaderBlock input_headers; | 55 SpdyHeaderBlock input_headers; |
54 input_headers[":pseudo1"] = "pseudo value1"; | 56 input_headers[":pseudo1"] = "pseudo value1"; |
55 input_headers[":pseudo2"] = "pseudo value2"; | 57 input_headers[":pseudo2"] = "pseudo value2"; |
56 input_headers["key1"] = "value1"; | 58 input_headers["key1"] = "value1"; |
57 const int64_t kContentLength = 12345678900; | 59 const int64_t kContentLength = 12345678900; |
58 input_headers["content-length"] = base::Int64ToString(kContentLength); | 60 input_headers["content-length"] = |
| 61 QuicTextUtils::Uint64ToString(kContentLength); |
59 input_headers["key2"] = "value2"; | 62 input_headers["key2"] = "value2"; |
60 | 63 |
61 // Serialize the header block. | 64 // Serialize the header block. |
62 string serialized_headers = | 65 string serialized_headers = |
63 SpdyUtils::SerializeUncompressedHeaders(input_headers); | 66 SpdyUtils::SerializeUncompressedHeaders(input_headers); |
64 | 67 |
65 // Take the serialized header block, and parse back into SpdyHeaderBlock. | 68 // Take the serialized header block, and parse back into SpdyHeaderBlock. |
66 SpdyHeaderBlock output_headers; | 69 SpdyHeaderBlock output_headers; |
67 int64_t content_length = -1; | 70 int64_t content_length = -1; |
68 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), | 71 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), |
69 serialized_headers.size(), | 72 serialized_headers.size(), |
70 &content_length, &output_headers)); | 73 &content_length, &output_headers)); |
71 | 74 |
72 // Should be back to the original headers. | 75 // Should be back to the original headers. |
73 EXPECT_EQ(content_length, kContentLength); | 76 EXPECT_EQ(content_length, kContentLength); |
74 EXPECT_EQ(output_headers, input_headers); | 77 EXPECT_EQ(output_headers, input_headers); |
75 } | 78 } |
76 | 79 |
77 TEST(SpdyUtilsTest, SerializeAndParseValidTrailers) { | 80 TEST(SpdyUtilsTest, SerializeAndParseValidTrailers) { |
78 // Creates a SpdyHeaderBlock with some valid Trailers key->value pairs, | 81 // Creates a SpdyHeaderBlock with some valid Trailers key->value pairs, |
79 // serializes it, then parses the serialized output and verifies that the end | 82 // serializes it, then parses the serialized output and verifies that the end |
80 // result is the same as the trailers that the test started with. | 83 // result is the same as the trailers that the test started with. |
81 SpdyHeaderBlock input_trailers; | 84 SpdyHeaderBlock input_trailers; |
82 const size_t kFinalOffset = 5678; | 85 const size_t kFinalOffset = 5678; |
83 input_trailers[kFinalOffsetHeaderKey] = base::IntToString(kFinalOffset); | 86 input_trailers[kFinalOffsetHeaderKey] = |
| 87 QuicTextUtils::Uint64ToString(kFinalOffset); |
84 input_trailers["key1"] = "value1"; | 88 input_trailers["key1"] = "value1"; |
85 input_trailers["key2"] = "value2"; | 89 input_trailers["key2"] = "value2"; |
86 | 90 |
87 // Serialize the trailers. | 91 // Serialize the trailers. |
88 string serialized_trailers = | 92 string serialized_trailers = |
89 SpdyUtils::SerializeUncompressedHeaders(input_trailers); | 93 SpdyUtils::SerializeUncompressedHeaders(input_trailers); |
90 | 94 |
91 // Take the serialized trailers, and parse back into a SpdyHeaderBlock. | 95 // Take the serialized trailers, and parse back into a SpdyHeaderBlock. |
92 SpdyHeaderBlock output_trailers; | 96 SpdyHeaderBlock output_trailers; |
93 size_t final_byte_offset = 0; | 97 size_t final_byte_offset = 0; |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { | 378 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { |
375 SpdyHeaderBlock headers; | 379 SpdyHeaderBlock headers; |
376 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); | 380 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); |
377 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); | 381 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); |
378 EXPECT_FALSE( | 382 EXPECT_FALSE( |
379 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); | 383 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); |
380 } | 384 } |
381 | 385 |
382 } // namespace test | 386 } // namespace test |
383 } // namespace net | 387 } // namespace net |
OLD | NEW |