Commit 1ecb4827 authored by Eugene But's avatar Eugene But Committed by Commit Bot

Modernize HttpAuthTest integration test.

- use EmbeddedTestServer instead of web::HttpServer
- do not assert inside test helpers
- inherit fixture from WebTestWithWebState
- replace deprecated base::test::ios::WaitUntilCondition
  with testing::WaitUntilConditionOrTimeout

Bug=None

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I3e7fbd67193f68fc5575fc529a7ac1f80d89126c
Reviewed-on: https://chromium-review.googlesource.com/1055777Reviewed-by: default avatarKurt Horimoto <kkhorimoto@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558366}
parent 7e6a1ff7
......@@ -2,15 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "base/strings/sys_string_conversions.h"
#include "base/test/ios/wait_util.h"
#import "ios/web/public/navigation_manager.h"
#import "ios/web/public/test/http_server/http_auth_response_provider.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "ios/web/public/test/http_server/http_server_util.h"
#import "ios/web/test/web_int_test.h"
#include "base/strings/utf_string_conversions.h"
#import "ios/testing/wait_util.h"
#import "ios/web/public/test/fakes/test_web_state_delegate.h"
#import "ios/web/public/test/js_test_util.h"
#import "ios/web/public/test/navigation_test_util.h"
#import "ios/web/public/test/web_test_with_web_state.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#import "testing/gtest_mac.h"
#include "url/gurl.h"
......@@ -18,109 +18,99 @@
#error "This file requires ARC support."
#endif
namespace web {
using testing::WaitUntilConditionOrTimeout;
using test::HttpServer;
namespace web {
// Test fixture for WebStateDelegate::OnAuthRequired integration tests.
class HttpAuthTest : public WebIntTest {
class HttpAuthTest : public WebTestWithWebState {
protected:
// Waits until WebStateDelegate::OnAuthRequired callback is called.
void WaitForOnAuthRequiredCallback() {
web_state_delegate_.ClearLastAuthenticationRequest();
base::test::ios::WaitUntilCondition(^bool {
return web_state_delegate_.last_authentication_request();
});
void SetUp() override {
WebTestWithWebState::SetUp();
web_state()->SetDelegate(&delegate_);
RegisterDefaultHandlers(&server_);
ASSERT_TRUE(server_.Start());
}
// Loads a page with URL and waits for OnAuthRequired callback.
void LoadUrlWithAuthChallenge(const GURL& url) {
NavigationManager::WebLoadParams params(url);
navigation_manager()->LoadURLWithParams(params);
WaitForOnAuthRequiredCallback();
}
// Authenticates and waits until the page finishes loading.
void Authenticate(NSString* username, NSString* password) {
ASSERT_TRUE(web_state()->IsLoading());
auto* auth_request = web_state_delegate_.last_authentication_request();
auth_request->auth_callback.Run(username, password);
base::test::ios::WaitUntilCondition(^bool {
return !web_state()->IsLoading();
// Waits until WebStateDelegate::OnAuthRequired callback is called.
bool WaitForOnAuthRequiredCallback() WARN_UNUSED_RESULT {
delegate_.ClearLastAuthenticationRequest();
return WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^bool {
return delegate_.last_authentication_request();
});
}
net::EmbeddedTestServer server_;
TestWebStateDelegate delegate_;
};
// Tests successful basic authentication.
TEST_F(HttpAuthTest, SuccessfullBasicAuth) {
// Load the page which requests basic HTTP authentication.
GURL url = HttpServer::MakeUrl("http://good-auth");
test::SetUpHttpServer(std::make_unique<HttpAuthResponseProvider>(
url, "GoodRealm", "gooduser", "goodpass"));
LoadUrlWithAuthChallenge(url);
GURL url = server_.GetURL("/auth-basic?password=goodpass&realm=Realm1");
test::LoadUrl(web_state(), url);
ASSERT_TRUE(WaitForOnAuthRequiredCallback());
// Verify that callback receives correct WebState.
auto* auth_request = web_state_delegate_.last_authentication_request();
auto* auth_request = delegate_.last_authentication_request();
EXPECT_EQ(web_state(), auth_request->web_state);
// Verify that callback receives correctly configured protection space.
NSURLProtectionSpace* protection_space = auth_request->protection_space;
EXPECT_NSEQ(@"GoodRealm", protection_space.realm);
EXPECT_NSEQ(@"Realm1", protection_space.realm);
EXPECT_FALSE(protection_space.receivesCredentialSecurely);
EXPECT_FALSE([protection_space isProxy]);
EXPECT_EQ(url.host(), base::SysNSStringToUTF8(protection_space.host));
EXPECT_EQ(
base::checked_cast<uint16_t>(HttpServer::GetSharedInstance().GetPort()),
base::checked_cast<uint16_t>(protection_space.port));
EXPECT_EQ(server_.port(),
base::checked_cast<uint16_t>(protection_space.port));
EXPECT_FALSE(protection_space.proxyType);
EXPECT_NSEQ(NSURLProtectionSpaceHTTP, protection_space.protocol);
EXPECT_NSEQ(NSURLAuthenticationMethodHTTPBasic,
protection_space.authenticationMethod);
// Make sure that authenticated page renders expected text.
Authenticate(@"gooduser", @"goodpass");
id document_body = ExecuteJavaScript(@"document.body.innerHTML");
EXPECT_EQ(HttpAuthResponseProvider::page_text(),
base::SysNSStringToUTF8(document_body));
ASSERT_TRUE(web_state()->IsLoading());
auth_request = delegate_.last_authentication_request();
ASSERT_TRUE(auth_request);
auth_request->auth_callback.Run(@"me", @"goodpass");
ASSERT_TRUE(WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{
return web_state()->GetTitle() == base::ASCIIToUTF16("me/goodpass");
}));
}
// Tests unsuccessful basic authentication.
TEST_F(HttpAuthTest, UnsucessfulBasicAuth) {
// Load the page which requests basic HTTP authentication.
GURL url = HttpServer::MakeUrl("http://bad-auth");
test::SetUpHttpServer(std::make_unique<HttpAuthResponseProvider>(
url, "BadRealm", "baduser", "badpass"));
LoadUrlWithAuthChallenge(url);
GURL url = server_.GetURL("/auth-basic?password=goodpass&realm=Realm2");
test::LoadUrl(web_state(), url);
ASSERT_TRUE(WaitForOnAuthRequiredCallback());
// Make sure that incorrect credentials request authentication again.
auto* auth_request = web_state_delegate_.last_authentication_request();
auth_request->auth_callback.Run(@"gooduser", @"goodpass");
WaitForOnAuthRequiredCallback();
auto* auth_request = delegate_.last_authentication_request();
auth_request->auth_callback.Run(@"me", @"badpass");
ASSERT_TRUE(WaitForOnAuthRequiredCallback());
// Verify that callback receives correct WebState.
auth_request = web_state_delegate_.last_authentication_request();
auth_request = delegate_.last_authentication_request();
EXPECT_EQ(web_state(), auth_request->web_state);
// Verify that callback receives correctly configured protection space.
NSURLProtectionSpace* protection_space = auth_request->protection_space;
EXPECT_NSEQ(@"BadRealm", protection_space.realm);
EXPECT_NSEQ(@"Realm2", protection_space.realm);
EXPECT_FALSE(protection_space.receivesCredentialSecurely);
EXPECT_FALSE([protection_space isProxy]);
EXPECT_EQ(url.host(), base::SysNSStringToUTF8(protection_space.host));
EXPECT_EQ(
base::checked_cast<uint16_t>(HttpServer::GetSharedInstance().GetPort()),
base::checked_cast<uint16_t>(protection_space.port));
EXPECT_EQ(server_.port(),
base::checked_cast<uint16_t>(protection_space.port));
EXPECT_FALSE(protection_space.proxyType);
EXPECT_NSEQ(NSURLProtectionSpaceHTTP, protection_space.protocol);
EXPECT_NSEQ(NSURLAuthenticationMethodHTTPBasic,
protection_space.authenticationMethod);
// Cancel authentication and make sure that the page is blank.
auth_request->auth_callback.Run(nil, nil);
base::test::ios::WaitUntilCondition(^bool {
return !web_state()->IsLoading();
});
EXPECT_FALSE(ExecuteJavaScript(@"window.document"));
// Cancel authentication and make sure that authentication is denied.
auth_request->auth_callback.Run(/*username=*/nil, /*password=*/nil);
ASSERT_TRUE(WaitUntilConditionOrTimeout(testing::kWaitForPageLoadTimeout, ^{
return web_state()->GetTitle() ==
base::ASCIIToUTF16("Denied: Missing Authorization Header");
}));
}
} // web
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment