William Cheng e8a83dfe1f
Add beta server stub generator for F#/Giraffe (#2802)
* Beta server stub generator for F#/Giraffe (#2705)

* first commit for F#/Giraffe

use CLI generator

work on handlers

add binding to url params

add parameter declaration to handler & fix array types

order models by dependency and add tests

add oauth handlers

add service generation

add service implementation

return json for map types and add all return types to service implementation

pare down record types for readability

move implementations to impl folder

fix additional handler invocation

remove logging

remove open api type provider package reference

add sane defaults for OAuth

add readme and reorganize files for easier ignore

fix oauth checks and move login to default template

typedef operation body params as model

add API test templates

fix test templates

set project & other folders when packageName is set

add ignore to test pipes

add ignore for oauth to hide compile warnings

escape model types for generic dictionaries

remove Boolean object from primitives

fix handler and param templates for multiple path params

remove "Model" from model module names and fix import mapping for dictionary

add package name to model imports

change model templates to use imports

move login to CustomHandlers

raise exception where oauth not properly configured

allow webhost configuration from CustomHandlers

remove explicit support for nullable types and render option in template instead

move Cookie options to CustomHandlers

add header params

integrate api key provider

add nullable to datetime types

fix test generation and pretty up model folder

add context path to handler test template

dont copy spec file

remove superseded copyright notices

remove superseded copyright notices

* remove carriage return in fsharp template

* remove superseded sample output directory

* fix bash build script

* update generated sample

* update documentation

* add new file

* fix compile issues
2019-05-06 01:30:51 +08:00

296 lines
8.8 KiB
Forth

namespace OpenAPI.Tests
open System
open System.Net
open System.Net.Http
open System.IO
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.TestHost
open Microsoft.Extensions.DependencyInjection
open FSharp.Control.Tasks.V2.ContextInsensitive
open Xunit
open System.Text
open Newtonsoft
open TestHelper
open PetApiHandlerTestsHelper
open OpenAPI.PetApiHandler
open OpenAPI.PetApiHandlerParams
open OpenAPI.Model.ApiResponse
open OpenAPI.Model.Pet
module PetApiHandlerTests =
// ---------------------------------
// Tests
// ---------------------------------
[<Fact>]
let ``AddPet - Add a new pet to the store returns 405 where Invalid input`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getAddPetExample "application/json").Add("application/xml", getAddPetExample "application/xml")
// or pass a body of type Pet
let body = obj() :?> Pet |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(405))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``DeletePet - Deletes a pet returns 400 where Invalid pet value`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}".Replace("petId", "ADDME")
HttpDelete client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``FindPetsByStatus - Finds Pets by status returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/findByStatus" + "?status=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``FindPetsByStatus - Finds Pets by status returns 400 where Invalid status value`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/findByStatus" + "?status=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``FindPetsByTags - Finds Pets by tags returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/findByTags" + "?tags=ADDME&maxCount=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``FindPetsByTags - Finds Pets by tags returns 400 where Invalid tag value`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/findByTags" + "?tags=ADDME&maxCount=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetPetById - Find pet by ID returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}".Replace("petId", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetPetById - Find pet by ID returns 400 where Invalid ID supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}".Replace("petId", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetPetById - Find pet by ID returns 404 where Pet not found`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}".Replace("petId", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(404))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``UpdatePet - Update an existing pet returns 400 where Invalid ID supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getUpdatePetExample "application/json").Add("application/xml", getUpdatePetExample "application/xml")
// or pass a body of type Pet
let body = obj() :?> Pet |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPut client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``UpdatePet - Update an existing pet returns 404 where Pet not found`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getUpdatePetExample "application/json").Add("application/xml", getUpdatePetExample "application/xml")
// or pass a body of type Pet
let body = obj() :?> Pet |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPut client path
|> isStatus (enum<HttpStatusCode>(404))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``UpdatePet - Update an existing pet returns 405 where Validation exception`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getUpdatePetExample "application/json").Add("application/xml", getUpdatePetExample "application/xml")
// or pass a body of type Pet
let body = obj() :?> Pet |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPut client path
|> isStatus (enum<HttpStatusCode>(405))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``UpdatePetWithForm - Updates a pet in the store with form data returns 405 where Invalid input`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}".Replace("petId", "ADDME")
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/x-www-form-urlencoded", getUpdatePetWithFormExample "application/x-www-form-urlencoded")
// or pass a formform
let body = obj() |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(405))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``UploadFile - uploads an image returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/pet/{petId}/uploadImage".Replace("petId", "ADDME")
// use an example requestBody provided by the spec
let examples = Map.empty.Add("multipart/form-data", getUploadFileExample "multipart/form-data")
// or pass a formform
let body = obj() |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
}