Skip to content

Instantly share code, notes, and snippets.

View dj-nitehawk's full-sized avatar

Dĵ ΝιΓΞΗΛψΚ dj-nitehawk

View GitHub Profile
@dj-nitehawk
dj-nitehawk / Endpoint.cs
Created August 5, 2024 04:28
Unit testing an endpoint that publishes an event
[HttpGet("publish"), AllowAnonymous]
sealed class MyEndpoint : EndpointWithoutRequest
{
public override async Task HandleAsync(CancellationToken c)
{
var evnt = new MyEvent { Message = "hello!" };
await PublishAsync(evnt);
await SendAsync("all good!");
}
}
@dj-nitehawk
dj-nitehawk / 1-Program.cs
Last active November 16, 2024 23:12
Response sending post-processor with ErrorOr package
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints(
c =>
{
c.Errors.UseProblemDetails();
@dj-nitehawk
dj-nitehawk / Program.cs
Created July 31, 2024 06:40
Using OneOf for polymorphic schema
using System.Text.Json.Serialization;
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument(o => o.UseOneOfForPolymorphism = true) //enable the setting
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen();
@dj-nitehawk
dj-nitehawk / Program.cs
Last active August 26, 2024 08:39
Sending custom response on JWT Bearer Auth failure
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddAuthenticationJwtBearer(
s => s.SigningKey = "...",
o =>
{
o.Events = new()
{
OnChallenge =
async ctx =>
@dj-nitehawk
dj-nitehawk / MyDbContext.cs
Last active June 6, 2024 09:02
Middleware setup for MS Identity API Endpoints
public class MyDbContext(DbContextOptions<MyDbContext> opts) : IdentityDbContext<IdentityUser>(opts);
@dj-nitehawk
dj-nitehawk / AppFixture.cs
Last active October 1, 2024 09:02
Integration testing with TestContainers & AppFixture
using Testcontainers.MongoDb;
public class Sut : AppFixture<Program>
{
const string Database = "TestingDB";
const string RootUsername = "root";
const string RootPassword = "password";
MongoDbContainer _container = null!;
@dj-nitehawk
dj-nitehawk / Index.html
Created March 24, 2024 02:07
Streaming JSON response with `IAsyncEnumerable`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Streaming Response Viewer</title>
<script>
async function streamResponse(url) {
const response = await fetch(url);
const reader = response.body.getReader();
@dj-nitehawk
dj-nitehawk / Program.cs
Last active September 20, 2024 06:37
Asymmetric JWT token usage example
bld.Services.AddAuthenticationJwtBearer(
s =>
{
s.SigningKey = "base64 encoded public key";
s.SigningStyle = TokenSigningStyle.Asymmetric;
s.KeyIsPemEncoded = true; // only if public key is in PEM format
},
b =>
{
b.TokenValidationParameters.ValidIssuer = "issuer";
@dj-nitehawk
dj-nitehawk / AuthenticationHandler.cs
Created February 24, 2024 06:45
Basic Authentication With Swagger Support
public class BasicAuth(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder)
: AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
internal const string SchemeName = "Basic";
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (IsPublicEndpoint() || !Request.Headers.ContainsKey("Authorization"))
@dj-nitehawk
dj-nitehawk / Program.cs
Last active October 1, 2024 09:01
Customizing Swagger Middleware & UI Paths
app.UseSwaggerGen(
s =>
{
//where to serve swagger.json files from
s.Path = "/PREFIX/swagger/{documentName}/swagger.json";
//api endpoint server base path customization
s.PostProcess = (document, request) =>
{
document.Servers.Clear();