mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[BEEEP] Integration tests (#1945)
* Add api integration tests * Add some stuff * Make program mockable * Work on IntegrationTests for Identity * Formatting * Update packages.lock.json * Update more packages.lock.json * Update all packages.lock.json * Fix InMemory configuration * Actually fix test configuration * Fix tests for CI * Fix event service * Force EF EventRepository * Add client_credentials test * Remove Api.IntegrationTest * Remove Api Program changes * Cleanup * Add more Auth-Email tests * Run formatting * Address some PR feedback * Move integration stuff to it's own common project * Ran linter * Add shared project to test solution * Remove sln changes * Clean usings * Add more coverage * Address PR feedback
This commit is contained in:
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace Bit.IntegrationTestCommon.Factories
|
||||
{
|
||||
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)
|
||||
{
|
||||
foreach (var header in content.Headers)
|
||||
{
|
||||
httpContext.Request.Headers.Add(header.Key, new StringValues(header.Value.ToArray()));
|
||||
}
|
||||
|
||||
httpContext.Request.Body = content.ReadAsStream();
|
||||
}
|
||||
|
||||
extraConfiguration?.Invoke(httpContext);
|
||||
});
|
||||
}
|
||||
public static Task<HttpContext> PostAsync(this TestServer server,
|
||||
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);
|
||||
public static async Task<string> ReadBodyAsStringAsync(this HttpContext context)
|
||||
{
|
||||
using var sr = new StreamReader(context.Response.Body);
|
||||
return await sr.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user