From f41b6e5ec431c88bc1d94e4832d8ba49ddc42004 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 5 Mar 2024 11:19:52 +0100 Subject: HomeserverEmulator work --- Tests/LibMatrix.HomeserverEmulator/Program.cs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'Tests/LibMatrix.HomeserverEmulator/Program.cs') diff --git a/Tests/LibMatrix.HomeserverEmulator/Program.cs b/Tests/LibMatrix.HomeserverEmulator/Program.cs index 516d380..ddf39c7 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Program.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Program.cs @@ -1,16 +1,20 @@ using System.Net.Mime; +using System.Text.Json.Serialization; using LibMatrix; using LibMatrix.HomeserverEmulator.Services; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http.Timeouts; +using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddControllers().AddJsonOptions(options => { + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; +}); + builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo() { @@ -20,11 +24,12 @@ builder.Services.AddSwaggerGen(c => { }); c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "LibMatrix.HomeserverEmulator.xml")); }); + builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); - builder.Services.AddScoped(); builder.Services.AddRequestTimeouts(x => { @@ -45,7 +50,7 @@ builder.Services.AddRequestTimeouts(x => { builder.Services.AddCors(options => { options.AddPolicy( "Open", - policy => policy.AllowAnyOrigin().AllowAnyHeader()); + policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); }); var app = builder.Build(); @@ -62,6 +67,8 @@ app.UseExceptionHandler(exceptionHandlerApp => { var exceptionHandlerPathFeature = context.Features.Get(); + if(exceptionHandlerPathFeature?.Error is not null) + Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!); if (exceptionHandlerPathFeature?.Error is MatrixException mxe) { context.Response.StatusCode = mxe.ErrorCode switch { @@ -87,4 +94,14 @@ app.UseAuthorization(); app.MapControllers(); +app.Map("/_matrix/{*_}", (HttpContext ctx) => { + Console.WriteLine($"Client hit non-existing route: {ctx.Request.Method} {ctx.Request.Path}"); + ctx.Response.StatusCode = StatusCodes.Status404NotFound; + ctx.Response.ContentType = MediaTypeNames.Application.Json; + return ctx.Response.WriteAsJsonAsync(new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNRECOGNISED, + Error = "Endpoint not implemented" + }); +}); + app.Run(); -- cgit 1.4.1