Skip to content

Commit ec8a5b6

Browse files
committed
finished tests around http2
1 parent 2772ce4 commit ec8a5b6

File tree

5 files changed

+236
-4
lines changed

5 files changed

+236
-4
lines changed

src/Ocelot/Configuration/Creator/VersionCreator.cs renamed to src/Ocelot/Configuration/Creator/HttpVersionCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System;
44

5-
public class VersionCreator : IVersionCreator
5+
public class HttpVersionCreator : IVersionCreator
66
{
77
public Version Create(string downstreamHttpVersion)
88
{

src/Ocelot/DependencyInjection/OcelotBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public OcelotBuilder(IServiceCollection services, IConfiguration configurationRo
136136
Services.TryAddSingleton<IFrameworkDescription, FrameworkDescription>();
137137
Services.TryAddSingleton<IQoSFactory, QoSFactory>();
138138
Services.TryAddSingleton<IExceptionToErrorMapper, HttpExeptionToErrorMapper>();
139-
Services.TryAddSingleton<IVersionCreator, VersionCreator>();
139+
Services.TryAddSingleton<IVersionCreator, HttpVersionCreator>();
140140

141141
//add security
142142
this.AddSecurity();
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
}

test/Ocelot.AcceptanceTests/ServiceHandler.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.Logging;
88
using System;
9+
using System.ComponentModel;
910
using System.IO;
1011
using System.Net;
1112
using System.Threading.Tasks;
13+
using Microsoft.AspNetCore.Server.Kestrel.Core;
1214

1315
public class ServiceHandler : IDisposable
1416
{
@@ -47,6 +49,31 @@ public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, Reque
4749
_builder.Start();
4850
}
4951

52+
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate del, int port, HttpProtocols protocols)
53+
{
54+
_builder = new WebHostBuilder()
55+
.UseUrls(baseUrl)
56+
.UseKestrel()
57+
.ConfigureKestrel(serverOptions =>
58+
{
59+
serverOptions.Listen(IPAddress.Loopback, port, listenOptions =>
60+
{
61+
listenOptions.UseHttps("idsrv3test.pfx", "idsrv3test");
62+
listenOptions.Protocols = protocols;
63+
});
64+
})
65+
.UseContentRoot(Directory.GetCurrentDirectory())
66+
.UseIISIntegration()
67+
.Configure(app =>
68+
{
69+
app.UsePathBase(basePath);
70+
app.Run(del);
71+
})
72+
.Build();
73+
74+
_builder.Start();
75+
}
76+
5077
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string fileName, string password, int port, RequestDelegate del)
5178
{
5279
_builder = new WebHostBuilder()

test/Ocelot.UnitTests/Configuration/VersionCreatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
public class VersionCreatorTests
1010
{
11-
private readonly VersionCreator _creator;
11+
private readonly HttpVersionCreator _creator;
1212
private string _input;
1313
private Version _result;
1414

1515
public VersionCreatorTests()
1616
{
17-
_creator = new VersionCreator();
17+
_creator = new HttpVersionCreator();
1818
}
1919

2020
[Fact]

0 commit comments

Comments
 (0)