OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 #include "net/quic/spdy_utils.h" |
| 5 |
| 6 #include "base/macros.h" |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "net/test/gtest_util.h" |
| 9 |
| 10 using std::string; |
| 11 |
| 12 namespace net { |
| 13 namespace test { |
| 14 |
| 15 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { |
| 16 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then |
| 17 // parses the serialized output and verifies that the end result is the same |
| 18 // as the headers that the test started with. |
| 19 |
| 20 SpdyHeaderBlock input_headers; |
| 21 input_headers[":pseudo1"] = "pseudo value1"; |
| 22 input_headers[":pseudo2"] = "pseudo value2"; |
| 23 input_headers["key1"] = "value1"; |
| 24 const int kContentLength = 1234; |
| 25 input_headers["content-length"] = base::IntToString(kContentLength); |
| 26 input_headers["key2"] = "value2"; |
| 27 |
| 28 // Serialize the header block. |
| 29 string serialized_headers = |
| 30 SpdyUtils::SerializeUncompressedHeaders(input_headers); |
| 31 |
| 32 // Take the serialized header block, and parse back into SpdyHeaderBlock. |
| 33 SpdyHeaderBlock output_headers; |
| 34 int content_length = -1; |
| 35 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), |
| 36 serialized_headers.size(), |
| 37 &content_length, &output_headers)); |
| 38 |
| 39 // Should be back to the original headers. |
| 40 EXPECT_EQ(content_length, kContentLength); |
| 41 EXPECT_EQ(output_headers, input_headers); |
| 42 } |
| 43 |
| 44 TEST(SpdyUtilsTest, SerializeAndParseValidTrailers) { |
| 45 // Creates a SpdyHeaderBlock with some valid Trailers key->value pairs, |
| 46 // serializes it, then parses the serialized output and verifies that the end |
| 47 // result is the same as the trailers that the test started with. |
| 48 SpdyHeaderBlock input_trailers; |
| 49 const size_t kFinalOffset = 5678; |
| 50 input_trailers[kFinalOffsetHeaderKey] = base::IntToString(kFinalOffset); |
| 51 input_trailers["key1"] = "value1"; |
| 52 input_trailers["key2"] = "value2"; |
| 53 |
| 54 // Serialize the trailers. |
| 55 string serialized_trailers = |
| 56 SpdyUtils::SerializeUncompressedHeaders(input_trailers); |
| 57 |
| 58 // Take the serialized trailers, and parse back into a SpdyHeaderBlock. |
| 59 SpdyHeaderBlock output_trailers; |
| 60 size_t final_byte_offset = 0; |
| 61 EXPECT_TRUE(SpdyUtils::ParseTrailers(serialized_trailers.data(), |
| 62 serialized_trailers.size(), |
| 63 &final_byte_offset, &output_trailers)); |
| 64 |
| 65 // Should be back to the original trailers, without the final offset header. |
| 66 EXPECT_EQ(final_byte_offset, kFinalOffset); |
| 67 input_trailers.erase(kFinalOffsetHeaderKey); |
| 68 EXPECT_EQ(output_trailers, input_trailers); |
| 69 } |
| 70 |
| 71 TEST(SpdyUtilsTest, SerializeAndParseTrailersWithoutFinalOffset) { |
| 72 // Verifies that parsing fails if Trailers are missing a final offset header. |
| 73 |
| 74 SpdyHeaderBlock input_trailers; |
| 75 input_trailers["key1"] = "value1"; |
| 76 input_trailers["key2"] = "value2"; |
| 77 |
| 78 // Serialize the trailers. |
| 79 string serialized_trailers = |
| 80 SpdyUtils::SerializeUncompressedHeaders(input_trailers); |
| 81 |
| 82 // Parsing the serialized trailers fails because of the missing final offset. |
| 83 SpdyHeaderBlock output_trailers; |
| 84 size_t final_byte_offset = 0; |
| 85 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), |
| 86 serialized_trailers.size(), |
| 87 &final_byte_offset, &output_trailers)); |
| 88 EXPECT_EQ(final_byte_offset, 0u); |
| 89 } |
| 90 |
| 91 TEST(SpdyUtilsTest, SerializeAndParseTrailersWithPseudoHeaders) { |
| 92 // Verifies that parsing fails if Trailers include pseudo-headers. |
| 93 |
| 94 SpdyHeaderBlock input_trailers; |
| 95 input_trailers[kFinalOffsetHeaderKey] = "12345"; |
| 96 input_trailers[":disallowed-pseudo-header"] = "pseudo value"; |
| 97 input_trailers["key1"] = "value1"; |
| 98 input_trailers["key2"] = "value2"; |
| 99 |
| 100 // Serialize the trailers. |
| 101 string serialized_trailers = |
| 102 SpdyUtils::SerializeUncompressedHeaders(input_trailers); |
| 103 |
| 104 // Parsing the serialized trailers fails because of the extra pseudo header. |
| 105 SpdyHeaderBlock output_trailers; |
| 106 size_t final_byte_offset = 0; |
| 107 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), |
| 108 serialized_trailers.size(), |
| 109 &final_byte_offset, &output_trailers)); |
| 110 } |
| 111 |
| 112 TEST(SpdyUtilsTest, GetUrlFromHeaderBlock) { |
| 113 SpdyHeaderBlock headers; |
| 114 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 115 headers[":scheme"] = "https"; |
| 116 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 117 headers[":authority"] = "www.google.com"; |
| 118 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 119 headers[":path"] = "/index.html"; |
| 120 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), |
| 121 "https://www.google.com/index.html"); |
| 122 headers["key1"] = "value1"; |
| 123 headers["key2"] = "value2"; |
| 124 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), |
| 125 "https://www.google.com/index.html"); |
| 126 } |
| 127 |
| 128 TEST(SpdyUtilsTest, UrlIsValid) { |
| 129 SpdyHeaderBlock headers; |
| 130 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); |
| 131 headers[":scheme"] = "https"; |
| 132 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); |
| 133 headers[":authority"] = "www.google.com"; |
| 134 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); |
| 135 headers[":path"] = "/index.html"; |
| 136 EXPECT_TRUE(SpdyUtils::UrlIsValid(headers)); |
| 137 } |
| 138 |
| 139 } // namespace test |
| 140 } // namespace net_quic |
OLD | NEW |