Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: chrome/browser/extensions/api/web_request/form_data_parser_unittest.cc

Issue 10694055: Add read-only access to POST data for webRequest's onBeforeRequest (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Indents corrected + LazyInstance made Leaky Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 #include "base/basictypes.h"
6 #include "base/string_piece.h"
7 #include "chrome/browser/extensions/api/web_request/form_data_parser.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace extensions {
11
12 namespace {
13
14 // Attempts to run the parser corresponding to the |content_type_header|
15 // on the source represented by the concatenation of blocks from |bytes|.
16 // On success, returns true and the parsed |output|, else false.
17 // Parsed |output| has names on even positions (0, 2, ...), values on odd ones.
18 bool RunParser(const std::string& content_type_header,
19 const std::vector<const base::StringPiece* >& bytes,
battre 2012/09/09 22:08:35 nit: - space before >
vabr (Chromium) 2012/09/12 11:18:08 Done.
20 std::vector<std::string>* output) {
21 if (output == NULL)
22 return false;
23 output->clear();
24 scoped_ptr<FormDataParser> parser(
25 FormDataParser::Create(&content_type_header));
26 if (parser.get() == NULL)
27 return false;
28 FormDataParser::Result result;
29 for (size_t block = 0; block < bytes.size(); ++block) {
30 if (!parser->SetSource(*(bytes[block])))
31 return false;
32 while (parser->GetNextNameValue(&result)) {
33 output->push_back(result.name());
34 output->push_back(result.value());
35 }
36 }
37 return parser->AllDataReadOK();
38 }
39
40 } // namespace
41
42 class WebRequestFormDataParserTest : public testing::Test {
43 public:
44 WebRequestFormDataParserTest() {}
45
46 protected:
47 virtual void SetUp() OVERRIDE {}
battre 2012/09/09 22:08:35 please remove empty SetUp methods
vabr (Chromium) 2012/09/12 11:18:08 Done.
48 };
battre 2012/09/09 22:08:35 Please remove the entire class and replace TEST_F
vabr (Chromium) 2012/09/12 11:18:08 Done. Sorry about that, I remember you had to poin
49
50 TEST_F(WebRequestFormDataParserTest, Parsing) {
51 // We verify that POST data parsers cope with various formats of POST data.
52 // Construct the test data.
53 #define kBoundary "THIS_IS_A_BOUNDARY"
54 #define kBlockStr1 "--" kBoundary "\r\n" \
55 "Content-Disposition: form-data; name=\"text\"\r\n" \
56 "\r\n" \
57 "test\rtext\nwith non-CRLF line breaks\r\n" \
58 "--" kBoundary "\r\n" \
59 "Content-Disposition: form-data; name=\"file\"; filename=\"test\"\r\n" \
60 "Content-Type: application/octet-stream\r\n" \
61 "\r\n"
62 #define kBlockStr2 "\r\n" \
63 "--" kBoundary "\r\n" \
64 "Content-Disposition: form-data; name=\"password\"\r\n" \
65 "\r\n" \
66 "test password\r\n" \
67 "--" kBoundary "\r\n" \
68 "Content-Disposition: form-data; name=\"radio\"\r\n" \
69 "\r\n" \
70 "Yes\r\n" \
71 "--" kBoundary "\r\n" \
72 "Content-Disposition: form-data; name=\"check\"\r\n" \
73 "\r\n" \
74 "option A\r\n" \
75 "--" kBoundary "\r\n" \
76 "Content-Disposition: form-data; name=\"check\"\r\n" \
77 "\r\n" \
78 "option B\r\n" \
79 "--" kBoundary "\r\n" \
80 "Content-Disposition: form-data; name=\"txtarea\"\r\n" \
81 "\r\n" \
82 "Some text.\r\n" \
83 "Other.\r\n" \
84 "\r\n" \
85 "--" kBoundary "\r\n" \
86 "Content-Disposition: form-data; name=\"select\"\r\n" \
87 "\r\n" \
88 "one\r\n" \
89 "--" kBoundary "--"
90 // POST data input.
91 const char kBigBlock[] = kBlockStr1 kBlockStr2;
92 const char kBlock1[] = kBlockStr1;
93 const char kBlock2[] = kBlockStr2;
94 const char kUrlEncodedBlock[] = "text=test%0Dtext%0Awith+non-CRLF+line+breaks"
95 "&file=test&password=test+password&radio=Yes&check=option+A"
96 "&check=option+B&txtarea=Some+text.%0D%0AOther.%0D%0A&select=one";
97 #define BYTES_FROM_BLOCK(bytes, block) \
98 const base::StringPiece bytes(block, sizeof(block) - 1)
99 BYTES_FROM_BLOCK(kMultipartBytes, kBigBlock);
100 BYTES_FROM_BLOCK(kMultipartBytesSplit1, kBlock1);
101 BYTES_FROM_BLOCK(kMultipartBytesSplit2, kBlock2);
102 BYTES_FROM_BLOCK(kUrlEncodedBytes, kUrlEncodedBlock);
103 const char kPlainBlock[] = "abc";
104 const base::StringPiece kTextPlainBytes(kPlainBlock, sizeof(kPlainBlock) - 1);
105 #undef BYTES_FROM_BLOCK
106 // Headers.
107 const char kUrlEncoded[] = "application/x-www-form-urlencoded";
108 const char kTextPlain[] = "text/plain";
109 const char kMultipart[] = "multipart/form-data; boundary=" kBoundary;
110 #undef kBlockStr2
111 #undef kBlockStr1
112 #undef kBoundary
113 // Expected output.
114 const char* kPairs[] = {
115 "text", "test\rtext\nwith non-CRLF line breaks",
116 "file", "test",
117 "password", "test password",
118 "radio", "Yes",
119 "check", "option A",
120 "check", "option B",
121 "txtarea", "Some text.\r\nOther.\r\n",
122 "select", "one"
123 };
124 const std::vector<std::string> kExpected(kPairs, kPairs + arraysize(kPairs));
125
126 std::vector<const base::StringPiece*> input;
127 std::vector<std::string> output;
128
129 // First test: multipart POST data in one lump.
130 input.push_back(&kMultipartBytes);
131 EXPECT_TRUE(RunParser(kMultipart, input, &output));
132 EXPECT_EQ(kExpected, output);
133
134 // Second test: multipart POST data in several lumps.
135 input.clear();
136 input.push_back(&kMultipartBytesSplit1);
137 input.push_back(&kMultipartBytesSplit2);
138 EXPECT_TRUE(RunParser(kMultipart, input, &output));
139 EXPECT_EQ(kExpected, output);
140
141 // Third test: URL-encoded POST data.
142 input.clear();
143 input.push_back(&kUrlEncodedBytes);
144 EXPECT_TRUE(RunParser(kUrlEncoded, input, &output));
145 EXPECT_EQ(kExpected, output);
146
147 // Fourth test: text/plain POST data in one lump.
148 input.clear();
149 input.push_back(&kTextPlainBytes);
150 // This should fail, text/plain is ambiguous and thus unparseable.
151 EXPECT_FALSE(RunParser(kTextPlain, input, &output));
152 }
153
154 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698