-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set User Agent for outgoing HTTP requests
- Loading branch information
1 parent
ced3ce0
commit 2c3b3eb
Showing
5 changed files
with
98 additions
and
33 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Net; | ||
using ModShark.Services; | ||
using Moq; | ||
using Moq.Protected; | ||
|
||
namespace ModShark.Tests.Services; | ||
|
||
public class HttpServiceTests | ||
{ | ||
private HttpResponseMessage FakeResponse { get; set; } = null!; | ||
|
||
private HttpService ServiceUnderTest { get; set; } = null!; | ||
|
||
private HttpClient HttpClient { get; set; } = null!; | ||
private Mock<HttpMessageHandler> MockHttpMessageHandler { get; set; } = null!; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
FakeResponse = new HttpResponseMessage(HttpStatusCode.OK); | ||
|
||
MockHttpMessageHandler = new Mock<HttpMessageHandler>(); | ||
MockHttpMessageHandler | ||
.Protected() | ||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) | ||
.ReturnsAsync((HttpRequestMessage _, CancellationToken _) => FakeResponse) | ||
.Verifiable(); | ||
|
||
HttpClient = new HttpClient(MockHttpMessageHandler.Object); | ||
ServiceUnderTest = new HttpService(HttpClient); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
FakeResponse.Dispose(); | ||
HttpClient.Dispose(); | ||
} | ||
|
||
[Test] | ||
public async Task PostAsync_ShouldAttachDefaultUserAgent_WhenNotSet() | ||
{ | ||
const string expected = "ModShark (https://github.com/warriordog/ModShark)"; | ||
|
||
await ServiceUnderTest.PostAsync("https://example.com", CancellationToken.None); | ||
|
||
MockHttpMessageHandler | ||
.Protected() | ||
.Verify<Task<HttpResponseMessage>>("SendAsync", Times.Once(), ItExpr.Is<HttpRequestMessage>(m => string.Join(" ", m.Headers.GetValues("User-Agent")) == expected), ItExpr.IsAny<CancellationToken>()); | ||
} | ||
|
||
[Test] | ||
public async Task PostAsync_ShouldNotAttachUserAgent_WhenAlreadySet() | ||
{ | ||
const string expected = "Custom Agent"; | ||
var headers = new Dictionary<string, string> | ||
{ | ||
["User-Agent"] = expected | ||
}; | ||
|
||
await ServiceUnderTest.PostAsync("https://example.com", CancellationToken.None, headers: headers); | ||
|
||
MockHttpMessageHandler | ||
.Protected() | ||
.Verify<Task<HttpResponseMessage>>("SendAsync", Times.Once(), ItExpr.Is<HttpRequestMessage>(m => string.Join(" ", m.Headers.GetValues("User-Agent")) == expected), ItExpr.IsAny<CancellationToken>()); | ||
} | ||
} |
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