mirror of
https://github.com/bitwarden/server.git
synced 2024-12-11 15:17:44 +01:00
4377c7a897
* 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
69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
|
|
namespace Bit.Test.Common.MockedHttpClient;
|
|
|
|
public class MockedHttpResponse : IMockedHttpResponse
|
|
{
|
|
private MockedHttpResponse _childResponse;
|
|
private readonly Func<HttpRequestMessage, HttpResponseBuilder, HttpResponseBuilder> _responder;
|
|
|
|
public int NumberOfResponses { get; private set; }
|
|
|
|
public MockedHttpResponse(HttpStatusCode statusCode)
|
|
{
|
|
_responder = (_, builder) => builder.WithStatusCode(statusCode);
|
|
}
|
|
|
|
private MockedHttpResponse(Func<HttpRequestMessage, HttpResponseBuilder, HttpResponseBuilder> responder)
|
|
{
|
|
_responder = responder;
|
|
}
|
|
|
|
public MockedHttpResponse WithStatusCode(HttpStatusCode statusCode)
|
|
{
|
|
return AddChild((_, builder) => builder.WithStatusCode(statusCode));
|
|
}
|
|
|
|
public MockedHttpResponse WithHeader(string name, string value)
|
|
{
|
|
return AddChild((_, builder) => builder.WithHeader(name, value));
|
|
}
|
|
public MockedHttpResponse WithHeaders(params KeyValuePair<string, string>[] headers)
|
|
{
|
|
return AddChild((_, builder) => headers.Aggregate(builder, (b, header) => b.WithHeader(header.Key, header.Value)));
|
|
}
|
|
|
|
public MockedHttpResponse WithContent(string mediaType, string content)
|
|
{
|
|
return WithContent(new StringContent(content, Encoding.UTF8, mediaType));
|
|
}
|
|
public MockedHttpResponse WithContent(string mediaType, byte[] content)
|
|
{
|
|
return WithContent(new ByteArrayContent(content) { Headers = { ContentType = new MediaTypeHeaderValue(mediaType) } });
|
|
}
|
|
public MockedHttpResponse WithContent(HttpContent content)
|
|
{
|
|
return AddChild((_, builder) => builder.WithContent(content));
|
|
}
|
|
|
|
public async Task<HttpResponseMessage> RespondToAsync(HttpRequestMessage request)
|
|
{
|
|
return await RespondToAsync(request, new HttpResponseBuilder());
|
|
}
|
|
|
|
private async Task<HttpResponseMessage> RespondToAsync(HttpRequestMessage request, HttpResponseBuilder currentBuilder)
|
|
{
|
|
NumberOfResponses++;
|
|
var nextBuilder = _responder(request, currentBuilder);
|
|
return await (_childResponse == null ? nextBuilder.ToHttpResponseAsync() : _childResponse.RespondToAsync(request, nextBuilder));
|
|
}
|
|
|
|
private MockedHttpResponse AddChild(Func<HttpRequestMessage, HttpResponseBuilder, HttpResponseBuilder> responder)
|
|
{
|
|
_childResponse = new MockedHttpResponse(responder);
|
|
return _childResponse;
|
|
}
|
|
}
|