[email protected] | fba65f7b | 2012-03-15 05:22:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/http_auth_handler.h" |
| 6 | |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 7 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 9 | #include "net/base/capturing_net_log.h" |
| 10 | #include "net/base/net_errors.h" |
| 11 | #include "net/base/net_log_unittest.h" |
| 12 | #include "net/base/test_completion_callback.h" |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame^] | 13 | #include "net/http/http_auth_challenge_tokenizer.h" |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 14 | #include "net/http/http_auth_handler_mock.h" |
| 15 | #include "net/http/http_request_info.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | TEST(HttpAuthHandlerTest, NetLog) { |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 21 | GURL origin("http://www.example.com"); |
| 22 | std::string challenge = "Mock asdf"; |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 23 | AuthCredentials credentials(base::ASCIIToUTF16("user"), |
| 24 | base::ASCIIToUTF16("pass")); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 25 | std::string auth_token; |
| 26 | HttpRequestInfo request; |
| 27 | |
| 28 | for (int i = 0; i < 2; ++i) { |
| 29 | bool async = (i == 0); |
| 30 | for (int j = 0; j < 2; ++j) { |
| 31 | int rv = (j == 0) ? OK : ERR_UNEXPECTED; |
| 32 | for (int k = 0; k < 2; ++k) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 33 | TestCompletionCallback test_callback; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 34 | HttpAuth::Target target = |
| 35 | (k == 0) ? HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
| 36 | NetLog::EventType event_type = |
| 37 | (k == 0) ? NetLog::TYPE_AUTH_PROXY : NetLog::TYPE_AUTH_SERVER; |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame^] | 38 | HttpAuthChallengeTokenizer tokenizer( |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 39 | challenge.begin(), challenge.end()); |
| 40 | HttpAuthHandlerMock mock_handler; |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 41 | CapturingNetLog capturing_net_log; |
[email protected] | fba65f7b | 2012-03-15 05:22:13 | [diff] [blame] | 42 | BoundNetLog bound_net_log(BoundNetLog::Make(&capturing_net_log, |
| 43 | net::NetLog::SOURCE_NONE)); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 44 | |
| 45 | mock_handler.InitFromChallenge(&tokenizer, target, |
| 46 | origin, bound_net_log); |
| 47 | mock_handler.SetGenerateExpectation(async, rv); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 48 | mock_handler.GenerateAuthToken(&credentials, &request, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 49 | test_callback.callback(), &auth_token); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 50 | if (async) |
| 51 | test_callback.WaitForResult(); |
| 52 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 53 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 54 | capturing_net_log.GetEntries(&entries); |
| 55 | |
| 56 | EXPECT_EQ(2u, entries.size()); |
| 57 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, event_type)); |
| 58 | EXPECT_TRUE(LogContainsEndEvent(entries, 1, event_type)); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | } // namespace net |