OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 #include "base/memory/ptr_util.h" | |
6 #include "base/strings/sys_string_conversions.h" | |
7 #include "base/test/ios/wait_util.h" | |
8 #import "ios/web/public/navigation_manager.h" | |
9 #import "ios/web/public/test/http_server.h" | |
10 #include "ios/web/public/test/http_server_util.h" | |
11 #import "ios/web/public/test/response_providers/http_auth_response_provider.h" | |
12 #import "ios/web/test/web_int_test.h" | |
13 #import "testing/gtest_mac.h" | |
14 #include "url/gurl.h" | |
15 | |
16 namespace web { | |
17 | |
18 using test::HttpServer; | |
19 | |
20 // Test fixture for WebStateDelegate::OnAuthRequired integration tests. | |
21 class HttpAuthTest : public WebIntTest { | |
22 protected: | |
23 // Waits until WebStateDelegate::OnAuthRequired callback is called. | |
24 void WaitForOnAuthRequiredCallback() { | |
25 web_state_delegate_.ClearLastAuthenticationRequest(); | |
michaeldo
2017/01/20 23:48:24
Should ClearLastAuthenticationRequest() be called
Eugene But (OOO till 7-30)
2017/01/21 01:42:10
Moving this code outside will change the behavior
| |
26 base::test::ios::WaitUntilCondition(^bool { | |
27 return web_state_delegate_.last_authentication_request(); | |
28 }); | |
29 } | |
30 | |
31 // Loads a page with URL and waits for OnAuthRequired callback. | |
32 void LoadUrlWithAuthChallenge(const GURL& url) { | |
33 NavigationManager::WebLoadParams params(url); | |
34 navigation_manager()->LoadURLWithParams(params); | |
35 WaitForOnAuthRequiredCallback(); | |
36 } | |
37 | |
38 // Authenticates and waits until the page finishes loading. | |
39 void Authenticate(NSString* username, NSString* password) { | |
40 ASSERT_TRUE(web_state()->IsLoading()); | |
41 auto auth_request = web_state_delegate_.last_authentication_request(); | |
42 auth_request->auth_callback.Run(username, password); | |
43 base::test::ios::WaitUntilCondition(^bool { | |
44 return !web_state()->IsLoading(); | |
45 }); | |
46 } | |
47 }; | |
48 | |
49 // Tests successful basic authentication. | |
50 TEST_F(HttpAuthTest, SuccessfullBasicAuth) { | |
51 // Load the page which requests basic HTTP authentication. | |
52 GURL url = HttpServer::MakeUrl("http://good-auth"); | |
53 test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>( | |
54 url, "GoodRealm", "gooduser", "goodpass")); | |
55 LoadUrlWithAuthChallenge(url); | |
56 | |
57 // Verify that callback receives correct WebState. | |
58 auto auth_request = web_state_delegate_.last_authentication_request(); | |
59 EXPECT_EQ(web_state(), auth_request->web_state); | |
60 | |
61 // Verify that callback receives correctly configured protection space. | |
62 NSURLProtectionSpace* protection_space = auth_request->protection_space; | |
63 EXPECT_NSEQ(@"GoodRealm", protection_space.realm); | |
64 EXPECT_FALSE(protection_space.receivesCredentialSecurely); | |
65 EXPECT_FALSE([protection_space isProxy]); | |
66 EXPECT_EQ(url.host(), base::SysNSStringToUTF8(protection_space.host)); | |
67 EXPECT_EQ( | |
68 base::checked_cast<uint16_t>(HttpServer::GetSharedInstance().GetPort()), | |
69 base::checked_cast<uint16_t>(protection_space.port)); | |
70 EXPECT_FALSE(protection_space.proxyType); | |
71 EXPECT_NSEQ(NSURLProtectionSpaceHTTP, protection_space.protocol); | |
72 EXPECT_NSEQ(NSURLAuthenticationMethodHTTPBasic, | |
73 protection_space.authenticationMethod); | |
74 | |
75 // Make sure that authenticated page renders expected text. | |
76 Authenticate(@"gooduser", @"goodpass"); | |
77 id document_body = ExecuteJavaScript(@"document.body.innerHTML"); | |
78 EXPECT_EQ(HttpAuthResponseProvider::page_text(), | |
79 base::SysNSStringToUTF8(document_body)); | |
80 } | |
81 | |
82 // Tests unsuccessful basic authentication. | |
83 TEST_F(HttpAuthTest, UnsucessfulBasicAuth) { | |
84 // Load the page which requests basic HTTP authentication. | |
85 GURL url = HttpServer::MakeUrl("http://bad-auth"); | |
86 test::SetUpHttpServer(base::MakeUnique<HttpAuthResponseProvider>( | |
87 url, "BadRealm", "baduser", "badpass")); | |
88 LoadUrlWithAuthChallenge(url); | |
89 | |
90 // Make sure that incorrect credentials request authentication again. | |
91 auto auth_request = web_state_delegate_.last_authentication_request(); | |
92 auth_request->auth_callback.Run(@"gooduser", @"goodpass"); | |
93 WaitForOnAuthRequiredCallback(); | |
94 | |
95 // Verify that callback receives correct WebState. | |
96 auth_request = web_state_delegate_.last_authentication_request(); | |
97 EXPECT_EQ(web_state(), auth_request->web_state); | |
98 | |
99 // Verify that callback receives correctly configured protection space. | |
100 NSURLProtectionSpace* protection_space = auth_request->protection_space; | |
101 EXPECT_NSEQ(@"BadRealm", protection_space.realm); | |
102 EXPECT_FALSE(protection_space.receivesCredentialSecurely); | |
103 EXPECT_FALSE([protection_space isProxy]); | |
104 EXPECT_EQ(url.host(), base::SysNSStringToUTF8(protection_space.host)); | |
105 EXPECT_EQ( | |
106 base::checked_cast<uint16_t>(HttpServer::GetSharedInstance().GetPort()), | |
107 base::checked_cast<uint16_t>(protection_space.port)); | |
108 EXPECT_FALSE(protection_space.proxyType); | |
109 EXPECT_NSEQ(NSURLProtectionSpaceHTTP, protection_space.protocol); | |
110 EXPECT_NSEQ(NSURLAuthenticationMethodHTTPBasic, | |
111 protection_space.authenticationMethod); | |
112 | |
113 // Cancel authentication and make sure that the page is blank. | |
114 auth_request->auth_callback.Run(nil, nil); | |
115 base::test::ios::WaitUntilCondition(^bool { | |
116 return !web_state()->IsLoading(); | |
117 }); | |
118 EXPECT_FALSE(ExecuteJavaScript(@"window.document")); | |
119 } | |
120 | |
121 } // web | |
OLD | NEW |