From 5f4349f7f070b299deefc058a469453650b1199b Mon Sep 17 00:00:00 2001 From: Lakshmi Priya Sekar Date: Mon, 12 Feb 2018 18:35:19 -0800 Subject: [PATCH] Fix test Commit migrated from https://github.com/dotnet/corefx/commit/102e4d3c731e1765a21a3a5fd3e908c428ef4a88 --- .../Common/tests/System/Net/Http/LoopbackServer.cs | 50 +++++++++++++++++----- .../FunctionalTests/SocketsHttpHandlerTest.cs | 11 +++-- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs b/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs index d76ddd3..05f9f62 100644 --- a/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/LoopbackServer.cs @@ -171,10 +171,13 @@ namespace System.Net.Test.Common if (trimmedValue.Contains(nameof(username))) { // Username is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; username = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } // Username is mandatory. if (string.IsNullOrEmpty(username)) @@ -186,9 +189,12 @@ namespace System.Net.Test.Common } else if (trimmedValue.Contains(nameof(uri))) { - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; uri = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } // Request uri is mandatory. if (string.IsNullOrEmpty(uri)) @@ -197,9 +203,12 @@ namespace System.Net.Test.Common else if (trimmedValue.Contains(nameof(realm))) { // Realm is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; realm = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } // Realm is mandatory. if (string.IsNullOrEmpty(realm)) @@ -208,16 +217,22 @@ namespace System.Net.Test.Common else if (trimmedValue.Contains(nameof(cnonce))) { // CNonce is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; cnonce = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } } else if (trimmedValue.Contains(nameof(nonce))) { // Nonce is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; nonce = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } // Nonce is mandatory. if (string.IsNullOrEmpty(nonce)) @@ -226,9 +241,12 @@ namespace System.Net.Test.Common else if (trimmedValue.Contains(nameof(response))) { // response is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; response = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } // Response is mandatory. if (string.IsNullOrEmpty(response)) @@ -236,9 +254,12 @@ namespace System.Net.Test.Common } else if (trimmedValue.Contains(nameof(algorithm))) { - int startIndex = trimmedValue.IndexOf('=') + 1; + int startIndex = trimmedValue.IndexOf('='); if (startIndex != -1) + { + startIndex += 1; algorithm = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex).Trim(); + } if (string.IsNullOrEmpty(algorithm)) algorithm = "sha-256"; @@ -246,21 +267,30 @@ namespace System.Net.Test.Common else if (trimmedValue.Contains(nameof(opaque))) { // Opaque is a quoted string. - int startIndex = trimmedValue.IndexOf('"') + 1; + int startIndex = trimmedValue.IndexOf('"'); if (startIndex != -1) + { + startIndex += 1; opaque = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex - 1); + } } else if (trimmedValue.Contains(nameof(qop))) { - int startIndex = trimmedValue.IndexOf('=') + 1; + int startIndex = trimmedValue.IndexOf('='); if (startIndex != -1) + { + startIndex += 1; qop = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex).Trim(); + } } else if (trimmedValue.Contains(nameof(nc))) { - int startIndex = trimmedValue.IndexOf('=') + 1; + int startIndex = trimmedValue.IndexOf('='); if (startIndex != -1) + { + startIndex += 1; nc = trimmedValue.Substring(startIndex, trimmedValue.Length - startIndex).Trim(); + } } } diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.cs index 6f34044..3dfef3e 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/SocketsHttpHandlerTest.cs @@ -165,7 +165,9 @@ namespace System.Net.Http.Functional.Tests { string serverResponse = $"HTTP/1.1 401 UnAuthorized\r\nDate: {DateTimeOffset.UtcNow:R}\r\nWWW-Authenticate: {authenticateHeader}\r\nContent-Length: 0\r\n\r\n"; HttpClientHandler handler = CreateHttpClientHandler(); - Task serverTask = LoopbackServer.ReadRequestAndAuthenticateAsync(server, serverResponse, options); + Task serverTask = result ? + LoopbackServer.ReadRequestAndAuthenticateAsync(server, serverResponse, options) : + LoopbackServer.ReadRequestAndSendResponseAsync(server, serverResponse, options); await TestHelper.WhenAllCompletedOrAnyFailed(_createAndValidateRequest(handler, url, result ? HttpStatusCode.OK : HttpStatusCode.Unauthorized, _credentials), serverTask); }, options); } @@ -187,13 +189,14 @@ namespace System.Net.Http.Functional.Tests }, options); } - [Fact] - public async void HttpClientHandler_IncorrectCredentials_Fails() + [Theory] + [InlineData("HTTP/1.1 401 UnAuthorized\r\nWWW-Authenticate: Basic realm=\"hello\"\r\nContent-Length: 0\r\n\r\n")] + [InlineData("HTTP/1.1 401 UnAuthorized\r\nWWW-Authenticate: Digest realm=\"hello\", nonce=\"testnonce\"\r\nContent-Length: 0\r\n\r\n")] + public async void HttpClientHandler_IncorrectCredentials_Fails(string serverResponse) { var options = new LoopbackServer.Options { Domain = Domain, Username = Username, Password = Password }; await LoopbackServer.CreateServerAsync(async (server, url) => { - string serverResponse = "HTTP/1.1 401 UnAuthorized\r\nWWW-Authenticate: Basic realm\"hello\"\r\nContent-Length: 0\r\n\r\n"; HttpClientHandler handler = CreateHttpClientHandler(); Task serverTask = LoopbackServer.ReadRequestAndAuthenticateAsync(server, serverResponse, options); await TestHelper.WhenAllCompletedOrAnyFailed(_createAndValidateRequest(handler, url, HttpStatusCode.Unauthorized, new NetworkCredential("wronguser", "wrongpassword")), serverTask); -- 2.7.4