From 2cc72d72cd6b32e596fa7f72a3623cc7d1e9eaf6 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Wed, 6 Sep 2017 11:56:38 -0700 Subject: [PATCH 1/3] Logging traffic at Trace level --- .../ILogger.ts | 3 +- .../Transports.ts | 6 +-- .../wwwroot/js/connectionTests.js | 2 +- .../wwwroot/js/hubConnectionTests.js | 45 ++++++++++++++++--- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/ILogger.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/ILogger.ts index b45dadf4ef..b04154cf92 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/ILogger.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/ILogger.ts @@ -2,7 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. export enum LogLevel { - Information = 0, + Trace = 0, + Information, Warning, Error, None diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts index ecadd77ffa..fe740cf66f 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts @@ -54,7 +54,7 @@ export class WebSocketTransport implements ITransport { }; webSocket.onmessage = (message: MessageEvent) => { - this.logger.log(LogLevel.Information, `(WebSockets transport) data received: ${message.data}`); + this.logger.log(LogLevel.Trace, `(WebSockets transport) data received: ${message.data}`); if (this.onDataReceived) { this.onDataReceived(message.data); } @@ -118,7 +118,7 @@ export class ServerSentEventsTransport implements ITransport { eventSource.onmessage = (e: MessageEvent) => { if (this.onDataReceived) { try { - this.logger.log(LogLevel.Information, `(SSE transport) data received: ${e.data}`); + this.logger.log(LogLevel.Trace, `(SSE transport) data received: ${e.data}`); this.onDataReceived(e.data); } catch (error) { if (this.onClosed) { @@ -208,7 +208,7 @@ export class LongPollingTransport implements ITransport { : pollXhr.response; if (response) { - this.logger.log(LogLevel.Information, `(LongPolling transport) data received: ${response}`); + this.logger.log(LogLevel.Trace, `(LongPolling transport) data received: ${response}`); this.onDataReceived(response); } else { diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js index d2c6e59911..da387cc182 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js @@ -34,7 +34,7 @@ describe('connection', function () { var message = "Hello World!"; var connection = new signalR.HttpConnection(ECHOENDPOINT_URL, { transport: transportType, - logger: signalR.LogLevel.Information + logging: signalR.LogLevel.Trace }); var received = ""; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js index d91a9d505d..97b9d3d508 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js @@ -11,7 +11,13 @@ describe('hubConnection', function () { it('can invoke server method and receive result', function (done) { var message = "你好,世界!"; - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); + + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options); hubConnection.onClosed = function (error) { expect(error).toBe(undefined); done(); @@ -32,7 +38,13 @@ describe('hubConnection', function () { }); it('can stream server method and receive result', function (done) { - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); + + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options); hubConnection.onClosed = function (error) { expect(error).toBe(undefined); @@ -62,7 +74,12 @@ describe('hubConnection', function () { it('rethrows an exception from the server when invoking', function (done) { var errorMessage = "An error occurred."; - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options); hubConnection.start().then(function () { hubConnection.invoke('ThrowException', errorMessage).then(function () { @@ -83,8 +100,12 @@ describe('hubConnection', function () { it('rethrows an exception from the server when streaming', function (done) { var errorMessage = "An error occurred."; - - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options); hubConnection.start().then(function () { hubConnection.stream('ThrowException', errorMessage).subscribe({ @@ -106,7 +127,12 @@ describe('hubConnection', function () { }); it('can receive server calls', function (done) { - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options); var message = "你好 SignalR!"; @@ -134,7 +160,12 @@ describe('hubConnection', function () { ServerSentEvents: "Error occurred" }; - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', { transport: transportType }), { protocol: protocol}); + var options = { + transport: transportType, + protocol: protocol, + logging: signalR.LogLevel.Trace + }; + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', options), options); hubConnection.onClosed = function (error) { expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]); From b4c61b6c2dbb819451b9e86d85a1bb27722e88ef Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Wed, 6 Sep 2017 12:22:59 -0700 Subject: [PATCH 2/3] Client side method names should be case insensitive --- .../HubConnection.ts | 4 ++-- .../wwwroot/js/hubConnectionTests.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index 4c257c6d20..8c36667fc3 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -75,7 +75,7 @@ export class HubConnection { } private invokeClientMethod(invocationMessage: InvocationMessage) { - let method = this.methods.get(invocationMessage.target); + let method = this.methods.get(invocationMessage.target.toLowerCase()); if (method) { method.apply(this, invocationMessage.arguments); if (!invocationMessage.nonblocking) { @@ -202,7 +202,7 @@ export class HubConnection { } on(methodName: string, method: (...args: any[]) => void) { - this.methods.set(methodName, method); + this.methods.set(methodName.toLowerCase(), method); } set onClosed(callback: ConnectionClosed) { diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js index 97b9d3d508..f58435025a 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js @@ -136,7 +136,12 @@ describe('hubConnection', function () { var message = "你好 SignalR!"; - hubConnection.on("Message", function (msg) { + // client side method names are case insensitive + var methodName = 'message'; + var idx = Math.floor(Math.random() * (methodName.length - 1)); + methodName = methodName.substr(0, idx) + methodName[idx].toUpperCase() + methodName.substr(idx + 1); + + hubConnection.on(methodName, function (msg) { expect(msg).toBe(message); done(); }); From 9e614b6cc79da03fb0126c15a11322536fda9323 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Wed, 6 Sep 2017 13:51:22 -0700 Subject: [PATCH 3/3] Initializing HubOptions.JsonSerializationSettings to default settings --- .../HubOptions.cs | 2 +- .../Hubs.cs | 1 - ...gnalRDependencyInjectionExtensionsTests.cs | 23 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/Microsoft.AspNetCore.SignalR.Tests/SignalRDependencyInjectionExtensionsTests.cs diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs index 830056bbdd..b1d6f40cdd 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubOptions.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.SignalR { public class HubOptions { - public JsonSerializerSettings JsonSerializerSettings { get; set; } + public JsonSerializerSettings JsonSerializerSettings { get; set; } = new JsonSerializerSettings(); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Hubs.cs b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Hubs.cs index 33daaa4202..c95608b0cf 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Hubs.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/Hubs.cs @@ -4,7 +4,6 @@ using System; using System.Reactive.Linq; using System.Threading.Tasks; -using Microsoft.CSharp; namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests { diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/SignalRDependencyInjectionExtensionsTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/SignalRDependencyInjectionExtensionsTests.cs new file mode 100644 index 0000000000..a4f834f97d --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests/SignalRDependencyInjectionExtensionsTests.cs @@ -0,0 +1,23 @@ +// 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 Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Xunit; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public class SignalRDependencyInjectionExtensionsTests + { + [Fact] + public void JSonSerializerSettingsShouldNotBeNullInOptions() + { + var services = new ServiceCollection(); + services.AddOptions(); + services.AddSignalR(); + var serviceProvider = services.BuildServiceProvider(); + var hubOptions = serviceProvider.GetService>(); + Assert.NotNull(hubOptions.Value.JsonSerializerSettings); + } + } +}