Fix tests when running on non-US locale (#2501)

This commit is contained in:
Andrew Stanton-Nurse 2018-06-15 15:19:00 -07:00 committed by GitHub
parent b124cb5283
commit 5b22616a63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 5 deletions

View File

@ -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<OperationCanceledException>(() => startTask.OrTimeout());
Assert.Equal("The operation was canceled.", exception.Message);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => startTask.OrTimeout());
// We aren't worried about the exact message and it's localized so asserting it is non-trivial.
}
finally
{

View File

@ -114,7 +114,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
await hubConnection.StartAsync().OrTimeout();
var exception = Assert.IsType<TimeoutException>(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<TimeoutException>(() => 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);
}
}

View File

@ -363,7 +363,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
var sseTransport = new ServerSentEventsTransport(httpClient, loggerFactory);
var ex = await Assert.ThrowsAsync<ArgumentException>(() => 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());
}
}

View File

@ -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);
}
}
}