From 5b22616a630646199aaf0be040bb20a85d184cbe Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Fri, 15 Jun 2018 15:19:00 -0700 Subject: [PATCH] Fix tests when running on non-US locale (#2501) --- .../HubConnectionTests.ConnectionLifecycle.cs | 8 ++++-- .../HubConnectionTests.cs | 8 ++++-- .../ServerSentEventsTransportTests.cs | 4 ++- .../ExceptionMessageExtensions.cs | 25 +++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/Microsoft.AspNetCore.SignalR.Tests.Utils/ExceptionMessageExtensions.cs diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs index ae0ae74cd1..61e34fab5c 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.ConnectionLifecycle.cs @@ -1,3 +1,6 @@ +// 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; using System.Collections.Generic; using System.Linq; @@ -508,8 +511,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests try { var startTask = hubConnection.StartAsync(cts.Token); - var exception = await Assert.ThrowsAnyAsync(() => startTask.OrTimeout()); - Assert.Equal("The operation was canceled.", exception.Message); + await Assert.ThrowsAnyAsync(() => startTask.OrTimeout()); + + // We aren't worried about the exact message and it's localized so asserting it is non-trivial. } finally { diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs index 4e80ebd6c5..4951413baf 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HubConnectionTests.cs @@ -114,7 +114,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests await hubConnection.StartAsync().OrTimeout(); var exception = Assert.IsType(await closeTcs.Task.OrTimeout()); - Assert.Equal("Server timeout (100.00ms) elapsed without receiving a message from the server.", exception.Message); + + // We use an interpolated string so the tests are accurate on non-US machines. + Assert.Equal($"Server timeout ({hubConnection.ServerTimeout.TotalMilliseconds:0.00}ms) elapsed without receiving a message from the server.", exception.Message); } [Fact] @@ -137,7 +139,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests var invokeTask = hubConnection.InvokeAsync("Method").OrTimeout(); var exception = await Assert.ThrowsAsync(() => invokeTask); - Assert.Equal("Server timeout (2000.00ms) elapsed without receiving a message from the server.", exception.Message); + + // We use an interpolated string so the tests are accurate on non-US machines. + Assert.Equal($"Server timeout ({hubConnection.ServerTimeout.TotalMilliseconds:0.00}ms) elapsed without receiving a message from the server.", exception.Message); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/ServerSentEventsTransportTests.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/ServerSentEventsTransportTests.cs index cc4ddc0b73..0830678222 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/ServerSentEventsTransportTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/ServerSentEventsTransportTests.cs @@ -363,7 +363,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests var sseTransport = new ServerSentEventsTransport(httpClient, loggerFactory); var ex = await Assert.ThrowsAsync(() => sseTransport.StartAsync(new Uri("http://fakeuri.org"), TransferFormat.Binary).OrTimeout()); - Assert.Equal($"The 'Binary' transfer format is not supported by this transport.{Environment.NewLine}Parameter name: transferFormat", ex.Message); + + Assert.Equal("transferFormat", ex.ParamName); + Assert.Equal($"The 'Binary' transfer format is not supported by this transport.", ex.GetLocalizationSafeMessage()); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ExceptionMessageExtensions.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ExceptionMessageExtensions.cs new file mode 100644 index 0000000000..b7f904c8a4 --- /dev/null +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/ExceptionMessageExtensions.cs @@ -0,0 +1,25 @@ +// 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; + +namespace Microsoft.AspNetCore.SignalR.Tests +{ + public static class ExceptionMessageExtensions + { + public static string GetLocalizationSafeMessage(this ArgumentException argEx) + { + // Strip off the last line since it's "Parameter Name: [parameterName]" and: + // 1. We verify the parameter name separately + // 2. It is localized, so we don't want our tests to break in non-US environments + var message = argEx.Message; + var lastNewline = message.LastIndexOf(Environment.NewLine, StringComparison.Ordinal); + if (lastNewline < 0) + { + return message; + } + + return message.Substring(0, lastNewline); + } + } +}