Skip to content

Commit

Permalink
Check for empty list instead of null value. (#630)
Browse files Browse the repository at this point in the history
* Check for empty list instead of null value.

* Added encoding and BOM check test.

Co-authored-by: Max Malakhowsky <[email protected]>
Co-authored-by: John Gathogo <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2022
1 parent c4f229d commit ceac943
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public override void WriteResponseHeaders(OutputFormatterWriteContext context)
// Set the character set.
MediaTypeHeaderValue currentContentType = GetContentType(response.Headers[HeaderNames.ContentType].FirstOrDefault());
RequestHeaders requestHeader = request.GetTypedHeaders();
if (requestHeader != null && requestHeader.AcceptCharset != null)
// Starting from ASP .NET Core 3.0 AcceptCharset returns an empty collection instead of null.
if (requestHeader?.AcceptCharset.Count > 0)
{
IEnumerable<string> acceptCharsetValues = requestHeader.AcceptCharset.Select(cs => cs.Value.Value);

Expand Down
26 changes: 26 additions & 0 deletions test/Microsoft.AspNetCore.OData.E2E.Tests/Enums/EnumsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ public async Task QueryEntitySetCount(string requestUri, int expectedCount)
Assert.Equal<int>(expectedCount, int.Parse(count));
}

[Theory]
[InlineData("/convention/Employees/$count", true)]
[InlineData("/convention/Employees/$count", false)]
public async Task QueryEntitySetCountEncoding(string requestUri, bool sendAcceptCharset)
{
// Arrange
await ResetDatasource();
HttpClient client = CreateClient();
client.DefaultRequestHeaders.AcceptCharset.Clear();
if (sendAcceptCharset)
{
client.DefaultRequestHeaders.AcceptCharset.ParseAdd("utf-8");
}

// Act
HttpResponseMessage response = await client.GetAsync(requestUri);

// Assert
Assert.True(response.IsSuccessStatusCode);
var blob = await response.Content.ReadAsByteArrayAsync();
Assert.Equal("utf-8", response.Content.Headers.ContentType.CharSet, StringComparer.OrdinalIgnoreCase);
var count = Encoding.UTF8.GetString(blob);
Assert.Equal(3, int.Parse(count));
Assert.False((blob[0] == 0xEF) && (blob[1] == 0xBB) && (blob[2] == 0xBF));
}

[Theory]
[InlineData("/convention/Employees(1)/SkillSet/$count", 2)]
[InlineData("/convention/Employees(1)/SkillSet/$count?$filter=$it eq Microsoft.AspNetCore.OData.E2E.Tests.Enums.Skill'Sql'", 1)]
Expand Down

0 comments on commit ceac943

Please sign in to comment.