Skip to content

Commit

Permalink
Add test project to minimal webservice template
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Jan 4, 2024
1 parent 2dd3629 commit 74dddd9
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<TargetFramework>net8.0</TargetFramework>

<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<IsPackable>false</IsPackable>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />

<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />

<PackageReference Include="GenHTTP.Testing" Version="8.1.0" />

<ProjectReference Include="..\$safeprojectname$\$safeprojectname$.csproj" />

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using GenHTTP.Testing;

namespace $safeprojectname$.Tests
{

[TestClass]
public class BookServiceTests
{

[TestMethod]
public async Task TestGetBooks()
{
using var runner = TestHost.Run(Project.Setup());

using var response = await runner.GetResponseAsync("/books/");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

<ItemGroup>

<PackageReference Include="GenHTTP.Core" Version="8.0.0" />
<PackageReference Include="GenHTTP.Core" Version="8.1.0" />

<PackageReference Include="GenHTTP.Modules.Security" Version="8.0.0" />
<PackageReference Include="GenHTTP.Modules.Functional" Version="8.0.0" />
<PackageReference Include="GenHTTP.Modules.Security" Version="8.1.0" />
<PackageReference Include="GenHTTP.Modules.Functional" Version="8.1.0" />

</ItemGroup>

Expand Down
26 changes: 4 additions & 22 deletions Templates/Webservice-Minimal/$safeprojectname$/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
using System.Collections.Generic;
using GenHTTP.Engine;

using GenHTTP.Engine;

using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Security;

// run this project and fetch a list of book records via http://localhost:8080/books/
var books = new List<Book>()
{
new Book(1, "Lord of the Rings")
};

// see https://genhttp.org/documentation/content/functional
var bookApi = Inline.Create()
.Get(() => books)
.Put((Book book) => books.Add(book));
using $safeprojectname$;

var content = Layout.Create()
.Add("books", bookApi)
.Add(CorsPolicy.Permissive());
var project = Project.Setup();

return Host.Create()
.Handler(content)
.Handler(project)
.Defaults()
.Console()
//-:cnd:noEmit
Expand All @@ -32,5 +16,3 @@
#endif
//+:cnd:noEmit
.Run();

record class Book(int ID, string Title);
41 changes: 41 additions & 0 deletions Templates/Webservice-Minimal/$safeprojectname$/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GenHTTP.Api.Content;

using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Security;
using GenHTTP.Modules.Functional;

namespace $safeprojectname$
{

record class Book(int ID, string Title);

public static class Project
{

public static IHandlerBuilder Setup()
{
// run this project and fetch a list of book records via http://localhost:8080/books/
var books = new List<Book>()
{
new Book(1, "Lord of the Rings")
};

// see https://genhttp.org/documentation/content/functional
var bookApi = Inline.Create()
.Get(() => books)
.Put((Book book) => books.Add(book));

return Layout.Create()
.Add("books", bookApi)
.Add(CorsPolicy.Permissive());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

using GenHTTP.Testing;

namespace $safeprojectname$.Tests.Services
namespace $safeprojectname$.Tests
{

[TestClass]
public class BookServiceTests
{

[TestMethod]
public async Task TestGetLordOfTheRings()
public async Task TestGetBooks()
{
using var runner = TestHost.Run(Project.Setup());

Expand Down

0 comments on commit 74dddd9

Please sign in to comment.