-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, failure to access http://example.com causes failure of the Networking Diagnostic portion of the diagnose command. To improve the experience for users who are unable to access http://example.com, this change: 1. Adds a fallback URI - if accessing http://example.com throws an exception, we now try http://httpforever.com. 2. Prints a warning when either the primary or both the primary and fallback uris throw an exception (instead of failing the Networking Diagnostic).
- Loading branch information
1 parent
0d70623
commit 5afa56b
Showing
3 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Security.AccessControl; | ||
using System.Text; | ||
using GitCredentialManager.Diagnostics; | ||
using GitCredentialManager.Tests.Objects; | ||
using Xunit; | ||
|
||
namespace Core.Tests.Commands; | ||
|
||
public class DiagnoseCommandTests | ||
{ | ||
[Fact] | ||
public void NetworkingDiagnostic_SendHttpRequest_Primary_OK() | ||
{ | ||
var primaryUriString = "http://example.com"; | ||
var sb = new StringBuilder(); | ||
var context = new TestCommandContext(); | ||
var networkingDiagnostic = new NetworkingDiagnostic(context); | ||
var primaryUri = new Uri(primaryUriString); | ||
var httpHandler = new TestHttpMessageHandler(); | ||
var httpResponse = new HttpResponseMessage(); | ||
var expected = $"Sending HEAD request to {primaryUriString}... OK{Environment.NewLine}"; | ||
|
||
httpHandler.Setup(HttpMethod.Head, primaryUri, httpResponse); | ||
|
||
networkingDiagnostic.SendHttpRequest(sb, new HttpClient(httpHandler)); | ||
|
||
httpHandler.AssertRequest(HttpMethod.Head, primaryUri, expectedNumberOfCalls: 1); | ||
Assert.Contains(expected, sb.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void NetworkingDiagnostic_SendHttpRequest_Backup_OK() | ||
{ | ||
var primaryUriString = "http://example.com"; | ||
var backupUriString = "http://httpforever.com"; | ||
var sb = new StringBuilder(); | ||
var context = new TestCommandContext(); | ||
var networkingDiagnostic = new NetworkingDiagnostic(context); | ||
var primaryUri = new Uri(primaryUriString); | ||
var backupUri = new Uri(backupUriString); | ||
var httpHandler = new TestHttpMessageHandler { SimulatePrimaryUriFailure = true }; | ||
var httpResponse = new HttpResponseMessage(); | ||
var expected = $"Sending HEAD request to {primaryUriString}... warning: HEAD request failed{Environment.NewLine}" + | ||
$"Sending HEAD request to {backupUriString}... OK{Environment.NewLine}"; | ||
|
||
httpHandler.Setup(HttpMethod.Head, primaryUri, httpResponse); | ||
httpHandler.Setup(HttpMethod.Head, backupUri, httpResponse); | ||
|
||
networkingDiagnostic.SendHttpRequest(sb, new HttpClient(httpHandler)); | ||
|
||
httpHandler.AssertRequest(HttpMethod.Head, primaryUri, expectedNumberOfCalls: 1); | ||
httpHandler.AssertRequest(HttpMethod.Head, backupUri, expectedNumberOfCalls: 1); | ||
Assert.Contains(expected, sb.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void NetworkingDiagnostic_SendHttpRequest_No_Network() | ||
{ | ||
var primaryUriString = "http://example.com"; | ||
var backupUriString = "http://httpforever.com"; | ||
var sb = new StringBuilder(); | ||
var context = new TestCommandContext(); | ||
var networkingDiagnostic = new NetworkingDiagnostic(context); | ||
var primaryUri = new Uri(primaryUriString); | ||
var backupUri = new Uri(backupUriString); | ||
var httpHandler = new TestHttpMessageHandler { SimulateNoNetwork = true }; | ||
var httpResponse = new HttpResponseMessage(); | ||
var expected = $"Sending HEAD request to {primaryUriString}... warning: HEAD request failed{Environment.NewLine}" + | ||
$"Sending HEAD request to {backupUriString}... warning: HEAD request failed{Environment.NewLine}"; | ||
|
||
httpHandler.Setup(HttpMethod.Head, primaryUri, httpResponse); | ||
httpHandler.Setup(HttpMethod.Head, backupUri, httpResponse); | ||
|
||
networkingDiagnostic.SendHttpRequest(sb, new HttpClient(httpHandler)); | ||
|
||
httpHandler.AssertRequest(HttpMethod.Head, primaryUri, expectedNumberOfCalls: 1); | ||
httpHandler.AssertRequest(HttpMethod.Head, backupUri, expectedNumberOfCalls: 1); | ||
Assert.Contains(expected, sb.ToString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters