1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-11 15:17:44 +01:00
bitwarden-server/test/Icons.Test/Models/IconHttpResponseTests.cs
Matt Gibson 4377c7a897
Rewrite Icon fetching (#3023)
* Rewrite Icon fetching

* Move validation to IconUri, Uri, or UriBuilder

* `dotnet format` 🤖

* PR suggestions

* Add not null compiler hint

* Add twitter to test case

* Move Uri manipulation to UriService

* Implement MockedHttpClient

Presents better, fluent handling of message matching and response
building.

* Add redirect handling tests

* Add testing to models

* More aggressively dispose content in icon link

* Format 🤖

* Update icon lockfile

* Convert to cloned stream for HttpResponseBuilder

Content was being disposed when HttResponseMessage was being disposed.
This avoids losing our reference to our content and allows multiple
usages of the same `MockedHttpMessageResponse`

* Move services to extension

Extension is shared by testing and allows access to services from
our service tests

* Remove unused `using`

* Prefer awaiting asyncs for better exception handling

* `dotnet format` 🤖

* Await async

* Update tests to use test TLD and ip ranges

* Remove unused interfaces

* Make assignments static when possible

* Prefer invariant comparer to downcasing

* Prefer injecting interface services to implementations

* Prefer comparer set in HashSet initialization

* Allow SVG icons

* Filter out icons with unknown formats

* Seek to beginning of MemoryStream after writing it

* More appropriate to not return icon if it's invalid

* Add svg icon test
2023-08-08 19:29:40 +00:00

102 lines
3.3 KiB
C#

using System.Net;
using AngleSharp.Html.Parser;
using Bit.Icons.Models;
using Bit.Icons.Services;
using Bit.Test.Common.Helpers;
using Bit.Test.Common.MockedHttpClient;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using Xunit;
namespace Bit.Icons.Test.Models;
public class IconHttpResponseTests
{
private readonly IUriService _mockedUriService;
private static readonly IHtmlParser _parser = new HtmlParser();
public IconHttpResponseTests()
{
_mockedUriService = Substitute.For<IUriService>();
_mockedUriService.TryGetUri(Arg.Any<Uri>(), out Arg.Any<IconUri>()).Returns(x =>
{
x[1] = new IconUri(new Uri("https://icon.test"), IPAddress.Parse("192.0.2.1"));
return true;
});
}
[Fact]
public async Task RetrieveIconsAsync_Processes200LinksAsync()
{
var htmlBuilder = new HtmlBuilder();
var headBuilder = new HtmlBuilder("head");
for (var i = 0; i < 200; i++)
{
headBuilder.Append(UnusableLinkNode());
}
headBuilder.Append(UsableLinkNode());
htmlBuilder.Append(headBuilder);
var response = GetHttpResponseMessage(htmlBuilder.ToString());
var sut = CurriedIconHttpResponse()(response);
var result = await sut.RetrieveIconsAsync(new Uri("https://icon.test"), "icon.test", _parser);
Assert.Empty(result);
}
[Fact]
public async Task RetrieveIconsAsync_Processes10IconsAsync()
{
var htmlBuilder = new HtmlBuilder();
var headBuilder = new HtmlBuilder("head");
for (var i = 0; i < 11; i++)
{
headBuilder.Append(UsableLinkNode());
}
htmlBuilder.Append(headBuilder);
var response = GetHttpResponseMessage(htmlBuilder.ToString());
var sut = CurriedIconHttpResponse()(response);
var result = await sut.RetrieveIconsAsync(new Uri("https://icon.test"), "icon.test", _parser);
Assert.Equal(10, result.Count());
}
private static string UsableLinkNode()
{
return "<link rel=\"icon\" href=\"https://icon.test/favicon.ico\" />";
}
private static string UnusableLinkNode()
{
// Empty href links are not usable
return "<link rel=\"icon\" href=\"\" />";
}
private static HttpResponseMessage GetHttpResponseMessage(string content)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://icon.test"),
Content = new StringContent(content)
};
}
private Func<HttpResponseMessage, IconHttpResponse> CurriedIconHttpResponse()
{
return (HttpResponseMessage response) => new IconHttpResponse(response, NullLogger<IIconFetchingService>.Instance, UsableIconHttpClientFactory(), _mockedUriService);
}
private static IHttpClientFactory UsableIconHttpClientFactory()
{
var substitute = Substitute.For<IHttpClientFactory>();
var handler = new MockedHttpMessageHandler();
handler.Fallback
.WithStatusCode(HttpStatusCode.OK)
.WithContent("image/png", new byte[] { 137, 80, 78, 71 });
substitute.CreateClient("Icons").Returns(handler.ToHttpClient());
return substitute;
}
}