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 "components/sync_driver/sync_stopped_reporter.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/non_thread_safe.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 using browser_sync::SyncStoppedReporter
;
20 const char kTestURL
[] = "http://chromium.org/test";
21 const char kTestURLTrailingSlash
[] = "http://chromium.org/test/";
22 const char kEventURL
[] = "http://chromium.org/test/event";
24 const char kTestUserAgent
[] = "the_fifth_element";
25 const char kAuthToken
[] = "multipass";
26 const char kCacheGuid
[] = "leeloo";
27 const char kBirthday
[] = "2263";
29 const char kAuthHeaderPrefix
[] = "Bearer ";
31 class SyncStoppedReporterTest
: public testing::Test
{
33 SyncStoppedReporterTest() {}
34 ~SyncStoppedReporterTest() override
{}
36 void SetUp() override
{
37 request_context_
= new net::TestURLRequestContextGetter(
38 message_loop_
.task_runner());
41 void RequestFinishedCallback(const SyncStoppedReporter::Result
& result
) {
42 request_result_
= result
;
46 return GURL(kTestURL
);
49 std::string
user_agent() const {
50 return std::string(kTestUserAgent
);
53 SyncStoppedReporter::ResultCallback
callback() {
54 return base::Bind(&SyncStoppedReporterTest::RequestFinishedCallback
,
55 base::Unretained(this));
58 const SyncStoppedReporter::Result
& request_result() const {
59 return request_result_
;
62 net::URLRequestContextGetter
* request_context() {
63 return request_context_
.get();
67 base::MessageLoop message_loop_
;
68 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
69 SyncStoppedReporter::Result request_result_
;
71 DISALLOW_COPY_AND_ASSIGN(SyncStoppedReporterTest
);
74 // Test that the event URL gets constructed correctly.
75 TEST_F(SyncStoppedReporterTest
, EventURL
) {
76 net::TestURLFetcherFactory factory
;
77 SyncStoppedReporter
ssr(GURL(kTestURL
), user_agent(),
78 request_context(), callback());
79 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
80 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
81 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
84 // Test that the event URL gets constructed correctly with a trailing slash.
85 TEST_F(SyncStoppedReporterTest
, EventURLWithSlash
) {
86 net::TestURLFetcherFactory factory
;
87 SyncStoppedReporter
ssr(GURL(kTestURLTrailingSlash
), user_agent(),
88 request_context(), callback());
89 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
90 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
91 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
94 // Test that the URLFetcher gets configured correctly.
95 TEST_F(SyncStoppedReporterTest
, FetcherConfiguration
) {
96 net::TestURLFetcherFactory factory
;
97 SyncStoppedReporter
ssr(test_url(), user_agent(),
98 request_context(), callback());
99 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
100 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
102 // Ensure the headers are set correctly.
103 net::HttpRequestHeaders headers
;
105 fetcher
->GetExtraRequestHeaders(&headers
);
106 headers
.GetHeader(net::HttpRequestHeaders::kAuthorization
, &header
);
107 std::string
auth_header(kAuthHeaderPrefix
);
108 auth_header
.append(kAuthToken
);
109 EXPECT_EQ(auth_header
, header
);
110 headers
.GetHeader(net::HttpRequestHeaders::kUserAgent
, &header
);
111 EXPECT_EQ(user_agent(), header
);
113 sync_pb::EventRequest event_request
;
114 event_request
.ParseFromString(fetcher
->upload_data());
116 EXPECT_EQ(kCacheGuid
, event_request
.sync_disabled().cache_guid());
117 EXPECT_EQ(kBirthday
, event_request
.sync_disabled().store_birthday());
118 EXPECT_EQ(kEventURL
, fetcher
->GetOriginalURL().spec());
121 TEST_F(SyncStoppedReporterTest
, HappyCase
) {
122 net::TestURLFetcherFactory factory
;
123 SyncStoppedReporter
ssr(test_url(), user_agent(),
124 request_context(), callback());
125 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
126 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
127 fetcher
->set_response_code(net::HTTP_OK
);
128 ssr
.OnURLFetchComplete(fetcher
);
129 base::RunLoop run_loop
;
130 run_loop
.RunUntilIdle();
131 EXPECT_EQ(SyncStoppedReporter::RESULT_SUCCESS
, request_result());
134 TEST_F(SyncStoppedReporterTest
, ServerNotFound
) {
135 net::TestURLFetcherFactory factory
;
136 SyncStoppedReporter
ssr(test_url(), user_agent(),
137 request_context(), callback());
138 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
139 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
140 fetcher
->set_response_code(net::HTTP_NOT_FOUND
);
141 ssr
.OnURLFetchComplete(fetcher
);
142 base::RunLoop run_loop
;
143 run_loop
.RunUntilIdle();
144 EXPECT_EQ(SyncStoppedReporter::RESULT_ERROR
, request_result());
147 TEST_F(SyncStoppedReporterTest
, DestructionDuringRequestHandler
) {
148 net::TestURLFetcherFactory factory
;
149 factory
.set_remove_fetcher_on_delete(true);
151 SyncStoppedReporter
ssr(test_url(), user_agent(),
152 request_context(), callback());
153 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
154 EXPECT_FALSE(factory
.GetFetcherByID(0) == nullptr);
156 EXPECT_TRUE(factory
.GetFetcherByID(0) == nullptr);
159 TEST_F(SyncStoppedReporterTest
, Timeout
) {
160 SyncStoppedReporter
ssr(test_url(), user_agent(),
161 request_context(), callback());
163 // A task runner that can trigger the timeout immediately.
164 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner(
165 new base::TestSimpleTaskRunner());
166 ssr
.SetTimerTaskRunnerForTest(task_runner
);
169 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
171 // Trigger the timeout.
172 ASSERT_TRUE(task_runner
->HasPendingTask());
173 task_runner
->RunPendingTasks();
175 base::RunLoop run_loop
;
176 run_loop
.RunUntilIdle();
177 EXPECT_EQ(SyncStoppedReporter::RESULT_TIMEOUT
, request_result());
180 TEST_F(SyncStoppedReporterTest
, NoCallback
) {
181 net::TestURLFetcherFactory factory
;
182 SyncStoppedReporter
ssr(GURL(kTestURL
), user_agent(), request_context(),
183 SyncStoppedReporter::ResultCallback());
184 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
185 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(0);
186 fetcher
->set_response_code(net::HTTP_OK
);
187 ssr
.OnURLFetchComplete(fetcher
);
190 TEST_F(SyncStoppedReporterTest
, NoCallbackTimeout
) {
191 SyncStoppedReporter
ssr(GURL(kTestURL
), user_agent(), request_context(),
192 SyncStoppedReporter::ResultCallback());
194 // A task runner that can trigger the timeout immediately.
195 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner(
196 new base::TestSimpleTaskRunner());
197 ssr
.SetTimerTaskRunnerForTest(task_runner
);
200 ssr
.ReportSyncStopped(kAuthToken
, kCacheGuid
, kBirthday
);
202 // Trigger the timeout.
203 ASSERT_TRUE(task_runner
->HasPendingTask());
204 task_runner
->RunPendingTasks();