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

Side by Side Diff: mojo/fetcher/data_fetcher_unittest.cc

Issue 1356623002: mojo: Add DataURLFetcher for processing data: urls. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: lolgyp Created 5 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 2015 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 "mojo/fetcher/data_fetcher.h"
6
7 #include "base/auto_reset.h"
8 #include "base/bind.h"
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "mojo/runner/context.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
17
18 namespace mojo {
19 namespace fetcher {
20 namespace {
21
22 class FetchCallbackHelper {
23 public:
24 FetchCallbackHelper() : run_loop_(nullptr) {}
25 ~FetchCallbackHelper() {}
26
27 shell::Fetcher::FetchCallback GetCallback() {
28 return base::Bind(&FetchCallbackHelper::CallbackHandler,
29 base::Unretained(this));
30 }
31
32 void WaitForCallback() {
33 base::RunLoop run_loop;
34 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop);
35 run_loop.Run();
36 }
37
38 shell::Fetcher* fetcher() const { return fetcher_.get(); }
39
40 private:
41 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) {
42 fetcher_ = fetcher.Pass();
43 if (run_loop_)
44 run_loop_->Quit();
45 }
46
47 // If it is not null, it points to a stack-allocated base::RunLoop instance in
48 // WaitForCallback().
49 base::RunLoop* run_loop_;
50 scoped_ptr<shell::Fetcher> fetcher_;
51 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper);
52 };
53
54 class DataFetcherTest : public testing::Test {
55 public:
56 DataFetcherTest() {}
57 ~DataFetcherTest() override {}
58
59 protected:
60 // Overridden from testing::Test:
61 void SetUp() override { runner::Context::EnsureEmbedderIsInitialized(); }
62
63 void TestFetchURL(const std::string& url,
64 uint32_t expected_status_code,
65 const std::string& expected_mime_type,
66 const std::string& expected_body) {
67 FetchCallbackHelper helper;
68 DataFetcher::Start(GURL(url), helper.GetCallback());
69 helper.WaitForCallback();
70
71 ASSERT_TRUE(helper.fetcher());
72 URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0);
73 ASSERT_TRUE(response);
74 EXPECT_EQ(url, response->url);
75 EXPECT_EQ(expected_status_code, response->status_code);
76
77 if (expected_status_code != 200)
78 return;
79
80 ASSERT_TRUE(response->body.is_valid());
81 EXPECT_EQ(expected_mime_type, response->mime_type);
82
83 uint32_t num_bytes = 0;
84 Handle body_handle = response->body.release();
85 MojoResult result = MojoReadData(body_handle.value(), nullptr, &num_bytes,
86 MOJO_READ_DATA_FLAG_QUERY);
87 ASSERT_EQ(MOJO_RESULT_OK, result);
88
89 scoped_ptr<char[]> body(new char[num_bytes]);
90 result = MojoReadData(body_handle.value(), body.get(), &num_bytes,
91 MOJO_READ_DATA_FLAG_ALL_OR_NONE);
92 ASSERT_EQ(MOJO_RESULT_OK, result);
93 EXPECT_EQ(expected_body, std::string(body.get(), num_bytes));
94 }
95
96 private:
97 base::MessageLoop loop_;
98
99 DISALLOW_COPY_AND_ASSIGN(DataFetcherTest);
100 };
101
102 TEST_F(DataFetcherTest, BasicSuccess) {
103 TestFetchURL("data:text/html,Hello world", 200, "text/html", "Hello world");
104 }
105
106 TEST_F(DataFetcherTest, BasicFailure) {
107 TestFetchURL("about:blank", 400, std::string(), std::string());
108 TestFetchURL("data:;base64,aGVs_-_-", 400, std::string(), std::string());
109 }
110
111 } // namespace
112 } // namespace fetcher
113 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698