OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_auth_handler.h" | 5 #include "net/http/http_auth_handler.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
11 #include "net/http/http_auth_challenge_tokenizer.h" | 11 #include "net/http/http_auth_challenge_tokenizer.h" |
12 #include "net/http/http_auth_handler_mock.h" | 12 #include "net/http/http_auth_handler_mock.h" |
13 #include "net/http/http_request_info.h" | 13 #include "net/http/http_request_info.h" |
| 14 #include "net/http/http_response_info.h" |
14 #include "net/log/test_net_log.h" | 15 #include "net/log/test_net_log.h" |
15 #include "net/log/test_net_log_entry.h" | 16 #include "net/log/test_net_log_entry.h" |
16 #include "net/log/test_net_log_util.h" | 17 #include "net/log/test_net_log_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 TEST(HttpAuthHandlerTest, NetLog) { | 22 TEST(HttpAuthHandlerTest, NetLog) { |
22 GURL origin("http://www.example.com"); | 23 GURL origin("http://www.example.com"); |
23 std::string challenge = "Mock asdf"; | 24 std::string challenge = "Mock asdf"; |
24 AuthCredentials credentials(base::ASCIIToUTF16("user"), | 25 AuthCredentials credentials(base::ASCIIToUTF16("user"), |
25 base::ASCIIToUTF16("pass")); | 26 base::ASCIIToUTF16("pass")); |
26 std::string auth_token; | 27 std::string auth_token; |
27 HttpRequestInfo request; | 28 HttpRequestInfo request; |
28 | 29 |
29 for (int i = 0; i < 2; ++i) { | 30 for (int i = 0; i < 2; ++i) { |
30 bool async = (i == 0); | 31 bool generate_token_async = (i == 0); |
31 for (int j = 0; j < 2; ++j) { | 32 for (int j = 0; j < 2; ++j) { |
32 int rv = (j == 0) ? OK : ERR_UNEXPECTED; | 33 int generate_token_rv = (j == 0) ? OK : ERR_UNEXPECTED; |
33 for (int k = 0; k < 2; ++k) { | 34 for (int k = 0; k < 2; ++k) { |
34 TestCompletionCallback test_callback; | 35 TestCompletionCallback test_callback; |
35 HttpAuth::Target target = | 36 HttpAuth::Target target = |
36 (k == 0) ? HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; | 37 (k == 0) ? HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
37 NetLog::EventType event_type = | 38 NetLog::EventType event_type = |
38 (k == 0) ? NetLog::TYPE_AUTH_PROXY : NetLog::TYPE_AUTH_SERVER; | 39 (k == 0) ? NetLog::TYPE_AUTH_PROXY : NetLog::TYPE_AUTH_SERVER; |
39 HttpAuthChallengeTokenizer tokenizer( | 40 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), |
40 challenge.begin(), challenge.end()); | 41 challenge.end()); |
41 HttpAuthHandlerMock mock_handler; | 42 HttpAuthHandlerMock mock_handler; |
42 TestNetLog test_net_log; | 43 TestNetLog test_net_log; |
43 BoundNetLog bound_net_log( | 44 BoundNetLog bound_net_log( |
44 BoundNetLog::Make(&test_net_log, NetLog::SOURCE_NONE)); | 45 BoundNetLog::Make(&test_net_log, NetLog::SOURCE_NONE)); |
45 | 46 |
46 mock_handler.HandleInitialChallenge(tokenizer, target, origin, | 47 HttpResponseInfo response_info; |
47 bound_net_log); | 48 int rv = mock_handler.HandleInitialChallenge( |
48 mock_handler.SetGenerateExpectation(async, rv); | 49 tokenizer, response_info, target, origin, bound_net_log, |
| 50 test_callback.callback()); |
| 51 // TODO(asanka): Test async init behavior |
| 52 test_callback.GetResult(rv); |
| 53 mock_handler.SetGenerateExpectation(generate_token_async, |
| 54 generate_token_rv); |
49 mock_handler.GenerateAuthToken(&credentials, request, | 55 mock_handler.GenerateAuthToken(&credentials, request, |
50 test_callback.callback(), &auth_token); | 56 test_callback.callback(), &auth_token); |
51 if (async) | 57 if (generate_token_async) |
52 test_callback.WaitForResult(); | 58 test_callback.WaitForResult(); |
53 | 59 |
54 TestNetLogEntry::List entries; | 60 TestNetLogEntry::List entries; |
55 test_net_log.GetEntries(&entries); | 61 test_net_log.GetEntries(&entries); |
56 | 62 |
57 EXPECT_EQ(2u, entries.size()); | 63 EXPECT_EQ(2u, entries.size()); |
58 EXPECT_TRUE(LogContainsBeginEvent(entries, 0, event_type)); | 64 EXPECT_TRUE(LogContainsBeginEvent(entries, 0, event_type)); |
59 EXPECT_TRUE(LogContainsEndEvent(entries, 1, event_type)); | 65 EXPECT_TRUE(LogContainsEndEvent(entries, 1, event_type)); |
60 } | 66 } |
61 } | 67 } |
62 } | 68 } |
63 } | 69 } |
64 | 70 |
65 } // namespace net | 71 } // namespace net |
OLD | NEW |