proxygen
RFC2616Test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
12 
13 using namespace proxygen;
14 
15 using std::string;
17 
18 TEST(QvalueTest, Basic) {
19 
20  std::vector<RFC2616::TokenQPair> output;
21 
22  string test1("iso-8859-5, unicode-1-1;q=0.8");
23  EXPECT_TRUE(RFC2616::parseQvalues(test1, output));
24  EXPECT_EQ(output.size(), 2);
25  EXPECT_EQ(output[0].first.compare(folly::StringPiece("iso-8859-5")), 0);
26  EXPECT_DOUBLE_EQ(output[0].second, 1);
27  EXPECT_EQ(output[1].first.compare(folly::StringPiece("unicode-1-1")), 0);
28  EXPECT_DOUBLE_EQ(output[1].second, 0.8);
29  output.clear();
30 
31  string test2("compress, gzip");
32  EXPECT_TRUE(RFC2616::parseQvalues(test2, output));
33  EXPECT_EQ(output.size(), 2);
34  EXPECT_EQ(output[0].first.compare(folly::StringPiece("compress")), 0);
35  EXPECT_DOUBLE_EQ(output[0].second, 1);
36  EXPECT_EQ(output[1].first.compare(folly::StringPiece("gzip")), 0);
37  EXPECT_DOUBLE_EQ(output[1].second, 1);
38  output.clear();
39 
40  string test3("");
41  // The spec says a blank one is ok but empty headers are disallowed in SPDY?
42  EXPECT_FALSE(RFC2616::parseQvalues(test3, output));
43  EXPECT_EQ(output.size(), 0);
44 
45  string test4("compress;q=0.5, gzip;q=1.0");
46  EXPECT_TRUE(RFC2616::parseQvalues(test4, output));
47  EXPECT_EQ(output.size(), 2);
48  EXPECT_EQ(output[0].first.compare(folly::StringPiece("compress")), 0);
49  EXPECT_DOUBLE_EQ(output[0].second, 0.5);
50  EXPECT_EQ(output[1].first.compare(folly::StringPiece("gzip")), 0);
51  EXPECT_DOUBLE_EQ(output[1].second, 1.0);
52  output.clear();
53 
54  string test5("gzip;q=1.0, identity; q=0.5, *;q=0");
55  EXPECT_TRUE(RFC2616::parseQvalues(test5, output));
56  EXPECT_EQ(output.size(), 3);
57  EXPECT_EQ(output[0].first.compare(folly::StringPiece("gzip")), 0);
58  EXPECT_DOUBLE_EQ(output[0].second, 1);
59  EXPECT_EQ(output[1].first.compare(folly::StringPiece("identity")), 0);
60  EXPECT_DOUBLE_EQ(output[1].second, 0.5);
61  EXPECT_EQ(output[2].first.compare(folly::StringPiece("*")), 0);
62  EXPECT_DOUBLE_EQ(output[2].second, 0);
63  output.clear();
64 
65  string test6("da, en-gb;q=0.8, en;q=0.7");
66  EXPECT_TRUE(RFC2616::parseQvalues(test6, output));
67  EXPECT_EQ(output.size(), 3);
68  EXPECT_EQ(output[0].first.compare(folly::StringPiece("da")), 0);
69  EXPECT_DOUBLE_EQ(output[0].second, 1);
70  EXPECT_EQ(output[1].first.compare(folly::StringPiece("en-gb")), 0);
71  EXPECT_DOUBLE_EQ(output[1].second, 0.8);
72  EXPECT_EQ(output[2].first.compare(folly::StringPiece("en")), 0);
73  EXPECT_DOUBLE_EQ(output[2].second, 0.7);
74  output.clear();
75 }
76 
77 TEST(QvalueTest, Extras) {
78 
79  std::vector<RFC2616::TokenQPair> output;
80 
81  string test1(" iso-8859-5, unicode-1-1; q=0.8 hi mom!");
82  EXPECT_TRUE(RFC2616::parseQvalues(test1, output));
83  EXPECT_EQ(output.size(), 2);
84  EXPECT_EQ(output[0].first.compare(folly::StringPiece("iso-8859-5")), 0);
85  EXPECT_DOUBLE_EQ(output[0].second, 1);
86  EXPECT_EQ(output[1].first.compare(folly::StringPiece("unicode-1-1")), 0);
87  EXPECT_DOUBLE_EQ(output[1].second, 0.8);
88  output.clear();
89 
90  string test2("gzip");
91  EXPECT_TRUE(RFC2616::parseQvalues(test2, output));
92  EXPECT_EQ(output.size(), 1);
93  EXPECT_EQ(output[0].first.compare(folly::StringPiece("gzip")), 0);
94  EXPECT_DOUBLE_EQ(output[0].second, 1);
95  output.clear();
96 }
97 
98 TEST(QvalueTest, Invalids) {
99 
100  std::vector<RFC2616::TokenQPair> output;
101 
102  string test1(",,,");
103  EXPECT_FALSE(RFC2616::parseQvalues(test1, output));
104  EXPECT_EQ(output.size(), 0);
105  output.clear();
106 
107  string test2(" ; q=0.1");
108  EXPECT_FALSE(RFC2616::parseQvalues(test2, output));
109  EXPECT_EQ(output.size(), 0);
110  output.clear();
111 
112  string test3("gzip; q=uietplease");
113  EXPECT_FALSE(RFC2616::parseQvalues(test3, output));
114  EXPECT_EQ(output.size(), 1);
115  EXPECT_EQ(output[0].first.compare(folly::StringPiece("gzip")), 0);
116  EXPECT_DOUBLE_EQ(output[0].second, 1);
117  output.clear();
118 
119  string test4("gzip; whoohoo, defalte");
120  EXPECT_FALSE(RFC2616::parseQvalues(test4, output));
121  EXPECT_EQ(output.size(), 2);
122  EXPECT_EQ(output[0].first.compare(folly::StringPiece("gzip")), 0);
123  EXPECT_DOUBLE_EQ(output[0].second, 1);
124  EXPECT_EQ(output[1].first.compare(folly::StringPiece("defalte")), 0);
125  EXPECT_DOUBLE_EQ(output[1].second, 1);
126  output.clear();
127 
128 }
129 
130 TEST(ByteRangeSpecTest, Valids) {
131  unsigned long firstByte = ULONG_MAX;
132  unsigned long lastByte = ULONG_MAX;
133  unsigned long instanceLength = ULONG_MAX;
134 
135  ASSERT_TRUE(
137  "bytes 0-10/100",
138  firstByte, lastByte, instanceLength));
139  EXPECT_EQ(0, firstByte);
140  EXPECT_EQ(10, lastByte);
141  EXPECT_EQ(100, instanceLength);
142 
143  ASSERT_TRUE(
145  "bytes */100",
146  firstByte, lastByte, instanceLength));
147  EXPECT_EQ(0, firstByte);
148  EXPECT_EQ(ULONG_MAX, lastByte);
149  EXPECT_EQ(100, instanceLength);
150 
151  ASSERT_TRUE(
153  "bytes 0-10/*",
154  firstByte, lastByte, instanceLength));
155  EXPECT_EQ(0, firstByte);
156  EXPECT_EQ(10, lastByte);
157  EXPECT_EQ(ULONG_MAX, instanceLength);
158 }
159 
160 TEST(ByteRangeSpecTest, Invalids) {
161  unsigned long dummy;
162 
163  EXPECT_FALSE(parseByteRangeSpec("0-10/100", dummy, dummy, dummy)) <<
164  "Spec must start with 'bytes '";
165  EXPECT_FALSE(parseByteRangeSpec("bytes 10/100", dummy, dummy, dummy)) <<
166  "Spec missing initial range";
167  EXPECT_FALSE(parseByteRangeSpec("bytes 10-/100", dummy, dummy, dummy)) <<
168  "Spec missing last byte in initial range";
169  EXPECT_FALSE(parseByteRangeSpec("bytes 0-10 100", dummy, dummy, dummy)) <<
170  "Spec missing '/' separator";
171  EXPECT_FALSE(parseByteRangeSpec("bytes 0-10/100Q", dummy, dummy, dummy)) <<
172  "Spec has trailing garbage";
173  EXPECT_FALSE(parseByteRangeSpec("bytes 10-1/100", dummy, dummy, dummy)) <<
174  "Spec initial range is invalid";
175  EXPECT_FALSE(parseByteRangeSpec("bytes 10-90/50", dummy, dummy, dummy)) <<
176  "Spec initial range is invalid too large";
177  EXPECT_FALSE(parseByteRangeSpec("bytes x/100", dummy, dummy, dummy)) <<
178  "Spec initial range has invalid first byte";
179  EXPECT_FALSE(parseByteRangeSpec("bytes 0-x/100", dummy, dummy, dummy)) <<
180  "Spec initial range has invalid last bytek";
181  EXPECT_FALSE(parseByteRangeSpec("bytes *-10/100", dummy, dummy, dummy)) <<
182  "Spec cannot contain wildcard in initial range";
183  EXPECT_FALSE(parseByteRangeSpec("bytes 0-*/100", dummy, dummy, dummy)) <<
184  "Spec cannot contain wildcard in initial range";
185 
186  folly::StringPiece sp("bytes 0-10/100");
187  sp.subtract(3);
188  EXPECT_FALSE(parseByteRangeSpec(sp, dummy, dummy, dummy)) <<
189  "Spec StringPiece ends before instance length";
190  sp.subtract(1);
191  EXPECT_FALSE(parseByteRangeSpec(sp, dummy, dummy, dummy)) <<
192  "Spec StringPiece ends before '/' character";
193  sp.subtract(2);
194  EXPECT_FALSE(parseByteRangeSpec(sp, dummy, dummy, dummy)) <<
195  "Spec StringPiece ends before last byte in initial byte range";
196  sp.subtract(1);
197  EXPECT_FALSE(parseByteRangeSpec(sp, dummy, dummy, dummy)) <<
198  "Spec StringPiece ends before '-' in initial byte range";
199  sp.subtract(2);
200  EXPECT_FALSE(parseByteRangeSpec(sp, dummy, dummy, dummy)) <<
201  "Spec StringPiece ends before first byte in initial byte range";
202 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST(QvalueTest, Basic)
Definition: RFC2616Test.cpp:18
void dummy()
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_DOUBLE_EQ(val1, val2)
Definition: gtest.h:2031
const char * string
Definition: Conv.cpp:212
void subtract(size_type n)
Definition: Range.h:679
bool parseByteRangeSpec(folly::StringPiece value, unsigned long &outFirstByte, unsigned long &outLastByte, unsigned long &outInstanceLength)
Definition: RFC2616.cpp:105
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
bool parseQvalues(folly::StringPiece value, std::vector< TokenQPair > &output)
Definition: RFC2616.cpp:64
constexpr detail::First first
Definition: Base-inl.h:2553