Skip to content

Instantly share code, notes, and snippets.

@ntakouris
Last active July 23, 2019 07:59
Show Gist options
  • Save ntakouris/95efa7313f0912f31fa11e60fe0c341e to your computer and use it in GitHub Desktop.
Save ntakouris/95efa7313f0912f31fa11e60fe0c341e to your computer and use it in GitHub Desktop.
public class IntegrationTest
{
protected readonly HttpClient TestClient;
private readonly IServiceProvider _serviceProvider;
protected IntegrationTest()
{
var appFactory = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
services.RemoveAll(typeof(DataContext));
services.AddDbContext<DataContext>(options => { options.UseInMemoryDatabase("TestDb"); });
});
});
_serviceProvider = appFactory.Services;
TestClient = appFactory.CreateClient();
}
protected async Task AuthenticateAsync()
{
TestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", await GetJwtAsync());
}
protected async Task<PostResponse> CreatePostAsync(CreatePostRequest request)
{
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Posts.Create, request);
return await response.Content.ReadAsAsync<PostResponse>();
}
private async Task<string> GetJwtAsync()
{
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Identity.Register, new UserRegistrationRequest
{
Email = "[email protected]",
Password = "SomePass1234!"
});
var registrationResponse = await response.Content.ReadAsAsync<AuthSuccessResponse>();
return registrationResponse.Token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment