Skip to content

Commit bb161f6

Browse files
committed
Tunes up test
1 parent 2acf359 commit bb161f6

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace Https.Tests
6+
{
7+
public class IntegrationTests : IClassFixture<WebHostFixture>
8+
{
9+
readonly WebHostFixture _fixture;
10+
public IntegrationTests(WebHostFixture fixture) =>
11+
_fixture = fixture;
12+
13+
[Fact]
14+
public async Task MirrorTests()
15+
{
16+
var args = new[]
17+
{
18+
"post", _fixture.Url, "--json", "foo=bar", "lorem=ipsum"
19+
};
20+
21+
using (var stdin = new MemoryStream())
22+
using (var stdout = new MemoryStream())
23+
using (var stderr = new MemoryStream())
24+
{
25+
await new Program(() => stderr, () => stdin, () => stdout, false)
26+
.RunAsync(args);
27+
28+
stdout.Position = 0;
29+
var json = new StreamReader(stdout).ReadToEnd();
30+
Assert.Equal("{\"foo\":\"bar\",\"lorem\":\"ipsum\"}", json);
31+
stderr.Position = 0;
32+
}
33+
}
34+
}
35+
}

tests/https.Tests/Startup.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
4+
namespace Https.Tests
5+
{
6+
public class Startup
7+
{
8+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
9+
{
10+
app.Run(async (context) =>
11+
{
12+
if (!string.IsNullOrEmpty(context.Request.ContentType))
13+
{
14+
context.Response.ContentType = context.Request.ContentType;
15+
}
16+
17+
await context.Request.Body.CopyToAsync(context.Response.Body);
18+
});
19+
}
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore;
2+
using Microsoft.AspNetCore.Hosting;
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace Https.Tests
8+
{
9+
public class WebHostFixture : IDisposable
10+
{
11+
readonly IWebHost _webHost;
12+
readonly Task _task;
13+
readonly CancellationTokenSource _cts;
14+
public string Url { get; }
15+
16+
public WebHostFixture()
17+
{
18+
_webHost = WebHost.CreateDefaultBuilder()
19+
.UseUrls(Url = "http://localhost:5000")
20+
.UseStartup<Startup>()
21+
.Build();
22+
_cts = new CancellationTokenSource();
23+
_task = _webHost.RunAsync(_cts.Token);
24+
}
25+
26+
public async void Dispose()
27+
{
28+
await _webHost.StopAsync();
29+
_cts.Cancel();
30+
_cts.Dispose();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)