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