Skip to content

Commit 7f2a014

Browse files
authored
Merge pull request #8 from joncloud/improve-test-coverage
Improves test coverage
2 parents e8db377 + 12e1959 commit 7f2a014

9 files changed

Lines changed: 239 additions & 16 deletions

File tree

tests/https.Tests/ContentTypeTests.cs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,72 @@ public async Task MirrorTest_ShouldReflectJson()
4040
Assert.Equal("{\"foo\":\"bar\",\"lorem\":\"ipsum\"}", actual);
4141
}
4242

43+
static async Task RunXmlTestAsync(string[] args, XDocument expected)
44+
{
45+
var result = await Https.ExecuteAsync(args);
46+
47+
var actual = XDocument.Load(
48+
new StreamReader(result.StdOut)
49+
).ToString();
50+
51+
Assert.Equal(expected.ToString(), actual);
52+
}
53+
4354
[Fact]
44-
public async Task MirrorTest_ShouldReflectXml()
55+
public async Task MirrorTest_ShouldReflectXmlWithDefaultRootElementName()
4556
{
4657
var args = new[]
4758
{
48-
"post", $"{_fixture.HttpUrl}/Mirror", "--xml=root", "foo=bar", "lorem=ipsum"
59+
"post", $"{_fixture.HttpUrl}/Mirror", "--xml", "foo=bar", "lorem=ipsum"
4960
};
5061

51-
var result = await Https.ExecuteAsync(args);
62+
var expected = new XDocument(
63+
new XElement(
64+
"xml",
65+
new XElement("foo", "bar"),
66+
new XElement("lorem", "ipsum")
67+
)
68+
);
69+
70+
await RunXmlTestAsync(args, expected);
71+
}
72+
73+
[Fact]
74+
public async Task MirrorTest_ShouldReflectXmlWithEmptyRootElementName()
75+
{
76+
var args = new[]
77+
{
78+
"post", $"{_fixture.HttpUrl}/Mirror", "--xml= ", "foo=bar", "lorem=ipsum"
79+
};
80+
81+
var expected = new XDocument(
82+
new XElement(
83+
"xml",
84+
new XElement("foo", "bar"),
85+
new XElement("lorem", "ipsum")
86+
)
87+
);
88+
89+
await RunXmlTestAsync(args, expected);
90+
}
91+
92+
[Fact]
93+
public async Task MirrorTest_ShouldReflectXmlWithRootElementName()
94+
{
95+
var args = new[]
96+
{
97+
"post", $"{_fixture.HttpUrl}/Mirror", "--xml=root", "foo=bar", "lorem=ipsum"
98+
};
5299

53100
var expected = new XDocument(
54101
new XElement(
55102
"root",
56103
new XElement("foo", "bar"),
57104
new XElement("lorem", "ipsum")
58105
)
59-
).ToString();
60-
var actual = XDocument.Load(
61-
new StreamReader(result.StdOut)
62-
).ToString();
106+
);
63107

64-
Assert.Equal(expected, actual);
108+
await RunXmlTestAsync(args, expected);
65109
}
66110
}
67111
}

tests/https.Tests/HttpsCsprojFixture.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
1-
using System.Xml.Linq;
1+
using System.Linq;
2+
using System.Xml.Linq;
3+
using Xunit;
24

35
namespace Https.Tests
46
{
57
public class HttpsCsprojFixture
68
{
79
public XDocument Document { get; }
810

11+
public XElement VersionPrefix
12+
{
13+
get
14+
{
15+
var value = Document
16+
.Root
17+
.Elements("PropertyGroup")
18+
.Elements("VersionPrefix")
19+
.FirstOrDefault();
20+
21+
Assert.NotNull(value);
22+
23+
return value;
24+
}
25+
}
26+
927
public HttpsCsprojFixture()
1028
{
1129
var path = "../../../../../src/https/https.csproj";

tests/https.Tests/HttpsResult.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class HttpsResult : IDisposable
99
{
1010
public int ExitCode { get; }
1111
public MemoryStream StdOut { get; }
12+
public MemoryStream StdErr { get; }
1213
public string Status { get; }
1314
public IReadOnlyDictionary<string, string> Headers { get; }
1415

@@ -19,6 +20,11 @@ public HttpsResult(int exitCode, MemoryStream stdout, MemoryStream stderr)
1920
StdOut = stdout;
2021
StdOut.Position = 0;
2122

23+
stderr.Position = 0;
24+
StdErr = new MemoryStream();
25+
stderr.CopyTo(StdErr);
26+
StdErr.Position = 0;
27+
2228
stderr.Position = 0;
2329
var lines = new StreamReader(stderr)
2430
.ReadToEnd()

tests/https.Tests/MethodTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.IO;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using System.Xml.Linq;
5+
using Xunit;
6+
7+
namespace Https.Tests
8+
{
9+
[Collection(nameof(WebHostFixture))]
10+
public class MethodTests
11+
{
12+
readonly WebHostFixture _fixture;
13+
public MethodTests(WebHostFixture fixture) =>
14+
_fixture = fixture;
15+
16+
[Fact]
17+
public async Task MethodTest_ShouldHandleHead()
18+
{
19+
var args = new[]
20+
{
21+
"head", $"{_fixture.HttpUrl}/Mirror"
22+
};
23+
24+
var result = await Https.ExecuteAsync(args);
25+
26+
var actual = new StreamReader(result.StdOut).ReadToEnd();
27+
Assert.Equal("", actual);
28+
Assert.Equal(0, result.ExitCode);
29+
}
30+
31+
// Most of these methods probably should be tested differently,
32+
// but apparently they work through HttpClient and ASP.NET Core.
33+
[InlineData(nameof(HttpMethod.Delete))]
34+
[InlineData(nameof(HttpMethod.Get))]
35+
[InlineData(nameof(HttpMethod.Options))]
36+
[InlineData(nameof(HttpMethod.Patch))]
37+
[InlineData(nameof(HttpMethod.Post))]
38+
[InlineData(nameof(HttpMethod.Put))]
39+
[InlineData(nameof(HttpMethod.Trace))]
40+
[Theory]
41+
public async Task MethodTest_ShouldHandleMethod(string method)
42+
{
43+
var args = new[]
44+
{
45+
method, $"{_fixture.HttpUrl}/Mirror", "--form", "foo=bar", "lorem=ipsum"
46+
};
47+
48+
var result = await Https.ExecuteAsync(args);
49+
50+
var actual = new StreamReader(result.StdOut).ReadToEnd();
51+
Assert.Equal("foo=bar&lorem=ipsum", actual);
52+
}
53+
}
54+
}

tests/https.Tests/ReadmeTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ public ReadmeTests(HttpsCsprojFixture httpsCsprojFixture, ReadmeFixture readmeFi
1919
[Fact]
2020
public void Installation_ShouldListSameVersionAsCsproj()
2121
{
22-
var versionPrefixElement = _httpsCsprojFixture.Document
23-
.Root
24-
.Elements("PropertyGroup")
25-
.Elements("VersionPrefix")
26-
.FirstOrDefault();
27-
28-
Assert.NotNull(versionPrefixElement);
22+
var versionPrefixElement = _httpsCsprojFixture.VersionPrefix;
2923

3024
var expected = versionPrefixElement.Value;
3125
var actual = _readmeFixture.Readme.InstallationVersion;

tests/https.Tests/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public void Configure(IApplicationBuilder app)
1010
{
1111
app.UseMiddleware<RedirectMiddleware>();
1212
app.UseMiddleware<MirrorMiddleware>();
13+
app.UseMiddleware<TimeoutMiddleware>();
1314

1415
app.Run(async (context) =>
1516
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
5+
namespace Https.Tests
6+
{
7+
class TimeoutMiddleware
8+
{
9+
readonly RequestDelegate _next;
10+
public TimeoutMiddleware(RequestDelegate next) =>
11+
_next = next;
12+
13+
public async Task InvokeAsync(HttpContext context)
14+
{
15+
if (context.Request.Path.StartsWithSegments("/Timeout") &&
16+
context.Request.Query.TryGetValue("delay", out var delayValues) &&
17+
int.TryParse(delayValues.FirstOrDefault(), out var delay))
18+
{
19+
await Task.Delay(delay);
20+
context.Response.StatusCode = StatusCodes.Status200OK;
21+
await context.Response.WriteAsync("Ignore this");
22+
}
23+
else
24+
{
25+
await _next(context);
26+
}
27+
}
28+
}
29+
}

tests/https.Tests/TimeoutTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using System.Xml.Linq;
5+
using Xunit;
6+
7+
namespace Https.Tests
8+
{
9+
[Collection(nameof(WebHostFixture))]
10+
public class TimeoutTests
11+
{
12+
readonly WebHostFixture _fixture;
13+
public TimeoutTests(WebHostFixture fixture) =>
14+
_fixture = fixture;
15+
16+
[Fact]
17+
public async Task TimeoutTest_ShouldRespectTimeoutOption()
18+
{
19+
var delay = 1_000;
20+
var args = new[]
21+
{
22+
"post", $"{_fixture.HttpUrl}/Timeout?delay={delay * 4}", $"--timeout={TimeSpan.FromMilliseconds(delay)}"
23+
};
24+
25+
var result = await Https.ExecuteAsync(args);
26+
27+
Assert.Equal(1, result.ExitCode);
28+
29+
var stdout = new StreamReader(result.StdOut).ReadToEnd();
30+
var expectedOut = "";
31+
Assert.Equal(expectedOut, stdout);
32+
33+
var stderr = new StreamReader(result.StdErr).ReadToEnd();
34+
var expectedErr = string.Join(Environment.NewLine, new[]
35+
{
36+
"The request was canceled due to the configured HttpClient.Timeout of 1 seconds elapsing.",
37+
"Request failed to complete within timeout. Try increasing the timeout with the --timeout flag",
38+
""
39+
});
40+
Assert.Equal(expectedErr, stderr);
41+
}
42+
}
43+
}

tests/https.Tests/VersionTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using System.Xml.Linq;
5+
using Xunit;
6+
7+
namespace Https.Tests
8+
{
9+
public class VersionTests : IClassFixture<HttpsCsprojFixture>
10+
{
11+
readonly HttpsCsprojFixture _httpsCsprojFixture;
12+
public VersionTests(HttpsCsprojFixture httpsCsprojFixture)
13+
{
14+
_httpsCsprojFixture = httpsCsprojFixture;
15+
}
16+
17+
[Fact]
18+
public async Task VersionFlag_ShouldReportVersion()
19+
{
20+
var args = new[]
21+
{
22+
"--version"
23+
};
24+
25+
var versionPrefix = _httpsCsprojFixture.VersionPrefix.Value;
26+
27+
var expected = $"dotnet-https {versionPrefix}.0" + Environment.NewLine;
28+
var result = await Https.ExecuteAsync(args);
29+
var actual = new StreamReader(result.StdOut).ReadToEnd();
30+
31+
Assert.Equal(expected, actual);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)