Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / mojo / fetcher / data_fetcher_unittest.cc
blob568088e5df728f23a34ca363a29adc97a50259de
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.
5 #include "mojo/fetcher/data_fetcher.h"
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"
18 namespace mojo {
19 namespace fetcher {
20 namespace {
22 class FetchCallbackHelper {
23 public:
24 FetchCallbackHelper() : run_loop_(nullptr) {}
25 ~FetchCallbackHelper() {}
27 shell::Fetcher::FetchCallback GetCallback() {
28 return base::Bind(&FetchCallbackHelper::CallbackHandler,
29 base::Unretained(this));
32 void WaitForCallback() {
33 base::RunLoop run_loop;
34 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop);
35 run_loop.Run();
38 shell::Fetcher* fetcher() const { return fetcher_.get(); }
40 private:
41 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) {
42 fetcher_ = fetcher.Pass();
43 if (run_loop_)
44 run_loop_->Quit();
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);
54 class DataFetcherTest : public testing::Test {
55 public:
56 DataFetcherTest() {}
57 ~DataFetcherTest() override {}
59 protected:
60 // Overridden from testing::Test:
61 void SetUp() override { runner::Context::EnsureEmbedderIsInitialized(); }
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();
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);
77 if (expected_status_code != 200)
78 return;
80 ASSERT_TRUE(response->body.is_valid());
81 EXPECT_EQ(expected_mime_type, response->mime_type);
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);
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));
96 private:
97 base::MessageLoop loop_;
99 DISALLOW_COPY_AND_ASSIGN(DataFetcherTest);
102 TEST_F(DataFetcherTest, BasicSuccess) {
103 TestFetchURL("data:text/html,Hello world", 200, "text/html", "Hello world");
106 TEST_F(DataFetcherTest, BasicFailure) {
107 TestFetchURL("about:blank", 400, std::string(), std::string());
108 TestFetchURL("data:;base64,aGVs_-_-", 400, std::string(), std::string());
111 } // namespace
112 } // namespace fetcher
113 } // namespace mojo