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))
}
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))
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))
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))
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))
}
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";
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();
+ }
}
}
{
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);
}
}, 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);