Skip to content

Commit 48bd132

Browse files
Fix redirecting without explicit status code
1 parent 546aecd commit 48bd132

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/React.Router/HtmlHelperExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var reactComponent
123123

124124
var executionResult = reactComponent.RenderRouterWithContext(clientOnly, serverOnly);
125125

126-
if (executionResult.Context?.status != null)
126+
if (executionResult.Context?.status != null || executionResult.Context?.url != null)
127127
{
128128
// Use provided contextHandler
129129
if (contextHandler != null)

src/React.Router/SetServerResponse.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static class SetServerResponse
3636
/// <param name="Response">The response object to use.</param>
3737
public static void ModifyResponse(RoutingContext context, HttpResponse Response)
3838
{
39-
var statusCode = context.status.Value;
39+
var statusCode = context.status ?? 302;
4040

4141
// 300-399
4242
if (statusCode >= 300 && statusCode < 400)

tests/React.Tests/Router/HtmlHelperExtensionsTest.cs

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* Copyright (c) 2014-Present, Facebook, Inc.
33
* All rights reserved.
44
*
@@ -7,14 +7,12 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
using System.Web;
11+
using System.Web.Mvc;
1012
using Moq;
11-
using Xunit;
12-
using React.Exceptions;
1313
using React.Router;
1414
using React.Tests.Core;
15-
using System.Web;
16-
using JavaScriptEngineSwitcher.Core;
17-
using System.Web.Mvc;
15+
using Xunit;
1816

1917
namespace React.Tests.Router
2018
{
@@ -231,6 +229,26 @@ public void ShouldRedirectPermanent()
231229
htmlHelperMock.httpResponse.Verify(x => x.RedirectPermanent(It.IsAny<string>()));
232230
}
233231

232+
[Fact]
233+
public void ShouldRedirectWithJustUrl()
234+
{
235+
var mocks = ConfigureMockReactEnvironment();
236+
ConfigureMockConfiguration();
237+
238+
mocks.Engine.Setup(x => x.Evaluate<string>("JSON.stringify(context);"))
239+
.Returns(@"{ url: ""/foo"" }");
240+
241+
var htmlHelperMock = new HtmlHelperMocks();
242+
243+
HtmlHelperExtensions.ReactRouterWithContext(
244+
htmlHelper: htmlHelperMock.htmlHelper.Object,
245+
componentName: "ComponentName",
246+
props: new { },
247+
path: "/"
248+
);
249+
htmlHelperMock.httpResponse.Verify(x => x.Redirect(It.IsAny<string>()));
250+
}
251+
234252
[Fact]
235253
public void ShouldFailRedirectWithNoUrl()
236254
{

0 commit comments

Comments
 (0)