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

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

Issue 1342093002: Mandoline: let html_viewer handle more contents (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « mojo/fetcher/network_fetcher.cc ('k') | mojo/runner/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/at_exit.h"
6 #include "base/auto_reset.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "mojo/fetcher/network_fetcher.h"
14 #include "mojo/public/cpp/bindings/strong_binding.h"
15 #include "mojo/runner/context.h"
16 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
17 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace mojo {
21 namespace fetcher {
22 namespace {
23
24 const char k200Request[] = "http://request_expect_200";
25 const char k404Request[] = "http://request_expect_404";
26 const char k504Request[] = "http://request_expect_504";
27 const char kErrorRequest[] = "http://request_expect_error";
28
29 class TestURLLoaderImpl : public URLLoader {
30 public:
31 explicit TestURLLoaderImpl(InterfaceRequest<URLLoader> request)
32 : binding_(this, request.Pass()) {}
33 ~TestURLLoaderImpl() override {}
34
35 private:
36 // URLLoader implementation.
37 void Start(URLRequestPtr request,
38 const Callback<void(URLResponsePtr)>& callback) override {
39 URLResponsePtr response(URLResponse::New());
40 response->url = request->url;
41 if (request->url == std::string(k200Request)) {
42 response->mime_type = "text/html";
43 response->status_code = 200;
44 } else if (request->url == std::string(k404Request)) {
45 response->mime_type = "text/html";
46 response->status_code = 404;
47 } else if (request->url == std::string(k504Request)) {
48 response->mime_type = "text/html";
49 response->status_code = 504;
50 } else {
51 response->error = NetworkError::New();
52 response->error->code = -2;
53 }
54 callback.Run(response.Pass());
55 }
56 void FollowRedirect(const Callback<void(URLResponsePtr)>& callback) override {
57 NOTREACHED();
58 }
59 void QueryStatus(
60 const Callback<void(URLLoaderStatusPtr)>& callback) override {
61 NOTREACHED();
62 }
63
64 StrongBinding<URLLoader> binding_;
65 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderImpl);
66 };
67
68 class TestURLLoaderFactoryImpl : public URLLoaderFactory {
69 public:
70 explicit TestURLLoaderFactoryImpl(InterfaceRequest<URLLoaderFactory> request)
71 : binding_(this, request.Pass()) {}
72 ~TestURLLoaderFactoryImpl() override {}
73
74 private:
75 // URLLoaderFactory implementation.
76 void CreateURLLoader(InterfaceRequest<URLLoader> loader) override {
77 new TestURLLoaderImpl(loader.Pass());
78 }
79
80 StrongBinding<URLLoaderFactory> binding_;
81 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactoryImpl);
82 };
83
84 class FetchCallbackHelper {
85 public:
86 FetchCallbackHelper() : run_loop_(nullptr) {}
87 ~FetchCallbackHelper() {}
88
89 shell::Fetcher::FetchCallback GetCallback() {
90 return base::Bind(&FetchCallbackHelper::CallbackHandler,
91 base::Unretained(this));
92 }
93
94 void WaitForCallback() {
95 base::RunLoop run_loop;
96 base::AutoReset<base::RunLoop*> auto_reset(&run_loop_, &run_loop);
97 run_loop.Run();
98 }
99
100 shell::Fetcher* fetcher() const { return fetcher_.get(); }
101
102 private:
103 void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) {
104 fetcher_ = fetcher.Pass();
105 if (run_loop_)
106 run_loop_->Quit();
107 }
108
109 // If it is not null, it points to a stack-allocated base::RunLoop instance in
110 // WaitForCallback().
111 base::RunLoop* run_loop_;
112 scoped_ptr<shell::Fetcher> fetcher_;
113 DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper);
114 };
115
116 class NetworkFetcherTest : public testing::Test {
117 public:
118 NetworkFetcherTest() {}
119 ~NetworkFetcherTest() override {}
120
121 protected:
122 // Overridden from testing::Test:
123 void SetUp() override {
124 runner::Context::EnsureEmbedderIsInitialized();
125 // Automatically destroyed when |url_loader_factory_| is closed.
126 new TestURLLoaderFactoryImpl(GetProxy(&url_loader_factory_));
127 }
128
129 // When |expect_fetch_success| is false, |expected_status_code| is ignored.
130 void TestFetchURL(const std::string& url,
131 bool expect_fetch_success,
132 uint32_t expected_status_code) {
133 FetchCallbackHelper helper;
134
135 URLRequestPtr request(URLRequest::New());
136 request->url = url;
137 new NetworkFetcher(true, request.Pass(), url_loader_factory_.get(),
138 helper.GetCallback());
139 helper.WaitForCallback();
140
141 if (!expect_fetch_success) {
142 ASSERT_FALSE(helper.fetcher());
143 } else {
144 ASSERT_TRUE(helper.fetcher());
145 URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0);
146 ASSERT_TRUE(response);
147 EXPECT_EQ(url, response->url);
148 EXPECT_EQ(expected_status_code, response->status_code);
149 }
150 }
151
152 private:
153 base::ShadowingAtExitManager at_exit_;
154 base::MessageLoop loop_;
155 URLLoaderFactoryPtr url_loader_factory_;
156
157 DISALLOW_COPY_AND_ASSIGN(NetworkFetcherTest);
158 };
159
160 TEST_F(NetworkFetcherTest, FetchSucceeded200) {
161 TestFetchURL(k200Request, true, 200u);
162 }
163
164 TEST_F(NetworkFetcherTest, FetchSucceeded404) {
165 TestFetchURL(k404Request, true, 404u);
166 }
167
168 TEST_F(NetworkFetcherTest, FetchSucceeded504) {
169 TestFetchURL(k504Request, true, 504u);
170 }
171
172 TEST_F(NetworkFetcherTest, FetchFailed) {
173 TestFetchURL(kErrorRequest, false, 0u);
174 }
175
176 } // namespace
177 } // namespace fetcher
178 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/fetcher/network_fetcher.cc ('k') | mojo/runner/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698