Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 13, 2024
1 parent fae846a commit 4d272d9
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 33 deletions.
19 changes: 12 additions & 7 deletions Modules/ApiBrowsing/ApiBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ public static class ApiBrowser

#region Factories

public static BrowserHandlerBuilder SwaggerUI() => new("Swagger");
public static BrowserHandlerBuilder SwaggerUI() => new("Swagger", "Swagger UI");

public static BrowserHandlerBuilder Redoc() => new("Redoc");
public static BrowserHandlerBuilder Redoc() => new("Redoc", "Redoc");

#endregion

#region Layout extensions

public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segment = "swagger", string? url = null)
=> AddBrowser(layout, SwaggerUI(), segment, url);
public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segment = "swagger", string? url = null, string? title = null)
=> AddBrowser(layout, SwaggerUI(), segment, url, title);

public static LayoutBuilder AddRedoc(this LayoutBuilder layout, string segment = "redoc", string? url = null)
=> AddBrowser(layout, Redoc(), segment, url);
public static LayoutBuilder AddRedoc(this LayoutBuilder layout, string segment = "redoc", string? url = null, string? title = null)
=> AddBrowser(layout, Redoc(), segment, url, title);

private static LayoutBuilder AddBrowser(this LayoutBuilder layout, BrowserHandlerBuilder builder, string segment, string? url)
private static LayoutBuilder AddBrowser(this LayoutBuilder layout, BrowserHandlerBuilder builder, string segment, string? url, string? title)
{
if (url != null)
{
builder.Url(url);
}

if (title != null)
{
builder.Title(title);
}

return layout.Add(segment, builder);
}

Expand Down
9 changes: 5 additions & 4 deletions Modules/ApiBrowsing/Common/BrowserHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public sealed class BrowserHandler: IHandler

public TemplateRenderer Template { get; }

public string Url { get; }
public BrowserMetaData MetaData { get; }

#endregion

#region Initialization

public BrowserHandler(string resourceRoot, string? url)
public BrowserHandler(string resourceRoot, BrowserMetaData metaData)
{
StaticResources = Resources.From(ResourceTree.FromAssembly($"{resourceRoot}.Static"))
.Build();

Template = Renderer.From(Resource.FromAssembly($"{resourceRoot}.Index.html").Build());

Url = url ?? "../openapi.json";
MetaData = metaData;
}

#endregion
Expand All @@ -50,7 +50,8 @@ public BrowserHandler(string resourceRoot, string? url)
{
var config = new Dictionary<Value, Value>
{
["url"] = Url
["title"] = MetaData.Title,
["url"] = (MetaData.Url ?? "../openapi.json")
};

var content = await Template.RenderAsync(config);
Expand Down
17 changes: 15 additions & 2 deletions Modules/ApiBrowsing/Common/BrowserHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@

namespace GenHTTP.Modules.ApiBrowsing.Common;

public class BrowserHandlerBuilder(string resourceRoot) : IHandlerBuilder<BrowserHandlerBuilder>
public class BrowserHandlerBuilder(string resourceRoot, string title) : IHandlerBuilder<BrowserHandlerBuilder>
{
private readonly List<IConcernBuilder> _Concerns = [];

private string? _Url;

private string _Title = title;

public BrowserHandlerBuilder Url(string url)
{
_Url = url;
return this;
}

public BrowserHandlerBuilder Title(string title)
{
_Title = title;
return this;
}

public BrowserHandlerBuilder Add(IConcernBuilder concern)
{
_Concerns.Add(concern);
return this;
}

public IHandler Build() => Concerns.Chain(_Concerns, new BrowserHandler(resourceRoot, _Url));
public IHandler Build()
{
var meta = new BrowserMetaData(_Url, _Title);

return Concerns.Chain(_Concerns, new BrowserHandler(resourceRoot, meta));
}

}
3 changes: 3 additions & 0 deletions Modules/ApiBrowsing/Common/BrowserMetaData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace GenHTTP.Modules.ApiBrowsing.Common;

public record BrowserMetaData(string? Url, string Title);
2 changes: 1 addition & 1 deletion Modules/ApiBrowsing/Redoc/Index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Redoc</title>
<title>{title}</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="./static/roboto.css" rel="stylesheet">
Expand Down
3 changes: 1 addition & 2 deletions Modules/ApiBrowsing/Swagger/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<title>{title}</title>
<link rel="stylesheet" href="./static/swagger-ui.css" />
</head>
<body>
Expand Down
15 changes: 1 addition & 14 deletions Playground/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
using GenHTTP.Engine.Kestrel;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.OpenApi;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.ApiBrowsing;
using GenHTTP.Modules.Layouting;

var api = Inline.Create()
.Get(() => 42);

var app = Layout.Create()
.Add(api)
.AddOpenApi()
.AddSwaggerUI()
.AddRedoc();

// var app = Content.From(Resource.FromString("Hello World"));
var app = Content.From(Resource.FromString("Hello World"));

await Host.Create()
.Handler(app)
Expand Down
6 changes: 3 additions & 3 deletions Testing/Acceptance/GenHTTP.Testing.Acceptance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@
<ProjectReference Include="..\..\Engine\Shared\GenHTTP.Engine.Shared.csproj" />

<ProjectReference Include="..\..\Adapters\AspNetCore\GenHTTP.Adapters.AspNetCore.csproj" />

<ProjectReference Include="..\..\Modules\GenHTTP.Modules.I18n\GenHTTP.Modules.I18n.csproj" />

<ProjectReference Include="..\Testing\GenHTTP.Testing.csproj" />


<ProjectReference Include="..\..\Modules\ApiBrowsing\GenHTTP.Modules.ApiBrowsing.csproj" />
<ProjectReference Include="..\..\Modules\GenHTTP.Modules.I18n\GenHTTP.Modules.I18n.csproj" />
<ProjectReference Include="..\..\Modules\Authentication\GenHTTP.Modules.Authentication.csproj" />
<ProjectReference Include="..\..\Modules\Basics\GenHTTP.Modules.Basics.csproj" />
<ProjectReference Include="..\..\Modules\Caching\GenHTTP.Modules.Caching.csproj" />
Expand Down
50 changes: 50 additions & 0 deletions Testing/Acceptance/Modules/ApiBrowsing/ApplicationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Net;
using GenHTTP.Modules.ApiBrowsing;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.OpenApi;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.ApiBrowsing;

[TestClass]
public class ApplicationTests
{

[TestMethod]
[MultiEngineTest]
public async Task TestSwagger(TestEngine engine)
{
var app = Layout.Create()
.Add(Inline.Create().Get(() => 42))
.AddOpenApi()
.AddSwaggerUI();

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync("/swagger/");

await response.AssertStatusAsync(HttpStatusCode.OK);

AssertX.Contains("Swagger", await response.GetContentAsync());
}

[TestMethod]
[MultiEngineTest]
public async Task TestRedoc(TestEngine engine)
{
var app = Layout.Create()
.Add(Inline.Create().Get(() => 42))
.AddOpenApi()
.AddRedoc();

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync("/redoc/");

await response.AssertStatusAsync(HttpStatusCode.OK);

AssertX.Contains("Redoc", await response.GetContentAsync());
}

}
81 changes: 81 additions & 0 deletions Testing/Acceptance/Modules/ApiBrowsing/HandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Net;
using GenHTTP.Modules.ApiBrowsing;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Layouting.Provider;
using GenHTTP.Modules.OpenApi;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GenHTTP.Testing.Acceptance.Modules.ApiBrowsing;

[TestClass]
public class HandlerTests
{

[TestMethod]
[MultiEngineTest]
public async Task TestGetOnly(TestEngine engine)
{
await using var host = await TestHost.RunAsync(GetApi().AddSwaggerUI(), engine: engine);

var request = host.GetRequest("/swagger");

request.Method = HttpMethod.Post;
request.Content = new StringContent("Content");

using var response = await host.GetResponseAsync(request);

await response.AssertStatusAsync(HttpStatusCode.MethodNotAllowed);

Assert.AreEqual("GET", response.GetContentHeader("Allow"));
}

[TestMethod]
[MultiEngineTest]
public async Task TestResourceAccess(TestEngine engine)
{
await using var host = await TestHost.RunAsync(GetApi().AddSwaggerUI(), engine: engine);

using var response = await host.GetResponseAsync("/swagger/static/swagger-ui.css");

await response.AssertStatusAsync(HttpStatusCode.OK);
}

[TestMethod]
[MultiEngineTest]
public async Task TestNotFound(TestEngine engine)
{
await using var host = await TestHost.RunAsync(GetApi().AddSwaggerUI(), engine: engine);

using var response = await host.GetResponseAsync("/swagger/notfound");

await response.AssertStatusAsync(HttpStatusCode.NotFound);
}

[TestMethod]
[MultiEngineTest]
public async Task TestCustomMeta(TestEngine engine)
{
var api = GetApi().AddSwaggerUI("docs", "https://localhost:5001/swagger.json", "My API");

await using var host = await TestHost.RunAsync(api, engine: engine);

using var response = await host.GetResponseAsync("/docs/");

await response.AssertStatusAsync(HttpStatusCode.OK);

var content = await response.GetContentAsync();

AssertX.Contains("https://localhost:5001/swagger.json", content);
AssertX.Contains("My API", content);
}

private static LayoutBuilder GetApi()
{
return Layout.Create()
.Add(Inline.Create().Get(() => 42))
.AddOpenApi();
}

}

0 comments on commit 4d272d9

Please sign in to comment.