|
| 1 | +namespace Ocelot.AcceptanceTests |
| 2 | +{ |
| 3 | + using Microsoft.AspNetCore.Http; |
| 4 | + using Ocelot.Configuration.File; |
| 5 | + using System; |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.IO; |
| 8 | + using System.Net; |
| 9 | + using System.Net.Http; |
| 10 | + using Microsoft.AspNetCore.Server.Kestrel.Core; |
| 11 | + using TestStack.BDDfy; |
| 12 | + using Xunit; |
| 13 | + |
| 14 | + public class HttpTests : IDisposable |
| 15 | + { |
| 16 | + private readonly Steps _steps; |
| 17 | + private readonly ServiceHandler _serviceHandler; |
| 18 | + |
| 19 | + public HttpTests() |
| 20 | + { |
| 21 | + _serviceHandler = new ServiceHandler(); |
| 22 | + _steps = new Steps(); |
| 23 | + } |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void should_return_response_200_when_using_http_one_point_one() |
| 27 | + { |
| 28 | + const int port = 53279; |
| 29 | + |
| 30 | + var configuration = new FileConfiguration |
| 31 | + { |
| 32 | + ReRoutes = new List<FileReRoute> |
| 33 | + { |
| 34 | + new FileReRoute |
| 35 | + { |
| 36 | + DownstreamPathTemplate = "/{url}", |
| 37 | + DownstreamScheme = "https", |
| 38 | + UpstreamPathTemplate = "/{url}", |
| 39 | + UpstreamHttpMethod = new List<string> { "Get" }, |
| 40 | + DownstreamHostAndPorts = new List<FileHostAndPort> |
| 41 | + { |
| 42 | + new FileHostAndPort |
| 43 | + { |
| 44 | + Host = "localhost", |
| 45 | + Port = port, |
| 46 | + }, |
| 47 | + }, |
| 48 | + DownstreamHttpMethod = "POST", |
| 49 | + DownstreamHttpVersion = "1.1", |
| 50 | + DangerousAcceptAnyServerCertificateValidator = true |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) |
| 56 | + .And(x => _steps.GivenThereIsAConfiguration(configuration)) |
| 57 | + .And(x => _steps.GivenOcelotIsRunning()) |
| 58 | + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) |
| 59 | + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) |
| 60 | + .BDDfy(); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void should_return_response_200_when_using_http_two_point_zero() |
| 65 | + { |
| 66 | + const int port = 53675; |
| 67 | + |
| 68 | + var configuration = new FileConfiguration |
| 69 | + { |
| 70 | + ReRoutes = new List<FileReRoute> |
| 71 | + { |
| 72 | + new FileReRoute |
| 73 | + { |
| 74 | + DownstreamPathTemplate = "/{url}", |
| 75 | + DownstreamScheme = "https", |
| 76 | + UpstreamPathTemplate = "/{url}", |
| 77 | + UpstreamHttpMethod = new List<string> { "Get" }, |
| 78 | + DownstreamHostAndPorts = new List<FileHostAndPort> |
| 79 | + { |
| 80 | + new FileHostAndPort |
| 81 | + { |
| 82 | + Host = "localhost", |
| 83 | + Port = port, |
| 84 | + }, |
| 85 | + }, |
| 86 | + DownstreamHttpMethod = "POST", |
| 87 | + DownstreamHttpVersion = "2.0", |
| 88 | + DangerousAcceptAnyServerCertificateValidator = true |
| 89 | + }, |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + const string expected = "here is some content"; |
| 94 | + var httpContent = new StringContent(expected); |
| 95 | + |
| 96 | + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) |
| 97 | + .And(x => _steps.GivenThereIsAConfiguration(configuration)) |
| 98 | + .And(x => _steps.GivenOcelotIsRunning()) |
| 99 | + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) |
| 100 | + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) |
| 101 | + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) |
| 102 | + .BDDfy(); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two() |
| 107 | + { |
| 108 | + const int port = 53677; |
| 109 | + |
| 110 | + var configuration = new FileConfiguration |
| 111 | + { |
| 112 | + ReRoutes = new List<FileReRoute> |
| 113 | + { |
| 114 | + new FileReRoute |
| 115 | + { |
| 116 | + DownstreamPathTemplate = "/{url}", |
| 117 | + DownstreamScheme = "https", |
| 118 | + UpstreamPathTemplate = "/{url}", |
| 119 | + UpstreamHttpMethod = new List<string> { "Get" }, |
| 120 | + DownstreamHostAndPorts = new List<FileHostAndPort> |
| 121 | + { |
| 122 | + new FileHostAndPort |
| 123 | + { |
| 124 | + Host = "localhost", |
| 125 | + Port = port, |
| 126 | + }, |
| 127 | + }, |
| 128 | + DownstreamHttpMethod = "POST", |
| 129 | + DownstreamHttpVersion = "1.1", |
| 130 | + DangerousAcceptAnyServerCertificateValidator = true |
| 131 | + }, |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + const string expected = "here is some content"; |
| 136 | + var httpContent = new StringContent(expected); |
| 137 | + |
| 138 | + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http2)) |
| 139 | + .And(x => _steps.GivenThereIsAConfiguration(configuration)) |
| 140 | + .And(x => _steps.GivenOcelotIsRunning()) |
| 141 | + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) |
| 142 | + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError)) |
| 143 | + .BDDfy(); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void should_return_response_200_when_using_http_two_to_talk_to_server_running_http_one_point_one() |
| 148 | + { |
| 149 | + const int port = 53679; |
| 150 | + |
| 151 | + var configuration = new FileConfiguration |
| 152 | + { |
| 153 | + ReRoutes = new List<FileReRoute> |
| 154 | + { |
| 155 | + new FileReRoute |
| 156 | + { |
| 157 | + DownstreamPathTemplate = "/{url}", |
| 158 | + DownstreamScheme = "https", |
| 159 | + UpstreamPathTemplate = "/{url}", |
| 160 | + UpstreamHttpMethod = new List<string> { "Get" }, |
| 161 | + DownstreamHostAndPorts = new List<FileHostAndPort> |
| 162 | + { |
| 163 | + new FileHostAndPort |
| 164 | + { |
| 165 | + Host = "localhost", |
| 166 | + Port = port, |
| 167 | + }, |
| 168 | + }, |
| 169 | + DownstreamHttpMethod = "POST", |
| 170 | + DownstreamHttpVersion = "2.0", |
| 171 | + DangerousAcceptAnyServerCertificateValidator = true |
| 172 | + }, |
| 173 | + }, |
| 174 | + }; |
| 175 | + |
| 176 | + const string expected = "here is some content"; |
| 177 | + var httpContent = new StringContent(expected); |
| 178 | + |
| 179 | + this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}/", "/", port, HttpProtocols.Http1)) |
| 180 | + .And(x => _steps.GivenThereIsAConfiguration(configuration)) |
| 181 | + .And(x => _steps.GivenOcelotIsRunning()) |
| 182 | + .When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent)) |
| 183 | + .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) |
| 184 | + .And(_ => _steps.ThenTheResponseBodyShouldBe(expected)) |
| 185 | + .BDDfy(); |
| 186 | + } |
| 187 | + |
| 188 | + private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int port, HttpProtocols protocols) |
| 189 | + { |
| 190 | + _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context => |
| 191 | + { |
| 192 | + context.Response.StatusCode = 200; |
| 193 | + var reader = new StreamReader(context.Request.Body); |
| 194 | + var body = await reader.ReadToEndAsync(); |
| 195 | + await context.Response.WriteAsync(body); |
| 196 | + }, port, protocols); |
| 197 | + } |
| 198 | + |
| 199 | + public void Dispose() |
| 200 | + { |
| 201 | + _serviceHandler.Dispose(); |
| 202 | + _steps.Dispose(); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments