Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Quick fix: Clean up test warnings
Browse files Browse the repository at this point in the history
- avoid "Skipping test case with duplicate ID" messages
  - xUnit gets confused when `[Theory]`s are overridden
- avoid "falling back to single test case" messages
  - fix inspired by aspnet/SignalR#1820
  • Loading branch information
dougbu committed Oct 29, 2018
1 parent 37e5629 commit 0b6932d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 105 deletions.
106 changes: 61 additions & 45 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/FileResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,21 @@ public async Task FileFromDisk_ReturnsFileWithFileName_IfRangeHeaderInvalid_Rang
Assert.Equal("This is a sample text file", body);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("", HttpStatusCode.OK, 26)]
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 26)]
[InlineData("0-6", HttpStatusCode.OK, 26)]
[InlineData("bytes = ", HttpStatusCode.OK, 26)]
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 26)]
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
[InlineData("", (int)HttpStatusCode.OK, 26)]
[InlineData("bytes = 0-6", (int)HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", (int)HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", (int)HttpStatusCode.PartialContent, 26)]
[InlineData("0-6", (int)HttpStatusCode.OK, 26)]
[InlineData("bytes = ", (int)HttpStatusCode.OK, 26)]
[InlineData("bytes = 1-4, 5-11", (int)HttpStatusCode.OK, 26)]
[InlineData("bytes = 35-36", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
[InlineData("bytes = -0", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 26)]
public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest_WithLastModifiedAndEtag(
string rangeString,
int httpStatusCode,
int expectedContentLength)
{
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromDiskWithFileName_WithLastModifiedAndEtag");
Expand All @@ -267,7 +271,7 @@ public async Task FileFromDisk_ReturnsFileWithFileName_DoesNotServeBody_ForHeadR
var response = await Client.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(httpStatusCode, response.StatusCode);
Assert.Equal(httpStatusCode, (int)response.StatusCode);

Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Expand Down Expand Up @@ -442,17 +446,21 @@ public async Task FileFromStream_ReturnsFileWithFileName_IfRangeHeaderInvalid_Ra
Assert.Equal("This is sample text from a stream", body);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("", HttpStatusCode.OK, 33)]
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 33)]
[InlineData("0-6", HttpStatusCode.OK, 33)]
[InlineData("bytes = ", HttpStatusCode.OK, 33)]
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 33)]
[InlineData("bytes = 35-36", HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
[InlineData("", (int)HttpStatusCode.OK, 33)]
[InlineData("bytes = 0-6", (int)HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", (int)HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", (int)HttpStatusCode.PartialContent, 33)]
[InlineData("0-6", (int)HttpStatusCode.OK, 33)]
[InlineData("bytes = ", (int)HttpStatusCode.OK, 33)]
[InlineData("bytes = 1-4, 5-11", (int)HttpStatusCode.OK, 33)]
[InlineData("bytes = 35-36", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
[InlineData("bytes = -0", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 33)]
public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(
string rangeString,
int httpStatusCode,
int expectedContentLength)
{
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromStreamWithFileName_WithEtag");
Expand All @@ -463,7 +471,7 @@ public async Task FileFromStream_ReturnsFileWithFileName_DoesNotServeBody_ForHea
var response = await Client.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(httpStatusCode, response.StatusCode);
Assert.Equal(httpStatusCode, (int)response.StatusCode);

Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Expand Down Expand Up @@ -616,7 +624,7 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_IfRangeHeaderValid(
var response = await Client.SendAsync(httpRequestMessage);
var body = await response.Content.ReadAsStringAsync();

// Assert
// Assert
Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.NotNull(body);
Expand All @@ -643,17 +651,21 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_IfRangeHeaderInvali
Assert.Equal("This is a sample text from a binary array", body);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("", HttpStatusCode.OK, 41)]
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 41)]
[InlineData("0-6", HttpStatusCode.OK, 41)]
[InlineData("bytes = ", HttpStatusCode.OK, 41)]
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 41)]
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
[InlineData("", (int)HttpStatusCode.OK, 41)]
[InlineData("bytes = 0-6", (int)HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", (int)HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", (int)HttpStatusCode.PartialContent, 41)]
[InlineData("0-6", (int)HttpStatusCode.OK, 41)]
[InlineData("bytes = ", (int)HttpStatusCode.OK, 41)]
[InlineData("bytes = 1-4, 5-11", (int)HttpStatusCode.OK, 41)]
[InlineData("bytes = 45-46", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
[InlineData("bytes = -0", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 41)]
public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(
string rangeString,
int httpStatusCode,
int expectedContentLength)
{
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/DownloadFiles/DownloadFromBinaryDataWithFileName_WithEtag");
Expand All @@ -664,7 +676,7 @@ public async Task FileFromBinaryData_ReturnsFileWithFileName_DoesNotServeBody_Fo
var response = await Client.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(httpStatusCode, response.StatusCode);
Assert.Equal(httpStatusCode, (int)response.StatusCode);

Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Expand Down Expand Up @@ -842,17 +854,21 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_RangeRequest
Assert.Equal("attachment; filename=downloadName.txt; filename*=UTF-8''downloadName.txt", contentDisposition);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("", HttpStatusCode.OK, 38)]
[InlineData("bytes = 0-6", HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", HttpStatusCode.PartialContent, 38)]
[InlineData("0-6", HttpStatusCode.OK, 38)]
[InlineData("bytes = ", HttpStatusCode.OK, 38)]
[InlineData("bytes = 1-4, 5-11", HttpStatusCode.OK, 38)]
[InlineData("bytes = 45-46", HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
[InlineData("bytes = -0", HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(string rangeString, HttpStatusCode httpStatusCode, int expectedContentLength)
[InlineData("", (int)HttpStatusCode.OK, 38)]
[InlineData("bytes = 0-6", (int)HttpStatusCode.PartialContent, 7)]
[InlineData("bytes = 17-25", (int)HttpStatusCode.PartialContent, 9)]
[InlineData("bytes = 0-50", (int)HttpStatusCode.PartialContent, 38)]
[InlineData("0-6", (int)HttpStatusCode.OK, 38)]
[InlineData("bytes = ", (int)HttpStatusCode.OK, 38)]
[InlineData("bytes = 1-4, 5-11", (int)HttpStatusCode.OK, 38)]
[InlineData("bytes = 45-46", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
[InlineData("bytes = -0", (int)HttpStatusCode.RequestedRangeNotSatisfiable, 38)]
public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServeBody_ForHeadRequest(
string rangeString,
int httpStatusCode,
int expectedContentLength)
{
// Arrange
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, "http://localhost/EmbeddedFiles/DownloadFileWithFileName");
Expand All @@ -863,7 +879,7 @@ public async Task FileFromEmbeddedResources_ReturnsFileWithFileName_DoesNotServe
var response = await Client.SendAsync(httpRequestMessage);

// Assert
Assert.Equal(httpStatusCode, response.StatusCode);
Assert.Equal(httpStatusCode, (int)response.StatusCode);

Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,53 +263,6 @@ public override async Task ConventionalRoutedController_InArea_ActionBlockedByHt
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}

[Theory]
[MemberData(nameof(AttributeRoutedAction_MultipleRouteAttributes_WithMultipleHttpAttributes_RespectsConstraintsData))]
public override async Task AttributeRoutedAction_MultipleRouteAttributes_WithMultipleHttpAttributes_RespectsConstraints(
string url,
string method)
{
// Arrange
var expectedUrl = new Uri(url).AbsolutePath;

// Act
var response = await Client.SendAsync(new HttpRequestMessage(new HttpMethod(method), url));

// Assert
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}

// Endpoint routing exposes HTTP 405s for HTTP method mismatches
[Theory]
[MemberData(nameof(AttributeRoutedAction_RejectsRequestsWithWrongMethods_InRoutesWithoutExtraTemplateSegmentsOnTheActionData))]
public override async Task AttributeRoutedAction_RejectsRequestsWithWrongMethods_InRoutesWithoutExtraTemplateSegmentsOnTheAction(
string method,
string url)
{
// Arrange
var request = new HttpRequestMessage(new HttpMethod(method), $"http://localhost{url}");

// Assert
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}

[Theory]
[MemberData(nameof(AttributeRouting_MixedAcceptVerbsAndRoute_UnreachableData))]
public override async Task AttributeRouting_MixedAcceptVerbsAndRoute_Unreachable(string path, string verb)
{
// Arrange
var request = new HttpRequestMessage(new HttpMethod(verb), "http://localhost" + path);

// Act
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}

[Fact]
public async Task ConventionalRoutedAction_ParameterTransformer()
{
Expand Down Expand Up @@ -423,5 +376,11 @@ public async Task ConventionalRoutedAction_ParameterTransformer_LinkToSelf()
Assert.Equal("Index", result.Action);
Assert.Equal("/ConventionalTransformerRoute/conventional-transformer", result.Link);
}

// Endpoint routing exposes HTTP 405s for HTTP method mismatches.
protected override void AssertCorsRejectionStatusCode(HttpResponseMessage response)
{
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
using Xunit;

Expand Down Expand Up @@ -362,7 +361,7 @@ public virtual async Task AttributeRoutedAction_RejectsRequestsWithWrongMethods_
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
AssertCorsRejectionStatusCode(response);
}

[Theory]
Expand Down Expand Up @@ -514,7 +513,7 @@ public virtual async Task AttributeRoutedAction_MultipleRouteAttributes_WithMult
var response = await Client.SendAsync(new HttpRequestMessage(new HttpMethod(method), url));

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
AssertCorsRejectionStatusCode(response);
}

// The url would be /Store/ListProducts with conventional routes
Expand Down Expand Up @@ -1295,7 +1294,7 @@ public virtual async Task AttributeRoutedAction_InArea_ExplicitLeaveArea()
Assert.Equal("/", result.Link);
}



[Fact]
public virtual async Task AttributeRoutedAction_InArea_LinkToConventionalRoutedActionInArea()
Expand Down Expand Up @@ -1431,7 +1430,7 @@ public virtual async Task AttributeRouting_MixedAcceptVerbsAndRoute_Unreachable(
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
AssertCorsRejectionStatusCode(response);
}

[Theory]
Expand Down Expand Up @@ -1475,5 +1474,10 @@ protected static LinkBuilder LinkFrom(string url)
{
return new LinkBuilder(url);
}

protected virtual void AssertCorsRejectionStatusCode(HttpResponseMessage response)
{
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,11 @@ public async Task ApiController_CreateResponse_HardcodedFormatter()
Assert.Equal("text/json", response.Content.Headers.ContentType.MediaType);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("http://localhost/Mvc/Index", HttpStatusCode.OK)]
[InlineData("http://localhost/api/Blog/Mvc/Index", HttpStatusCode.NotFound)]
public async Task WebApiRouting_AccessMvcController(string url, HttpStatusCode expected)
[InlineData("http://localhost/Mvc/Index", (int)HttpStatusCode.OK)]
[InlineData("http://localhost/api/Blog/Mvc/Index", (int)HttpStatusCode.NotFound)]
public async Task WebApiRouting_AccessMvcController(string url, int expected)
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, url);
Expand All @@ -370,13 +371,14 @@ public async Task WebApiRouting_AccessMvcController(string url, HttpStatusCode e
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(expected, response.StatusCode);
Assert.Equal(expected, (int)response.StatusCode);
}

// Use int for HttpStatusCode data because xUnit cannot serialize a GAC'd enum when running on .NET Framework.
[Theory]
[InlineData("http://localhost/BasicApi/GenerateUrl", HttpStatusCode.NotFound)]
[InlineData("http://localhost/api/Blog/BasicApi/GenerateUrl", HttpStatusCode.OK)]
public async Task WebApiRouting_AccessWebApiController(string url, HttpStatusCode expected)
[InlineData("http://localhost/BasicApi/GenerateUrl", (int)HttpStatusCode.NotFound)]
[InlineData("http://localhost/api/Blog/BasicApi/GenerateUrl", (int)HttpStatusCode.OK)]
public async Task WebApiRouting_AccessWebApiController(string url, int expected)
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, url);
Expand All @@ -385,7 +387,7 @@ public async Task WebApiRouting_AccessWebApiController(string url, HttpStatusCod
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(expected, response.StatusCode);
Assert.Equal(expected, (int)response.StatusCode);
}

[Fact]
Expand Down

0 comments on commit 0b6932d

Please sign in to comment.