Skip to content

Commit

Permalink
complete integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pimbrouwers committed Oct 18, 2024
1 parent 2bdde49 commit a95f06c
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 2 deletions.
18 changes: 18 additions & 0 deletions test/Falco.OpenApi.Tests.App/Falco.OpenApi.Tests.App.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Falco" Version="5.0.0-alpha3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Falco.OpenApi/Falco.OpenApi.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Falco.OpenApi.Tests" />
</ItemGroup>
</Project>
62 changes: 62 additions & 0 deletions test/Falco.OpenApi.Tests.App/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module Falco.OpenApi.Tests.App

open Falco
open Falco.OpenApi
open Falco.Routing
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting

type Greeting =
{ Message : string }

let endpoints =
[
get "/" (Response.ofPlainText "Hello World!")
|> OpenApi.name "HelloWorld"
|> OpenApi.summary "A hello world greeter"
|> OpenApi.description "This is a simple endpoint that will return a greeting message."

mapGet "/hello/{name?}"
(fun route ->
let name = route?name.AsStringNonEmpty("world")
let age = route?age.AsIntOption()

let message =
match age with
| Some a -> $"Hello {name}, you are {a} years old!"
| _ -> $"Hello {name}!"

{ Message = message })
Response.ofJson
|> OpenApi.name "Greeting"
|> OpenApi.summary "A friendly greeter"
|> OpenApi.description "This endpoint will provide a customized greeting based on the name and age (optional) provided."
|> OpenApi.route [
{ Type = typeof<string>; Name = "Name"; Required = false } ]
|> OpenApi.query [
{ Type = typeof<int>; Name = "Age"; Required = false } ]
|> OpenApi.acceptsType typeof<string>
|> OpenApi.returnType typeof<Greeting>
]

let bldr = WebApplication.CreateBuilder()

bldr.Services
.AddFalcoOpenApi()
.AddSwaggerGen()
|> ignore

let wapp = bldr.Build()

wapp.UseHttpsRedirection()
.UseSwagger()
.UseSwaggerUI()
|> ignore

wapp.UseFalco(endpoints)
|> ignore

wapp.Run()

type Program() = class end
8 changes: 8 additions & 0 deletions test/Falco.OpenApi.Tests.App/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Urls": "https://localhost:5001",
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
7 changes: 5 additions & 2 deletions test/Falco.OpenApi.Tests/Falco.OpenApi.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="TestHelpers.fs" />
<Compile Include="OpenApiTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand All @@ -26,6 +28,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Falco.OpenApi\Falco.OpenApi.fsproj" />
<ProjectReference Include="../../src/Falco.OpenApi/Falco.OpenApi.fsproj" />
<ProjectReference Include="../Falco.OpenApi.Tests.App/Falco.OpenApi.Tests.App.fsproj" />
</ItemGroup>
</Project>
123 changes: 123 additions & 0 deletions test/Falco.OpenApi.Tests/OpenApiTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
module Falco.OpenApi.Tests.OpenApi

open Xunit

[<Fact>]
let ``Can GET /hello``() =
let factory = FalcoOpenApiTestServer.createFactory ()
let client = factory.CreateClient()
let response = client.GetAsync("/").Result
let content = response.Content.ReadAsStringAsync().Result
Assert.Equal("Hello World!", content)

[<Fact>]
let ``Can GET /hello/{name?}`` () =
let factory = FalcoOpenApiTestServer.createFactory ()
let client = factory.CreateClient()
let response = client.GetAsync("/hello").Result
let content = response.Content.ReadAsStringAsync().Result
Assert.Equal("""{"Message":"Hello world!"}""", content)

let response = client.GetAsync("/hello/John").Result
let content = response.Content.ReadAsStringAsync().Result
Assert.Equal("""{"Message":"Hello John!"}""", content)

let response = client.GetAsync("/hello/John?age=42").Result
let content = response.Content.ReadAsStringAsync().Result
Assert.Equal("""{"Message":"Hello John, you are 42 years old!"}""", content)

let private expectedOpenApiDoc = """{
"openapi": "3.0.1",
"info": {
"title": "testhost",
"version": "1.0"
},
"paths": {
"/hello/{name}": {
"get": {
"tags": [
"Greeting"
],
"summary": "A friendly greeter",
"description": "This endpoint will provide a customized greeting based on the name and age (optional) provided.",
"operationId": "Greeting",
"parameters": [
{
"name": "Name",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "Age",
"in": "query",
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Greeting"
}
}
}
}
}
}
},
"/": {
"get": {
"tags": [
"HelloWorld"
],
"summary": "A hello world greeter",
"description": "This is a simple endpoint that will return a greeting message.",
"operationId": "HelloWorld",
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
"schemas": {
"Greeting": {
"type": "object",
"properties": {
"message": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}"""

[<Fact>]
let ``Can GET /swagger/v1/swagger.json`` () =
let factory = FalcoOpenApiTestServer.createFactory ()
let client = factory.CreateClient()
let response = client.GetAsync("/swagger/v1/swagger.json").Result
let content = response.Content.ReadAsStringAsync().Result
Assert.Equal(expectedOpenApiDoc, content)
9 changes: 9 additions & 0 deletions test/Falco.OpenApi.Tests/TestHelpers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Falco.OpenApi.Tests

open Microsoft.AspNetCore.Mvc.Testing
open Microsoft.AspNetCore.TestHost
open Falco.OpenApi.Tests.App

module FalcoOpenApiTestServer =
let createFactory() =
new WebApplicationFactory<Program>()

0 comments on commit a95f06c

Please sign in to comment.