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

273 lines
8.0 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 UserApiHandlerTestsHelper
open OpenAPI.UserApiHandler
open OpenAPI.UserApiHandlerParams
open OpenAPI.Model.User
module UserApiHandlerTests =
// ---------------------------------
// Tests
// ---------------------------------
[<Fact>]
let ``CreateUser - Create user returns 0 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getCreateUserExample "application/json")
// or pass a body of type User
let body = obj() :?> User |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(0))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``CreateUsersWithArrayInput - Creates list of users with given input array returns 0 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/createWithArray"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getCreateUsersWithArrayInputExample "application/json")
// or pass a body of type User[]
let body = obj() :?> User[] |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(0))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``CreateUsersWithListInput - Creates list of users with given input array returns 0 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/createWithList"
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getCreateUsersWithListInputExample "application/json")
// or pass a body of type User[]
let body = obj() :?> User[] |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPost client path
|> isStatus (enum<HttpStatusCode>(0))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``DeleteUser - Delete user returns 400 where Invalid username supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
HttpDelete client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``DeleteUser - Delete user returns 404 where User not found`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
HttpDelete client path
|> isStatus (enum<HttpStatusCode>(404))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetUserByName - Get user by user name returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetUserByName - Get user by user name returns 400 where Invalid username supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``GetUserByName - Get user by user name returns 404 where User not found`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
HttpGet client path
|> isStatus (enum<HttpStatusCode>(404))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``LoginUser - Logs user into the system returns 200 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/login" + "?username=ADDME&password=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(200))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``LoginUser - Logs user into the system returns 400 where Invalid username/password supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/login" + "?username=ADDME&password=ADDME"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``LogoutUser - Logs out current logged in user session returns 0 where successful operation`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/logout"
HttpGet client path
|> isStatus (enum<HttpStatusCode>(0))
|> readText
|> shouldEqual "TESTME"
|> ignore
}
[<Fact>]
let ``UpdateUser - Updated user returns 400 where Invalid user supplied`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getUpdateUserExample "application/json")
// or pass a body of type User
let body = obj() :?> User |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPut client path
|> isStatus (enum<HttpStatusCode>(400))
|> readText
|> shouldEqual "TESTME"
}
[<Fact>]
let ``UpdateUser - Updated user returns 404 where User not found`` () =
task {
use server = new TestServer(createHost())
use client = server.CreateClient()
// add your setup code here
let path = "/v2/user/{username}".Replace("username", "ADDME")
// use an example requestBody provided by the spec
let examples = Map.empty.Add("application/json", getUpdateUserExample "application/json")
// or pass a body of type User
let body = obj() :?> User |> Newtonsoft.Json.JsonConvert.SerializeObject |> Encoding.UTF8.GetBytes |> MemoryStream |> StreamContent
body
|> HttpPut client path
|> isStatus (enum<HttpStatusCode>(404))
|> readText
|> shouldEqual "TESTME"
}