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 "base/at_exit.h"
6 #include "base/auto_reset.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"
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";
29 class TestURLLoaderImpl
: public URLLoader
{
31 explicit TestURLLoaderImpl(InterfaceRequest
<URLLoader
> request
)
32 : binding_(this, request
.Pass()) {}
33 ~TestURLLoaderImpl() override
{}
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;
51 response
->error
= NetworkError::New();
52 response
->error
->code
= -2;
54 callback
.Run(response
.Pass());
56 void FollowRedirect(const Callback
<void(URLResponsePtr
)>& callback
) override
{
60 const Callback
<void(URLLoaderStatusPtr
)>& callback
) override
{
64 StrongBinding
<URLLoader
> binding_
;
65 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderImpl
);
68 class TestURLLoaderFactoryImpl
: public URLLoaderFactory
{
70 explicit TestURLLoaderFactoryImpl(InterfaceRequest
<URLLoaderFactory
> request
)
71 : binding_(this, request
.Pass()) {}
72 ~TestURLLoaderFactoryImpl() override
{}
75 // URLLoaderFactory implementation.
76 void CreateURLLoader(InterfaceRequest
<URLLoader
> loader
) override
{
77 new TestURLLoaderImpl(loader
.Pass());
80 StrongBinding
<URLLoaderFactory
> binding_
;
81 DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactoryImpl
);
84 class FetchCallbackHelper
{
86 FetchCallbackHelper() : run_loop_(nullptr) {}
87 ~FetchCallbackHelper() {}
89 shell::Fetcher::FetchCallback
GetCallback() {
90 return base::Bind(&FetchCallbackHelper::CallbackHandler
,
91 base::Unretained(this));
94 void WaitForCallback() {
95 base::RunLoop run_loop
;
96 base::AutoReset
<base::RunLoop
*> auto_reset(&run_loop_
, &run_loop
);
100 shell::Fetcher
* fetcher() const { return fetcher_
.get(); }
103 void CallbackHandler(scoped_ptr
<shell::Fetcher
> fetcher
) {
104 fetcher_
= fetcher
.Pass();
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
);
116 class NetworkFetcherTest
: public testing::Test
{
118 NetworkFetcherTest() {}
119 ~NetworkFetcherTest() override
{}
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_
));
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
;
135 URLRequestPtr
request(URLRequest::New());
137 new NetworkFetcher(true, request
.Pass(), url_loader_factory_
.get(),
138 helper
.GetCallback());
139 helper
.WaitForCallback();
141 if (!expect_fetch_success
) {
142 ASSERT_FALSE(helper
.fetcher());
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
);
153 base::ShadowingAtExitManager at_exit_
;
154 base::MessageLoop loop_
;
155 URLLoaderFactoryPtr url_loader_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(NetworkFetcherTest
);
160 TEST_F(NetworkFetcherTest
, FetchSucceeded200
) {
161 TestFetchURL(k200Request
, true, 200u);
164 TEST_F(NetworkFetcherTest
, FetchSucceeded404
) {
165 TestFetchURL(k404Request
, true, 404u);
168 TEST_F(NetworkFetcherTest
, FetchSucceeded504
) {
169 TestFetchURL(k504Request
, true, 504u);
172 TEST_F(NetworkFetcherTest
, FetchFailed
) {
173 TestFetchURL(kErrorRequest
, false, 0u);
177 } // namespace fetcher