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 / 1.Program.cs
Created November 21, 2025 05:57
Using a Test/Mock Authentication Handler With Integration Tests
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddAuthenticationJwtBearer(s => s.SigningKey = bld.Configuration["Auth:JwtKey"])
.AddAuthorization()
.AddFastEndpoints();
var app = bld.Build();
app.UseAuthentication()
.UseAuthorization()
.UseFastEndpoints();
@dj-nitehawk
dj-nitehawk / Program.cs
Last active July 5, 2025 10:50
Content Negotiation Example
using System.Xml.Serialization;
using Microsoft.AspNetCore.Http.Metadata;
var bld = WebApplication.CreateBuilder();
bld.Services
.AddSingleton(typeof(IRequestBinder<>), typeof(NegotiatingBinder<>))
.AddFastEndpoints()
.SwaggerDocument(o => o.SerializerSettings = s => s.PropertyNamingPolicy = null);
var app = bld.Build();
@dj-nitehawk
dj-nitehawk / 1-xunit.runner.json
Last active May 13, 2025 11:01
Forwarding app log messages to xUnit message sink
{
"diagnosticMessages": true
}
@dj-nitehawk
dj-nitehawk / 1-Program.cs
Last active July 22, 2025 14:18
Update JWT Signing Key during runtime
var bld = WebApplication.CreateBuilder(args);
bld.Services
.Configure<JwtSigningOptions>(s => s.SigningKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") //must register signing options
.Configure<JwtCreationOptions>(c => c.SigningKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") //optional
.SwaggerDocument()
.AddAuthenticationJwtBearer(s => { }) //no need to specify signing options here due to above
.AddAuthorization()
.AddFastEndpoints();
var app = bld.Build();
@dj-nitehawk
dj-nitehawk / 1-Event.cs
Last active July 22, 2025 14:14
Integration testing an endpoint that publishes an event
sealed class MyEvent : IEvent
{
public string? Message { get; set; }
}
sealed class MyEventHandler : IEventHandler<MyEvent>
{
public Task HandleAsync(MyEvent e, CancellationToken c)
=> Task.CompletedTask;
}
@dj-nitehawk
dj-nitehawk / Program.cs
Last active July 22, 2025 13:41
Global response modifier
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints(
c => c.Endpoints.GlobalResponseModifier
= (ctx, content) =>
{
@dj-nitehawk
dj-nitehawk / Program.cs
Last active July 22, 2025 13:40
Response interceptor example
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen();
app.Run();
@dj-nitehawk
dj-nitehawk / Program.cs
Last active November 4, 2025 06:43
API Visualization with Scalar
using FastEndpoints.Swagger;
using Scalar.AspNetCore; //dotnet add package Scalar.AspNetCore
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument(); //define a swagger doc - v1 by default
var app = bld.Build();
app.UseFastEndpoints(); //must come before the UseOpenApi() call
@dj-nitehawk
dj-nitehawk / Endpoint.cs
Last active July 22, 2025 14:15
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 Send.OkAsync("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();