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 #ifndef GOOGLE_APIS_GCM_ENGINE_GCM_REQUEST_TEST_BASE_H_ 6 #define GOOGLE_APIS_GCM_ENGINE_GCM_REQUEST_TEST_BASE_H_ 7 8 #include "base/macros.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/test/task_environment.h" 11 #include "base/threading/thread_task_runner_handle.h" 12 #include "net/base/backoff_entry.h" 13 #include "services/network/test/test_url_loader_factory.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace network { 17 class SharedURLLoaderFactory; 18 } 19 20 namespace gcm { 21 22 // The base class for testing various GCM requests that contains all the common 23 // logic to set up a task runner and complete a request with retries. 24 class GCMRequestTestBase : public testing::Test { 25 public: 26 GCMRequestTestBase(); 27 ~GCMRequestTestBase() override; 28 29 const net::BackoffEntry::Policy& GetBackoffPolicy() const; 30 31 // Called before fetch about to be completed. Can be overridden by the test 32 // class to add additional logic. 33 virtual void OnAboutToCompleteFetch(); 34 url_loader_factory()35 network::SharedURLLoaderFactory* url_loader_factory() const { 36 return shared_factory_.get(); 37 } 38 test_url_loader_factory()39 network::TestURLLoaderFactory* test_url_loader_factory() { 40 return &test_url_loader_factory_; 41 } 42 43 // This is a version for the TestURLLoaderFactory path; it also needs a URL. 44 void SetResponseForURLAndComplete(const std::string& url, 45 net::HttpStatusCode status_code, 46 const std::string& response_body, 47 int net_error_code = net::OK); 48 49 // Note: may return null if URL isn't pending. 50 const net::HttpRequestHeaders* GetExtraHeadersForURL(const std::string& url); 51 52 // Returns false if URL isn't pending or extraction failed. 53 bool GetUploadDataForURL(const std::string& url, std::string* data_out); 54 55 // See docs for VerifyFetcherUploadData. 56 void VerifyFetcherUploadDataForURL( 57 const std::string& url, 58 std::map<std::string, std::string>* expected_pairs); 59 60 private: 61 // Fast forward the timer used in the test to retry the request immediately. 62 void FastForwardToTriggerNextRetry(); 63 64 base::test::TaskEnvironment task_environment_{ 65 base::test::TaskEnvironment::TimeSource::MOCK_TIME}; 66 67 network::TestURLLoaderFactory test_url_loader_factory_; 68 scoped_refptr<network::SharedURLLoaderFactory> shared_factory_; 69 70 // Tracks the number of retries so far. 71 int retry_count_; 72 73 DISALLOW_COPY_AND_ASSIGN(GCMRequestTestBase); 74 }; 75 76 } // namespace gcm 77 78 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_REQUEST_TEST_BASE_H_ 79