http_auth_handler_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (c) 2012 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. #include "net/http/http_auth_handler.h"
  5. #include "base/strings/string_util.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/test/task_environment.h"
  8. #include "net/base/net_errors.h"
  9. #include "net/base/network_isolation_key.h"
  10. #include "net/base/test_completion_callback.h"
  11. #include "net/http/http_auth_challenge_tokenizer.h"
  12. #include "net/http/http_auth_handler_mock.h"
  13. #include "net/http/http_request_info.h"
  14. #include "net/log/net_log_event_type.h"
  15. #include "net/log/net_log_source_type.h"
  16. #include "net/log/test_net_log.h"
  17. #include "net/log/test_net_log_util.h"
  18. #include "net/ssl/ssl_info.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "url/gurl.h"
  21. #include "url/scheme_host_port.h"
  22. namespace net {
  23. TEST(HttpAuthHandlerTest, NetLog) {
  24. base::test::TaskEnvironment task_environment;
  25. url::SchemeHostPort scheme_host_port(GURL("http://www.example.com"));
  26. std::string challenge = "Mock asdf";
  27. AuthCredentials credentials(u"user", u"pass");
  28. std::string auth_token;
  29. HttpRequestInfo request;
  30. for (auto async : {true, false}) {
  31. for (auto target : {HttpAuth::AUTH_PROXY, HttpAuth::AUTH_SERVER}) {
  32. TestCompletionCallback test_callback;
  33. HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end());
  34. HttpAuthHandlerMock mock_handler;
  35. RecordingNetLogObserver net_log_observer;
  36. // set_connection_based(true) indicates that the HandleAnotherChallenge()
  37. // call after GenerateAuthToken() is expected and does not result in
  38. // AUTHORIZATION_RESULT_REJECT.
  39. mock_handler.set_connection_based(true);
  40. mock_handler.InitFromChallenge(
  41. &tokenizer, target, SSLInfo(), NetworkIsolationKey(),
  42. scheme_host_port, NetLogWithSource::Make(NetLogSourceType::NONE));
  43. mock_handler.SetGenerateExpectation(async, OK);
  44. mock_handler.GenerateAuthToken(&credentials, &request,
  45. test_callback.callback(), &auth_token);
  46. if (async)
  47. test_callback.WaitForResult();
  48. mock_handler.HandleAnotherChallenge(&tokenizer);
  49. auto entries = net_log_observer.GetEntries();
  50. ASSERT_EQ(5u, entries.size());
  51. EXPECT_TRUE(LogContainsBeginEvent(entries, 0,
  52. NetLogEventType::AUTH_HANDLER_INIT));
  53. EXPECT_TRUE(
  54. LogContainsEndEvent(entries, 1, NetLogEventType::AUTH_HANDLER_INIT));
  55. EXPECT_TRUE(LogContainsBeginEvent(entries, 2,
  56. NetLogEventType::AUTH_GENERATE_TOKEN));
  57. EXPECT_TRUE(LogContainsEndEvent(entries, 3,
  58. NetLogEventType::AUTH_GENERATE_TOKEN));
  59. EXPECT_TRUE(LogContainsEntryWithType(
  60. entries, 4, NetLogEventType::AUTH_HANDLE_CHALLENGE));
  61. }
  62. }
  63. }
  64. } // namespace net