Skip to content

Commit 25d40ce

Browse files
committed
Better testing types
1 parent bb161f6 commit 25d40ce

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

tests/https.Tests/Https.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
4+
namespace Https.Tests
5+
{
6+
public static class Https
7+
{
8+
public static async Task<HttpsResult> ExecuteAsync(params string[] args)
9+
{
10+
using var stdin = new MemoryStream();
11+
12+
return await ExecuteAsync(stdin, args);
13+
}
14+
15+
public static async Task<HttpsResult> ExecuteAsync(Stream stdin, params string[] args)
16+
{
17+
var stdout = new MemoryStream();
18+
var stderr = new MemoryStream();
19+
20+
var exitCode = await new Program(() => stderr, () => stdin, () => stdout, false)
21+
.RunAsync(args);
22+
23+
return new HttpsResult(exitCode, stdout, stderr);
24+
}
25+
}
26+
}

tests/https.Tests/HttpsResult.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace Https.Tests
7+
{
8+
public class HttpsResult : IDisposable
9+
{
10+
public int ExitCode { get; }
11+
public MemoryStream StdOut { get; }
12+
public string Status { get; }
13+
public IReadOnlyDictionary<string, string> Headers { get; }
14+
15+
public HttpsResult(int exitCode, MemoryStream stdout, MemoryStream stderr)
16+
{
17+
ExitCode = exitCode;
18+
19+
StdOut = stdout;
20+
StdOut.Position = 0;
21+
22+
stderr.Position = 0;
23+
var lines = new StreamReader(stderr)
24+
.ReadToEnd()
25+
.Split(Environment.NewLine);
26+
27+
Status = lines[0];
28+
29+
var headers = new Dictionary<string, string>();
30+
foreach (var line in lines.Skip(1))
31+
{
32+
var pos = line.IndexOf(':');
33+
if (pos > -1)
34+
{
35+
var key = line.Substring(0, pos);
36+
var value = line.Substring(pos + 1);
37+
headers[key] = value;
38+
}
39+
}
40+
Headers = headers;
41+
42+
stderr.Dispose();
43+
}
44+
45+
public void Dispose()
46+
{
47+
StdOut.Dispose();
48+
}
49+
}
50+
}

tests/https.Tests/IntegrationTests.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,10 @@ public async Task MirrorTests()
1818
"post", _fixture.Url, "--json", "foo=bar", "lorem=ipsum"
1919
};
2020

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);
21+
var result = await Https.ExecuteAsync(args);
2722

28-
stdout.Position = 0;
29-
var json = new StreamReader(stdout).ReadToEnd();
30-
Assert.Equal("{\"foo\":\"bar\",\"lorem\":\"ipsum\"}", json);
31-
stderr.Position = 0;
32-
}
23+
var json = new StreamReader(result.StdOut).ReadToEnd();
24+
Assert.Equal("{\"foo\":\"bar\",\"lorem\":\"ipsum\"}", json);
3325
}
3426
}
3527
}

0 commit comments

Comments
 (0)