| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
| 10 #include "extensions/browser/api/web_request/form_data_parser.h" | 10 #include "extensions/browser/api/web_request/form_data_parser.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Attempts to create a parser corresponding to the |content_type_header|. | 17 // Attempts to create a parser corresponding to the |content_type_header|. |
| 18 // On success, returns the parser. | 18 // On success, returns the parser. |
| 19 scoped_ptr<FormDataParser> InitParser(const std::string& content_type_header) { | 19 std::unique_ptr<FormDataParser> InitParser( |
| 20 scoped_ptr<FormDataParser> parser( | 20 const std::string& content_type_header) { |
| 21 std::unique_ptr<FormDataParser> parser( |
| 21 FormDataParser::CreateFromContentTypeHeader(&content_type_header)); | 22 FormDataParser::CreateFromContentTypeHeader(&content_type_header)); |
| 22 if (parser.get() == NULL) | 23 if (parser.get() == NULL) |
| 23 return scoped_ptr<FormDataParser>(); | 24 return std::unique_ptr<FormDataParser>(); |
| 24 return parser; | 25 return parser; |
| 25 } | 26 } |
| 26 | 27 |
| 27 // Attempts to run the parser corresponding to the |content_type_header| | 28 // Attempts to run the parser corresponding to the |content_type_header| |
| 28 // on the source represented by the concatenation of blocks from |bytes|. | 29 // on the source represented by the concatenation of blocks from |bytes|. |
| 29 // On success, returns true and the parsed |output|, else false. | 30 // On success, returns true and the parsed |output|, else false. |
| 30 // Parsed |output| has names on even positions (0, 2, ...), values on odd ones. | 31 // Parsed |output| has names on even positions (0, 2, ...), values on odd ones. |
| 31 bool RunParser(const std::string& content_type_header, | 32 bool RunParser(const std::string& content_type_header, |
| 32 const std::vector<const base::StringPiece*>& bytes, | 33 const std::vector<const base::StringPiece*>& bytes, |
| 33 std::vector<std::string>* output) { | 34 std::vector<std::string>* output) { |
| 34 DCHECK(output); | 35 DCHECK(output); |
| 35 output->clear(); | 36 output->clear(); |
| 36 scoped_ptr<FormDataParser> parser(InitParser(content_type_header)); | 37 std::unique_ptr<FormDataParser> parser(InitParser(content_type_header)); |
| 37 if (!parser.get()) | 38 if (!parser.get()) |
| 38 return false; | 39 return false; |
| 39 FormDataParser::Result result; | 40 FormDataParser::Result result; |
| 40 for (size_t block = 0; block < bytes.size(); ++block) { | 41 for (size_t block = 0; block < bytes.size(); ++block) { |
| 41 if (!parser->SetSource(*(bytes[block]))) | 42 if (!parser->SetSource(*(bytes[block]))) |
| 42 return false; | 43 return false; |
| 43 while (parser->GetNextNameValue(&result)) { | 44 while (parser->GetNextNameValue(&result)) { |
| 44 output->push_back(result.name()); | 45 output->push_back(result.name()); |
| 45 output->push_back(result.value()); | 46 output->push_back(result.value()); |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 return parser->AllDataReadOK(); | 49 return parser->AllDataReadOK(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // Attempts to run the parser corresponding to the |content_type_header| | 52 // Attempts to run the parser corresponding to the |content_type_header| |
| 52 // on the source represented by the concatenation of blocks from |bytes|. | 53 // on the source represented by the concatenation of blocks from |bytes|. |
| 53 // Checks that the parser fails parsing. | 54 // Checks that the parser fails parsing. |
| 54 bool CheckParserFails(const std::string& content_type_header, | 55 bool CheckParserFails(const std::string& content_type_header, |
| 55 const std::vector<const base::StringPiece*>& bytes) { | 56 const std::vector<const base::StringPiece*>& bytes) { |
| 56 std::vector<std::string> output; | 57 std::vector<std::string> output; |
| 57 scoped_ptr<FormDataParser> parser(InitParser(content_type_header)); | 58 std::unique_ptr<FormDataParser> parser(InitParser(content_type_header)); |
| 58 if (!parser.get()) | 59 if (!parser.get()) |
| 59 return false; | 60 return false; |
| 60 FormDataParser::Result result; | 61 FormDataParser::Result result; |
| 61 for (size_t block = 0; block < bytes.size(); ++block) { | 62 for (size_t block = 0; block < bytes.size(); ++block) { |
| 62 if (!parser->SetSource(*(bytes[block]))) | 63 if (!parser->SetSource(*(bytes[block]))) |
| 63 break; | 64 break; |
| 64 while (parser->GetNextNameValue(&result)) { | 65 while (parser->GetNextNameValue(&result)) { |
| 65 output.push_back(result.name()); | 66 output.push_back(result.name()); |
| 66 output.push_back(result.value()); | 67 output.push_back(result.value()); |
| 67 } | 68 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // Fourth test: empty URL-encoded POST data. Note that an empty string is a | 244 // Fourth test: empty URL-encoded POST data. Note that an empty string is a |
| 244 // valid url-encoded value, so this should parse correctly. | 245 // valid url-encoded value, so this should parse correctly. |
| 245 std::vector<std::string> output; | 246 std::vector<std::string> output; |
| 246 input.clear(); | 247 input.clear(); |
| 247 input.push_back(&kUrlEncodedBytesEmpty); | 248 input.push_back(&kUrlEncodedBytesEmpty); |
| 248 EXPECT_TRUE(RunParser(kUrlEncoded, input, &output)); | 249 EXPECT_TRUE(RunParser(kUrlEncoded, input, &output)); |
| 249 EXPECT_EQ(0u, output.size()); | 250 EXPECT_EQ(0u, output.size()); |
| 250 } | 251 } |
| 251 | 252 |
| 252 } // namespace extensions | 253 } // namespace extensions |
| OLD | NEW |