connect_job_test_util.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_SOCKET_CONNECT_JOB_TEST_UTIL_H_
  5. #define NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_
  6. #include <memory>
  7. #include "base/memory/ref_counted.h"
  8. #include "base/run_loop.h"
  9. #include "net/base/net_errors.h"
  10. #include "net/http/http_auth_controller.h"
  11. #include "net/http/http_response_info.h"
  12. #include "net/socket/connect_job.h"
  13. namespace net {
  14. class StreamSocket;
  15. class TestConnectJobDelegate : public ConnectJob::Delegate {
  16. public:
  17. // Whether a socket should be returned. In most cases, no socket is returned
  18. // on failure; however, on certain SSL errors, a socket is returned in the
  19. // case of error.
  20. enum class SocketExpected {
  21. ON_SUCCESS_ONLY,
  22. ALWAYS,
  23. };
  24. explicit TestConnectJobDelegate(
  25. SocketExpected socket_expected = SocketExpected::ON_SUCCESS_ONLY);
  26. TestConnectJobDelegate(const TestConnectJobDelegate&) = delete;
  27. TestConnectJobDelegate& operator=(const TestConnectJobDelegate&) = delete;
  28. ~TestConnectJobDelegate() override;
  29. // ConnectJob::Delegate implementation.
  30. void OnConnectJobComplete(int result, ConnectJob* job) override;
  31. void OnNeedsProxyAuth(const HttpResponseInfo& response,
  32. HttpAuthController* auth_controller,
  33. base::OnceClosure restart_with_auth_callback,
  34. ConnectJob* job) override;
  35. // Waits for the specified number of total auth challenges to be seen. Number
  36. // includes auth challenges that have already been waited for. Fails the test
  37. // if more auth challenges are seen than expected.
  38. void WaitForAuthChallenge(int num_auth_challenges_to_wait_for);
  39. void RunAuthCallback();
  40. // Waits for the ConnectJob to complete if it hasn't already and returns the
  41. // resulting network error code.
  42. int WaitForResult();
  43. int num_auth_challenges() const { return num_auth_challenges_; }
  44. const HttpResponseInfo& auth_response_info() const {
  45. return auth_response_info_;
  46. }
  47. scoped_refptr<HttpAuthController> auth_controller() {
  48. return auth_controller_.get();
  49. }
  50. // Returns true if the ConnectJob has a result.
  51. bool has_result() const { return has_result_; }
  52. void StartJobExpectingResult(ConnectJob* connect_job,
  53. net::Error expected_result,
  54. bool expect_sync_result);
  55. StreamSocket* socket() { return socket_.get(); }
  56. std::unique_ptr<StreamSocket> ReleaseSocket();
  57. private:
  58. const SocketExpected socket_expected_;
  59. bool has_result_ = false;
  60. int result_ = ERR_IO_PENDING;
  61. std::unique_ptr<StreamSocket> socket_;
  62. // These values are all updated each time a proxy auth challenge is seen.
  63. int num_auth_challenges_ = 0;
  64. HttpResponseInfo auth_response_info_;
  65. scoped_refptr<HttpAuthController> auth_controller_;
  66. base::OnceClosure restart_with_auth_callback_;
  67. base::RunLoop run_loop_;
  68. std::unique_ptr<base::RunLoop> auth_challenge_run_loop_;
  69. };
  70. } // namespace net
  71. #endif // NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_