-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bdde49
commit a95f06c
Showing
6 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
test/Falco.OpenApi.Tests.App/Falco.OpenApi.Tests.App.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Urls": "https://localhost:5001", | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() |