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