-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged PR 2264: Fix encoding used in JS generated by prerenderer
Fix encoding used in JS generated by prerenderer
- Loading branch information
1 parent
124b96b
commit fc65b4f
Showing
3 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...test/Microsoft.AspNetCore.SpaServices.Tests/Microsoft.AspNetCore.SpaServices.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.AspNetCore.SpaServices" /> | ||
</ItemGroup> | ||
|
||
</Project> |
71 changes: 71 additions & 0 deletions
71
...eware/SpaServices/test/Microsoft.AspNetCore.SpaServices.Tests/RenderToStringResultTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.SpaServices.Prerendering; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.SpaServices.Tests | ||
{ | ||
public class RenderToStringResultTest | ||
{ | ||
[Fact] | ||
public void HandlesNullGlobals() | ||
{ | ||
// Arrange | ||
var renderToStringResult = new RenderToStringResult(); | ||
renderToStringResult.Globals = null; | ||
|
||
// Act | ||
var actualScript = renderToStringResult.CreateGlobalsAssignmentScript(); | ||
|
||
// Assert | ||
Assert.Equal(string.Empty, actualScript); | ||
} | ||
|
||
[Fact] | ||
public void HandlesGlobalsWithMultipleProperties() | ||
{ | ||
// Arrange | ||
var renderToStringResult = new RenderToStringResult(); | ||
renderToStringResult.Globals = ToJObject(new | ||
{ | ||
FirstProperty = "first value", | ||
SecondProperty = new[] { "Array entry 0", "Array entry 1" } | ||
}); | ||
|
||
// Act | ||
var actualScript = renderToStringResult.CreateGlobalsAssignmentScript(); | ||
|
||
// Assert | ||
var expectedScript = @"window[""FirstProperty""] = JSON.parse(""\u0022first value\u0022"");" + | ||
@"window[""SecondProperty""] = JSON.parse(""[\u0022Array entry 0\u0022,\u0022Array entry 1\u0022]"");"; | ||
Assert.Equal(expectedScript, actualScript); | ||
} | ||
|
||
[Fact] | ||
public void HandlesGlobalsWithCorrectStringEncoding() | ||
{ | ||
// Arrange | ||
var renderToStringResult = new RenderToStringResult(); | ||
renderToStringResult.Globals = ToJObject(new Dictionary<string, object> | ||
{ | ||
{ "Va<l'u\"e", "</tag>\"'}\u260E" } | ||
}); | ||
|
||
// Act | ||
var actualScript = renderToStringResult.CreateGlobalsAssignmentScript(); | ||
|
||
// Assert | ||
var expectedScript = @"window[""Va\u003Cl\u0027u\u0022e""] = JSON.parse(""\u0022\u003C\/tag\u003E\\\u0022\u0027}\u260E\u0022"");"; | ||
Assert.Equal(expectedScript, actualScript); | ||
} | ||
|
||
private static JObject ToJObject(object value) | ||
{ | ||
return JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(value)); | ||
} | ||
} | ||
} |