2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Net;
|
2022-06-24 16:39:34 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
|
|
|
|
|
namespace Bit.IntegrationTestCommon.Factories;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public static class WebApplicationFactoryExtensions
|
|
|
|
|
{
|
|
|
|
|
private static async Task<HttpContext> SendAsync(this TestServer server,
|
|
|
|
|
HttpMethod method,
|
|
|
|
|
string requestUri,
|
|
|
|
|
HttpContent content = null,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
{
|
|
|
|
|
return await server.SendAsync(httpContext =>
|
|
|
|
|
{
|
|
|
|
|
// Automatically set the whitelisted IP so normal tests do not run into rate limit issues
|
|
|
|
|
// to test rate limiter, use the extraConfiguration parameter to set Connection.RemoteIpAddress
|
|
|
|
|
// it runs after this so it will take precedence.
|
|
|
|
|
httpContext.Connection.RemoteIpAddress = IPAddress.Parse(FactoryConstants.WhitelistedIp);
|
|
|
|
|
|
|
|
|
|
httpContext.Request.Path = new PathString(requestUri);
|
|
|
|
|
httpContext.Request.Method = method.Method;
|
|
|
|
|
|
|
|
|
|
if (content != null)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
foreach (var header in content.Headers)
|
|
|
|
|
{
|
2024-02-05 19:03:42 +01:00
|
|
|
|
httpContext.Request.Headers.Append(header.Key, new StringValues(header.Value.ToArray()));
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
|
|
|
|
httpContext.Request.Body = content.ReadAsStream();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
|
|
|
|
extraConfiguration?.Invoke(httpContext);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public static Task<HttpContext> PostAsync(this TestServer server,
|
2022-06-24 16:39:34 +02:00
|
|
|
|
string requestUri,
|
|
|
|
|
HttpContent content,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
=> SendAsync(server, HttpMethod.Post, requestUri, content, extraConfiguration);
|
|
|
|
|
public static Task<HttpContext> GetAsync(this TestServer server,
|
|
|
|
|
string requestUri,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
=> SendAsync(server, HttpMethod.Get, requestUri, content: null, extraConfiguration);
|
2022-09-08 10:00:59 +02:00
|
|
|
|
public static Task<HttpContext> PutAsync(this TestServer server,
|
|
|
|
|
string requestUri,
|
|
|
|
|
HttpContent content,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
=> SendAsync(server, HttpMethod.Put, requestUri, content, extraConfiguration);
|
|
|
|
|
public static Task<HttpContext> PatchAsync(this TestServer server,
|
|
|
|
|
string requestUri,
|
|
|
|
|
HttpContent content,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
=> SendAsync(server, HttpMethod.Patch, requestUri, content, extraConfiguration);
|
|
|
|
|
public static Task<HttpContext> DeleteAsync(this TestServer server,
|
|
|
|
|
string requestUri,
|
|
|
|
|
HttpContent content,
|
|
|
|
|
Action<HttpContext> extraConfiguration = null)
|
|
|
|
|
=> SendAsync(server, HttpMethod.Delete, requestUri, content: content, extraConfiguration);
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public static HttpContext SetAuthEmail(this HttpContext context, string username)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2024-02-05 19:03:42 +01:00
|
|
|
|
context.Request.Headers.Append("Auth-Email", CoreHelpers.Base64UrlEncodeString(username));
|
2022-06-24 16:39:34 +02:00
|
|
|
|
return context;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 16:39:34 +02:00
|
|
|
|
public static HttpContext SetIp(this HttpContext context, string ip)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-24 16:39:34 +02:00
|
|
|
|
context.Connection.RemoteIpAddress = IPAddress.Parse(ip);
|
|
|
|
|
return context;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public static async Task<string> ReadBodyAsStringAsync(this HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
using var sr = new StreamReader(context.Response.Body);
|
|
|
|
|
return await sr.ReadToEndAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|