stub_url_checker.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 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. #ifndef COMPONENTS_SAFE_SEARCH_API_STUB_URL_CHECKER_H_
  5. #define COMPONENTS_SAFE_SEARCH_API_STUB_URL_CHECKER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/scoped_refptr.h"
  9. #include "net/base/net_errors.h"
  10. #include "services/network/test/test_url_loader_factory.h"
  11. namespace network {
  12. class SharedURLLoaderFactory;
  13. } // namespace network
  14. namespace safe_search_api {
  15. class URLChecker;
  16. // Helper class to stub out a URLLoaderFactory for use with URLChecker. This
  17. // lets tests control the response the URLChecker will receive from the Safe
  18. // Search API. Used to test both URLChecker itself and classes that use it.
  19. // This class builds a real URLChecker but maintains control over it to set up
  20. // fake responses.
  21. class StubURLChecker {
  22. public:
  23. StubURLChecker();
  24. StubURLChecker(const StubURLChecker&) = delete;
  25. StubURLChecker& operator=(const StubURLChecker&) = delete;
  26. ~StubURLChecker();
  27. // Returns a URLChecker that will use the stubbed-out responses. Can be called
  28. // before or after setting up the responses.
  29. std::unique_ptr<URLChecker> BuildURLChecker(size_t cache_size);
  30. // Sets the stub to return a successful response to all Safe Search API calls
  31. // from now on.
  32. void SetUpValidResponse(bool is_porn);
  33. // Sets the stub to respond to all Safe Search API calls with a failure from
  34. // now on.
  35. void SetUpFailedResponse();
  36. // Clears the stub so it won't return any response from now on.
  37. void ClearResponses();
  38. private:
  39. void SetUpResponse(net::Error error, const std::string& response);
  40. network::TestURLLoaderFactory test_url_loader_factory_;
  41. scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
  42. };
  43. } // namespace safe_search_api
  44. #endif // COMPONENTS_SAFE_SEARCH_API_STUB_URL_CHECKER_H_