OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "chrome/browser/safe_browsing/incident_report_uploader_impl.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/test/test_simple_task_runner.h" | |
10 #include "chrome/common/safe_browsing/csd.pb.h" | |
11 #include "net/base/load_flags.h" | |
12 #include "net/http/http_status_code.h" | |
13 #include "net/url_request/test_url_fetcher_factory.h" | |
14 #include "net/url_request/url_request_context_getter.h" | |
15 #include "net/url_request/url_request_status.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 class IncidentReportUploaderImplTest : public testing::Test { | |
19 public: | |
20 // safe_browsing::IncidentReportUploader::OnResultCallback implementation. | |
21 void OnReportUploadResult( | |
22 safe_browsing::IncidentReportUploader::Result result, | |
23 scoped_ptr<safe_browsing::ClientIncidentResponse> response) { | |
24 result_ = result; | |
25 response_ = response.Pass(); | |
26 } | |
27 | |
28 protected: | |
29 IncidentReportUploaderImplTest() | |
30 : task_runner_(new base::TestSimpleTaskRunner), | |
31 result_(safe_browsing::IncidentReportUploader::UPLOAD_REQUEST_FAILED) {} | |
32 | |
33 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
34 net::TestURLFetcherFactory url_fetcher_factory_; | |
35 safe_browsing::IncidentReportUploader::Result result_; | |
36 scoped_ptr<safe_browsing::ClientIncidentResponse> response_; | |
37 }; | |
38 | |
39 TEST_F(IncidentReportUploaderImplTest, Success) { | |
40 safe_browsing::ClientIncidentReport report; | |
41 scoped_ptr<safe_browsing::IncidentReportUploader> instance( | |
42 safe_browsing::IncidentReportUploaderImpl::UploadReport( | |
43 base::Bind(&IncidentReportUploaderImplTest::OnReportUploadResult, | |
44 base::Unretained(this)), | |
45 NULL, | |
46 report)); | |
47 | |
48 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( | |
49 safe_browsing::IncidentReportUploaderImpl::kTestUrlFetcherId); | |
50 ASSERT_NE(static_cast<net::TestURLFetcher*>(NULL), fetcher); | |
51 | |
52 safe_browsing::ClientIncidentReport uploaded_report; | |
53 | |
54 EXPECT_EQ(net::LOAD_DISABLE_CACHE, fetcher->GetLoadFlags()); | |
55 EXPECT_TRUE(uploaded_report.ParseFromString(fetcher->upload_data())); | |
56 | |
57 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
58 fetcher->set_response_code(net::HTTP_OK); | |
59 std::string response; | |
60 safe_browsing::ClientIncidentResponse().SerializeToString(&response); | |
61 fetcher->SetResponseString(response); | |
62 | |
63 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
64 | |
65 EXPECT_EQ(safe_browsing::IncidentReportUploader::UPLOAD_SUCCESS, result_); | |
66 EXPECT_TRUE(response_); | |
67 } | |
68 | |
69 // TODO(grt): | |
70 // bad status/response code | |
71 // confirm data in request is in upload test | |
72 // confirm data in response is parsed | |
OLD | NEW |