Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(477)

Unified Diff: ios/web/public/test/response_providers/http_auth_response_provider.mm

Issue 2644223002: Integration tests for WebStateDelegate::OnAuthRequired callback. (Closed)
Patch Set: Addressed review comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/web/public/test/response_providers/http_auth_response_provider.mm
diff --git a/ios/web/public/test/response_providers/http_auth_response_provider.mm b/ios/web/public/test/response_providers/http_auth_response_provider.mm
new file mode 100644
index 0000000000000000000000000000000000000000..36ed46c7cf3f85703d8924aee6e7608f14afc03b
--- /dev/null
+++ b/ios/web/public/test/response_providers/http_auth_response_provider.mm
@@ -0,0 +1,53 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/web/public/test/response_providers/http_auth_response_provider.h"
+
+#include "base/base64.h"
+#include "base/strings/stringprintf.h"
+#include "net/http/http_request_headers.h"
+#include "net/http/http_response_headers.h"
+
+namespace web {
+
+HttpAuthResponseProvider::HttpAuthResponseProvider(const GURL& url,
+ const std::string& realm,
+ const std::string& username,
+ const std::string& password)
+ : url_(url), realm_(realm), username_(username), password_(password) {}
+
+HttpAuthResponseProvider::~HttpAuthResponseProvider() = default;
+
+bool HttpAuthResponseProvider::CanHandleRequest(const Request& request) {
+ return request.url == url_;
+}
+
+void HttpAuthResponseProvider::GetResponseHeadersAndBody(
+ const Request& request,
+ scoped_refptr<net::HttpResponseHeaders>* headers,
+ std::string* response_body) {
+ if (HeadersHaveValidCredentials(request.headers)) {
+ *response_body = page_text();
+ *headers = GetDefaultResponseHeaders();
+ } else {
+ *headers = GetResponseHeaders("", net::HTTP_UNAUTHORIZED);
+ (*headers)->AddHeader(base::StringPrintf(
+ "WWW-Authenticate: Basic realm=\"%s\"", realm_.c_str()));
+ }
+}
+
+bool HttpAuthResponseProvider::HeadersHaveValidCredentials(
+ const net::HttpRequestHeaders& headers) {
+ std::string header;
+ if (headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &header)) {
+ std::string auth =
+ base::StringPrintf("%s:%s", username_.c_str(), password_.c_str());
+ std::string encoded_auth;
+ base::Base64Encode(auth, &encoded_auth);
+ return header == base::StringPrintf("Basic %s", encoded_auth.c_str());
+ }
+ return false;
+}
+
+} // namespace web

Powered by Google App Engine
This is Rietveld 408576698