network_error_logging_test_util.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_
  5. #define NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "net/base/address_list.h"
  10. #include "net/base/ip_address.h"
  11. #include "net/network_error_logging/network_error_logging_service.h"
  12. #include "url/origin.h"
  13. namespace net {
  14. class IPAddress;
  15. // A NetworkErrorLoggingService implementation that stashes all NEL headers and
  16. // reports so that they can be easily verified in unit tests.
  17. class TestNetworkErrorLoggingService : public NetworkErrorLoggingService {
  18. public:
  19. struct Header {
  20. Header() = default;
  21. ~Header() = default;
  22. // Returns whether the |received_ip_address| field matches any of the
  23. // addresses in |address_list|.
  24. bool MatchesAddressList(const AddressList& address_list) const;
  25. NetworkIsolationKey network_isolation_key;
  26. url::Origin origin;
  27. IPAddress received_ip_address;
  28. std::string value;
  29. };
  30. TestNetworkErrorLoggingService();
  31. TestNetworkErrorLoggingService(const TestNetworkErrorLoggingService&) =
  32. delete;
  33. TestNetworkErrorLoggingService& operator=(
  34. const TestNetworkErrorLoggingService&) = delete;
  35. ~TestNetworkErrorLoggingService() override;
  36. const std::vector<Header>& headers() { return headers_; }
  37. const std::vector<RequestDetails>& errors() { return errors_; }
  38. // NetworkErrorLoggingService implementation
  39. void OnHeader(const NetworkIsolationKey& network_isolation_key,
  40. const url::Origin& origin,
  41. const IPAddress& received_ip_address,
  42. const std::string& value) override;
  43. void OnRequest(RequestDetails details) override;
  44. void QueueSignedExchangeReport(SignedExchangeReportDetails details) override;
  45. void RemoveBrowsingData(
  46. const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter)
  47. override;
  48. void RemoveAllBrowsingData() override;
  49. private:
  50. std::vector<Header> headers_;
  51. std::vector<RequestDetails> errors_;
  52. };
  53. } // namespace net
  54. #endif // NET_NETWORK_ERROR_LOGGING_NETWORK_ERROR_LOGGING_TEST_UTIL_H_