| Index: chrome/browser/extensions/api/web_request/post_data_parser_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/web_request/post_data_parser_unittest.cc b/chrome/browser/extensions/api/web_request/post_data_parser_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..808beab24d5e65aeb5f36285552c96c3422c817d
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/web_request/post_data_parser_unittest.cc
|
| @@ -0,0 +1,147 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/extensions/api/web_request/post_data_parser.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +// Attempts to run the parser corresponding to the |content_type_header|
|
| +// on the source represented by the concatenation of blocks from |bytes|.
|
| +// On success, returns true and the parsed |output|, else false.
|
| +// Parsed |output| has keys on even positions (0, 2, ...), values on odd ones.
|
| +bool RunParser(const std::string& content_type_header,
|
| + const std::vector<const std::vector<char>* >& bytes,
|
| + std::vector<std::string>* output) {
|
| + if (output == NULL)
|
| + return false;
|
| + output->clear();
|
| + scoped_ptr<extensions::PostDataParser> parser(
|
| + extensions::PostDataParser::CreatePostDataParser(&content_type_header));
|
| + if (parser.get() == NULL)
|
| + return false;
|
| + extensions::PostDataParser::Result result;
|
| + for (size_t block = 0; block < bytes.size(); ++block) {
|
| + if (!parser->SetSource(bytes[block]))
|
| + return false;
|
| + while (parser->GetNextPair(&result)) {
|
| + output->push_back(result.key());
|
| + output->push_back(result.val());
|
| + }
|
| + }
|
| + return parser->AllDataReadOK();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class ExtensionWebRequestPostDataParserTest : public testing::Test {
|
| + public:
|
| + ExtensionWebRequestPostDataParserTest() {}
|
| +
|
| + protected:
|
| + virtual void SetUp() OVERRIDE {}
|
| +};
|
| +
|
| +TEST_F(ExtensionWebRequestPostDataParserTest, Parsing) {
|
| + // We verify that POST data parsers cope with various formats of POST data.
|
| + // Construct the test data.
|
| +#define kBoundary "THIS_IS_A_BOUNDARY"
|
| +#define kBlockStr1 "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"text\"\r\n" \
|
| + "\r\n" \
|
| + "test text\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"file\"; filename=\"test\"\r\n" \
|
| + "Content-Type: application/octet-stream\r\n" \
|
| + "\r\n"
|
| +#define kBlockStr2 "\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"password\"\r\n" \
|
| + "\r\n" \
|
| + "test password\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"radio\"\r\n" \
|
| + "\r\n" \
|
| + "Yes\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"check\"\r\n" \
|
| + "\r\n" \
|
| + "option A\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"check\"\r\n" \
|
| + "\r\n" \
|
| + "option B\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"txtarea\"\r\n" \
|
| + "\r\n" \
|
| + "Some text.\r\n" \
|
| + "Other.\r\n" \
|
| + "\r\n" \
|
| + "--" kBoundary "\r\n" \
|
| + "Content-Disposition: form-data; name=\"select\"\r\n" \
|
| + "\r\n" \
|
| + "one\r\n" \
|
| + "--" kBoundary "--"
|
| + // POST data input.
|
| + const char kBigBlock[] = kBlockStr1 kBlockStr2;
|
| + const char kBlock1[] = kBlockStr1;
|
| + const char kBlock2[] = kBlockStr2;
|
| + const char kUrlEncodedBlock[] = "text=test+text&file=test"
|
| + "&password=test+password&radio=Yes&check=option+A&check=option+B"
|
| + "&txtarea=Some+text.%0D%0AOther.%0D%0A&select=one";
|
| +#define BYTES_FROM_BLOCK(bytes, block) \
|
| + const std::vector<char> bytes(block, block + strlen(block))
|
| + BYTES_FROM_BLOCK(kMultipartBytes, kBigBlock);
|
| + BYTES_FROM_BLOCK(kMultipartBytesSplit1, kBlock1);
|
| + BYTES_FROM_BLOCK(kMultipartBytesSplit2, kBlock2);
|
| + BYTES_FROM_BLOCK(kUrlEncodedBytes, kUrlEncodedBlock);
|
| + const std::vector<char> kTextPlainBytes;
|
| +#undef BYTES_FROM_BLOCK
|
| + // Headers.
|
| + const char kUrlEncoded[] = "application/x-www-form-urlencoded";
|
| + const char kTextPlain[] = "text/plain";
|
| + const char kMultipart[] = "multipart/form-data; boundary=" kBoundary;
|
| +#undef kBlockStr2
|
| +#undef kBlockStr1
|
| +#undef kBoundary
|
| + // Expected output.
|
| + const char* kPairs[] = {
|
| + "text", "test text",
|
| + "file", "test",
|
| + "password", "test password",
|
| + "radio", "Yes",
|
| + "check", "option A",
|
| + "check", "option B",
|
| + "txtarea", "Some text.\r\nOther.\r\n",
|
| + "select", "one"
|
| + };
|
| + const std::vector<std::string> kExpected(kPairs, kPairs + arraysize(kPairs));
|
| +
|
| + std::vector<const std::vector<char>* > input;
|
| + std::vector<std::string> output;
|
| +
|
| + // First test: multipart POST data in one lump.
|
| + input.push_back(&kMultipartBytes);
|
| + EXPECT_TRUE(RunParser(kMultipart, input, &output));
|
| + EXPECT_EQ(kExpected, output);
|
| +
|
| + // Second test: multipart POST data in several lumps.
|
| + input.clear();
|
| + input.push_back(&kMultipartBytesSplit1);
|
| + input.push_back(&kMultipartBytesSplit2);
|
| + EXPECT_TRUE(RunParser(kMultipart, input, &output));
|
| + EXPECT_EQ(kExpected, output);
|
| +
|
| + // Third test: URL-encoded POST data.
|
| + input.clear();
|
| + input.push_back(&kUrlEncodedBytes);
|
| + EXPECT_TRUE(RunParser(kUrlEncoded, input, &output));
|
| + EXPECT_EQ(kExpected, output);
|
| +
|
| + // Fourth test: text/plain POST data in one lump.
|
| + input.clear();
|
| + input.push_back(&kTextPlainBytes);
|
| + // This should fail, text/plain is ambiguous and thus unparseable.
|
| + EXPECT_FALSE(RunParser(kTextPlain, input, &output));
|
| +}
|
|
|