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