Skip to content

Commit

Permalink
Ensure ForeverFrameTransport throws if frameId is not a number
Browse files Browse the repository at this point in the history
- Added a test to verify the exception when not a number which required making InitializeResponse protected internal
- #1446
  • Loading branch information
DamianEdwards committed Jan 30, 2013
1 parent d3b5b5f commit b58782d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/Microsoft.AspNet.SignalR.Core/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.AspNet.SignalR.Core/Resources.resx
Expand Up @@ -156,6 +156,9 @@
<data name="Error_HubInvocationFailed" xml:space="preserve">
<value>There was an error invoking Hub method '{0}.{1}'.</value>
</data>
<data name="Error_InvalidForeverFrameId" xml:space="preserve">
<value>The supplied frameId is in the incorrect format.</value>
</data>
<data name="Error_IsNotA" xml:space="preserve">
<value>'{0}' is not a {1}.</value>
</data>
Expand Down
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -91,8 +93,15 @@ public override Task Send(PersistentResponse response)
});
}

protected override Task InitializeResponse(ITransportConnection connection)
protected internal override Task InitializeResponse(ITransportConnection connection)
{
int frameId;
if (!Int32.TryParse(Context.Request.QueryString["frameId"], out frameId))
{
// Invalid frameId passed in
throw new InvalidOperationException(Resources.Error_InvalidForeverFrameId);
}

return base.InitializeResponse(connection)
.Then(initScript =>
{
Expand All @@ -106,7 +115,7 @@ protected override Task InitializeResponse(ITransportConnection connection)
return Context.Response.Flush();
});
},
_initPrefix + Context.Request.QueryString["frameId"] + _initSuffix);
_initPrefix + frameId.ToString(CultureInfo.InvariantCulture) + _initSuffix);
}

private class HTMLTextWriter : StreamWriter
Expand Down
Expand Up @@ -168,7 +168,7 @@ public virtual Task Send(object value)
});
}

protected virtual Task InitializeResponse(ITransportConnection connection)
protected internal virtual Task InitializeResponse(ITransportConnection connection)
{
return TaskAsyncHelper.Empty;
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public override Task Send(PersistentResponse response)
});
}

protected override Task InitializeResponse(ITransportConnection connection)
protected internal override Task InitializeResponse(ITransportConnection connection)
{
return base.InitializeResponse(connection)
.Then(() =>
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -24,6 +25,20 @@ public void ForeverFrameTransportEscapesTags()
AssertEscaped(fft, response, "<p>ELLO</p>", "\\u003cp\\u003eELLO\\u003c/p\\u003e");
}

[Fact]
public void ForeverFrameTransportThrowsOnInvalidFrameId()
{
var request = new Mock<IRequest>();
var qs = new NameValueCollection { { "frameId", "invalid" } };
request.Setup(r => r.QueryString).Returns(qs);
var response = new CustomResponse();
var context = new HostContext(request.Object, response);
var connection = new Mock<ITransportConnection>();
var fft = new ForeverFrameTransport(context, new DefaultDependencyResolver());

Assert.Throws(typeof(InvalidOperationException), () => fft.InitializeResponse(connection.Object));
}

private static void AssertEscaped(ForeverFrameTransport fft, CustomResponse response, string input, string expectedOutput)
{
fft.Send(input).Wait();
Expand Down

0 comments on commit b58782d

Please sign in to comment.