|
| 1 | +namespace GiraffeGenerator.IntegrationTests |
| 2 | + |
| 3 | +open System.Net |
| 4 | +open System.Net.Http |
| 5 | +open System.Threading.Tasks |
| 6 | +open FSharp.Control.Tasks.V2.ContextInsensitive |
| 7 | +open System |
| 8 | +open Giraffe |
| 9 | +open Microsoft.AspNetCore |
| 10 | +open Microsoft.AspNetCore.Builder |
| 11 | +open Microsoft.AspNetCore.Hosting |
| 12 | +open Microsoft.AspNetCore.TestHost |
| 13 | +open Microsoft.Extensions.DependencyInjection |
| 14 | +open Microsoft.Extensions.Logging |
| 15 | +open Xunit |
| 16 | + |
| 17 | +type SpecWithTextXmlTests() = |
| 18 | + |
| 19 | + let service = |
| 20 | + { new SpecwithxmlschemasAPI.Service() with |
| 21 | + member _.PostXmlInput ((i,_)) = Task.FromResult i |
| 22 | + member _.ApplicationJsonInput _ = Task.FromResult "1" |
| 23 | + member _.ApplicationXmlInput _ = Task.FromResult "2" |
| 24 | + member _.TextJsonInput _ = Task.FromResult "3" |
| 25 | + member _.TextXmlInput _ = Task.FromResult "4" } |
| 26 | + |
| 27 | + let configureApp (app : IApplicationBuilder) = |
| 28 | + app.UseGiraffe SpecwithxmlschemasAPI.webApp |
| 29 | + |
| 30 | + let configureServices (services : IServiceCollection) = |
| 31 | + services |
| 32 | + .AddGiraffe() |
| 33 | + .AddSingleton(service) |
| 34 | + |> ignore |
| 35 | + |
| 36 | + let configureLogging (loggerBuilder : ILoggingBuilder) = |
| 37 | + loggerBuilder.AddFilter(fun lvl -> lvl.Equals LogLevel.Error) |
| 38 | + .AddConsole() |
| 39 | + .AddDebug() |> ignore |
| 40 | + |
| 41 | + let webHostBuilder = |
| 42 | + WebHost.CreateDefaultBuilder() |
| 43 | + .Configure(Action<IApplicationBuilder> configureApp) |
| 44 | + .ConfigureServices(configureServices) |
| 45 | + .ConfigureLogging(configureLogging) |
| 46 | + |
| 47 | + let server = new TestServer(webHostBuilder) |
| 48 | + let client = server.CreateClient() |
| 49 | + |
| 50 | + [<Fact>] |
| 51 | + let ``GET /applicationJson -> OK "1"``() = task { |
| 52 | + let! response = client.GetAsync("/applicationJson") |
| 53 | + let! text = response.Content.ReadAsStringAsync() |
| 54 | + Assert.Equal("\"1\"",text) |
| 55 | + Assert.Equal(HttpStatusCode.OK ,response.StatusCode) |
| 56 | + } |
| 57 | + |
| 58 | + [<Fact>] |
| 59 | + let ``GET /applicationXml -> OK "2"``() = task { |
| 60 | + let! response = client.GetAsync("/applicationXml") |
| 61 | + let! text = response.Content.ReadAsStringAsync() |
| 62 | + Assert.Equal("""<?xml version="1.0" encoding="utf-8"?> |
| 63 | +<string>2</string>""",text) |
| 64 | + Assert.Equal(HttpStatusCode.OK ,response.StatusCode) |
| 65 | + } |
| 66 | + |
| 67 | + [<Fact>] |
| 68 | + let ``GET /textJson -> OK "3"``() = task { |
| 69 | + let! response = client.GetAsync("/textJson") |
| 70 | + let! text = response.Content.ReadAsStringAsync() |
| 71 | + Assert.Equal("\"3\"",text) |
| 72 | + Assert.Equal(HttpStatusCode.OK ,response.StatusCode) |
| 73 | + } |
| 74 | + |
| 75 | + [<Fact>] |
| 76 | + let ``GET /textXml -> OK "4"``() = task { |
| 77 | + let! response = client.GetAsync("/textXml") |
| 78 | + let! text = response.Content.ReadAsStringAsync() |
| 79 | + Assert.Equal("""<?xml version="1.0" encoding="utf-8"?> |
| 80 | +<string>4</string>""",text) |
| 81 | + Assert.Equal(HttpStatusCode.OK ,response.StatusCode) |
| 82 | + } |
| 83 | + |
| 84 | + [<Fact>] |
| 85 | + let ``POST /postXml -> OK "42"``() = task { |
| 86 | + use xmlContent = new StringContent("""<?xml version="1.0" encoding="utf-8"?> |
| 87 | +<int>42</int>""", Text.Encoding.UTF8, "application/xml") |
| 88 | + let! response = client.PostAsync("/postXml", xmlContent) |
| 89 | + let! text = response.Content.ReadAsStringAsync() |
| 90 | + Assert.Equal("""<?xml version="1.0" encoding="utf-8"?> |
| 91 | +<int>42</int>""",text) |
| 92 | + Assert.Equal(HttpStatusCode.OK ,response.StatusCode) |
| 93 | + } |
| 94 | + |
| 95 | + interface IDisposable with |
| 96 | + member _.Dispose() = server.Dispose() |
0 commit comments