| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/extensions/api/web_request/upload_data_presenter.h" | 7 #include "chrome/browser/extensions/api/web_request/upload_data_presenter.h" |
| 8 #include "chrome/browser/extensions/api/web_request/web_request_api_constants.h" | 8 #include "chrome/browser/extensions/api/web_request/web_request_api_constants.h" |
| 9 #include "net/base/upload_bytes_element_reader.h" | 9 #include "net/base/upload_bytes_element_reader.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using base::BinaryValue; | |
| 13 using base::ListValue; | |
| 14 using base::StringValue; | |
| 15 using base::Value; | |
| 16 | |
| 17 namespace keys = extension_web_request_api_constants; | 12 namespace keys = extension_web_request_api_constants; |
| 18 | 13 |
| 19 namespace extensions { | 14 namespace extensions { |
| 20 | 15 |
| 21 // This only tests the handling of dots in keys. Other functionality is covered | 16 // This only tests the handling of dots in keys. Other functionality is covered |
| 22 // by ExtensionWebRequestTest.AccessRequestBodyData and | 17 // by ExtensionWebRequestTest.AccessRequestBodyData and |
| 23 // WebRequestFormDataParserTest. | 18 // WebRequestFormDataParserTest. |
| 24 TEST(WebRequestUploadDataPresenterTest, ParsedData) { | 19 TEST(WebRequestUploadDataPresenterTest, ParsedData) { |
| 25 // Input. | 20 // Input. |
| 26 const char block[] = "key.with.dots=value"; | 21 const char block[] = "key.with.dots=value"; |
| 27 net::UploadBytesElementReader element(block, sizeof(block) - 1); | 22 net::UploadBytesElementReader element(block, sizeof(block) - 1); |
| 28 | 23 |
| 29 // Expected output. | 24 // Expected output. |
| 30 scoped_ptr<ListValue> values(new ListValue); | 25 scoped_ptr<base::ListValue> values(new base::ListValue); |
| 31 values->Append(Value::CreateStringValue("value")); | 26 values->Append(base::Value::CreateStringValue("value")); |
| 32 DictionaryValue expected_form; | 27 base::DictionaryValue expected_form; |
| 33 expected_form.SetWithoutPathExpansion("key.with.dots", values.release()); | 28 expected_form.SetWithoutPathExpansion("key.with.dots", values.release()); |
| 34 | 29 |
| 35 // Real output. | 30 // Real output. |
| 36 scoped_ptr<ParsedDataPresenter> parsed_data_presenter( | 31 scoped_ptr<ParsedDataPresenter> parsed_data_presenter( |
| 37 ParsedDataPresenter::CreateForTests()); | 32 ParsedDataPresenter::CreateForTests()); |
| 38 ASSERT_TRUE(parsed_data_presenter.get() != NULL); | 33 ASSERT_TRUE(parsed_data_presenter.get() != NULL); |
| 39 parsed_data_presenter->FeedNext(element); | 34 parsed_data_presenter->FeedNext(element); |
| 40 EXPECT_TRUE(parsed_data_presenter->Succeeded()); | 35 EXPECT_TRUE(parsed_data_presenter->Succeeded()); |
| 41 scoped_ptr<Value> result = parsed_data_presenter->Result(); | 36 scoped_ptr<base::Value> result = parsed_data_presenter->Result(); |
| 42 ASSERT_TRUE(result.get() != NULL); | 37 ASSERT_TRUE(result.get() != NULL); |
| 43 | 38 |
| 44 EXPECT_TRUE(result->Equals(&expected_form)); | 39 EXPECT_TRUE(result->Equals(&expected_form)); |
| 45 } | 40 } |
| 46 | 41 |
| 47 TEST(WebRequestUploadDataPresenterTest, RawData) { | 42 TEST(WebRequestUploadDataPresenterTest, RawData) { |
| 48 // Input. | 43 // Input. |
| 49 const char block1[] = "test"; | 44 const char block1[] = "test"; |
| 50 const size_t block1_size = sizeof(block1) - 1; | 45 const size_t block1_size = sizeof(block1) - 1; |
| 51 const char kFilename[] = "path/test_filename.ext"; | 46 const char kFilename[] = "path/test_filename.ext"; |
| 52 const char block2[] = "another test"; | 47 const char block2[] = "another test"; |
| 53 const size_t block2_size = sizeof(block2) - 1; | 48 const size_t block2_size = sizeof(block2) - 1; |
| 54 | 49 |
| 55 // Expected output. | 50 // Expected output. |
| 56 scoped_ptr<BinaryValue> expected_a( | 51 scoped_ptr<base::BinaryValue> expected_a( |
| 57 BinaryValue::CreateWithCopiedBuffer(block1, block1_size)); | 52 base::BinaryValue::CreateWithCopiedBuffer(block1, block1_size)); |
| 58 ASSERT_TRUE(expected_a.get() != NULL); | 53 ASSERT_TRUE(expected_a.get() != NULL); |
| 59 scoped_ptr<StringValue> expected_b(Value::CreateStringValue(kFilename)); | 54 scoped_ptr<base::StringValue> expected_b( |
| 55 base::Value::CreateStringValue(kFilename)); |
| 60 ASSERT_TRUE(expected_b.get() != NULL); | 56 ASSERT_TRUE(expected_b.get() != NULL); |
| 61 scoped_ptr<BinaryValue> expected_c( | 57 scoped_ptr<base::BinaryValue> expected_c( |
| 62 BinaryValue::CreateWithCopiedBuffer(block2, block2_size)); | 58 base::BinaryValue::CreateWithCopiedBuffer(block2, block2_size)); |
| 63 ASSERT_TRUE(expected_c.get() != NULL); | 59 ASSERT_TRUE(expected_c.get() != NULL); |
| 64 | 60 |
| 65 ListValue expected_list; | 61 base::ListValue expected_list; |
| 66 subtle::AppendKeyValuePair( | 62 subtle::AppendKeyValuePair( |
| 67 keys::kRequestBodyRawBytesKey, expected_a.release(), &expected_list); | 63 keys::kRequestBodyRawBytesKey, expected_a.release(), &expected_list); |
| 68 subtle::AppendKeyValuePair( | 64 subtle::AppendKeyValuePair( |
| 69 keys::kRequestBodyRawFileKey, expected_b.release(), &expected_list); | 65 keys::kRequestBodyRawFileKey, expected_b.release(), &expected_list); |
| 70 subtle::AppendKeyValuePair( | 66 subtle::AppendKeyValuePair( |
| 71 keys::kRequestBodyRawBytesKey, expected_c.release(), &expected_list); | 67 keys::kRequestBodyRawBytesKey, expected_c.release(), &expected_list); |
| 72 | 68 |
| 73 // Real output. | 69 // Real output. |
| 74 RawDataPresenter raw_presenter; | 70 RawDataPresenter raw_presenter; |
| 75 raw_presenter.FeedNextBytes(block1, block1_size); | 71 raw_presenter.FeedNextBytes(block1, block1_size); |
| 76 raw_presenter.FeedNextFile(kFilename); | 72 raw_presenter.FeedNextFile(kFilename); |
| 77 raw_presenter.FeedNextBytes(block2, block2_size); | 73 raw_presenter.FeedNextBytes(block2, block2_size); |
| 78 EXPECT_TRUE(raw_presenter.Succeeded()); | 74 EXPECT_TRUE(raw_presenter.Succeeded()); |
| 79 scoped_ptr<Value> result = raw_presenter.Result(); | 75 scoped_ptr<base::Value> result = raw_presenter.Result(); |
| 80 ASSERT_TRUE(result.get() != NULL); | 76 ASSERT_TRUE(result.get() != NULL); |
| 81 | 77 |
| 82 EXPECT_TRUE(result->Equals(&expected_list)); | 78 EXPECT_TRUE(result->Equals(&expected_list)); |
| 83 } | 79 } |
| 84 | 80 |
| 85 } // namespace extensions | 81 } // namespace extensions |
| OLD | NEW |