Skip to content

Commit fa8eb31

Browse files
committed
Fix SignalR typescript tests with Chrome SameSite reaction (#25283)
* Fix Typescript tests * fixup
1 parent c75b3f7 commit fa8eb31

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

src/SignalR/clients/ts/FunctionalTests/Startup.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
160160
{
161161
if (context.Request.Path.Value.Contains("/negotiate"))
162162
{
163-
context.Response.Cookies.Append("testCookie", "testValue");
164-
context.Response.Cookies.Append("testCookie2", "testValue2");
165-
context.Response.Cookies.Append("expiredCookie", "doesntmatter", new CookieOptions() { Expires = DateTimeOffset.Now.AddHours(-1) });
163+
var cookieOptions = new CookieOptions();
164+
var expiredCookieOptions = new CookieOptions() { Expires = DateTimeOffset.Now.AddHours(-1) };
165+
if (context.Request.IsHttps)
166+
{
167+
cookieOptions.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
168+
cookieOptions.Secure = true;
169+
170+
expiredCookieOptions.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None;
171+
expiredCookieOptions.Secure = true;
172+
}
173+
context.Response.Cookies.Append("testCookie", "testValue", cookieOptions);
174+
context.Response.Cookies.Append("testCookie2", "testValue2", cookieOptions);
175+
176+
cookieOptions.Expires = DateTimeOffset.Now.AddHours(-1);
177+
context.Response.Cookies.Append("expiredCookie", "doesntmatter", expiredCookieOptions);
166178
}
167179

168180
await next.Invoke();

src/SignalR/clients/ts/FunctionalTests/ts/Common.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ console.log(`Using SignalR HTTPS Server: '${ENDPOINT_BASE_HTTPS_URL}'`);
5454
console.log(`Jasmine DEFAULT_TIMEOUT_INTERVAL: ${jasmine.DEFAULT_TIMEOUT_INTERVAL}`);
5555

5656
export const ECHOENDPOINT_URL = ENDPOINT_BASE_URL + "/echo";
57+
export const HTTPS_ECHOENDPOINT_URL = ENDPOINT_BASE_HTTPS_URL + "/echo";
5758

5859
export function getHttpTransportTypes(): HttpTransportType[] {
5960
const transportTypes = [];
@@ -100,3 +101,14 @@ export function eachTransportAndProtocol(action: (transport: HttpTransportType,
100101
export function getGlobalObject(): any {
101102
return typeof window !== "undefined" ? window : global;
102103
}
104+
105+
// Run test in Node or Chrome, but not on macOS
106+
export const shouldRunHttpsTests =
107+
// Need to have an HTTPS URL
108+
!!ENDPOINT_BASE_HTTPS_URL &&
109+
110+
// Run on Node, unless macOS
111+
(process && process.platform !== "darwin") &&
112+
113+
// Only run under Chrome browser
114+
(typeof navigator === "undefined" || navigator.userAgent.search("Chrome") !== -1);

src/SignalR/clients/ts/FunctionalTests/ts/ConnectionTests.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
// tslint:disable:no-floating-promises
66

77
import { HttpTransportType, IHttpConnectionOptions, TransferFormat } from "@microsoft/signalr";
8-
import { eachTransport, ECHOENDPOINT_URL } from "./Common";
8+
import { eachTransport, ECHOENDPOINT_URL, HTTPS_ECHOENDPOINT_URL, shouldRunHttpsTests } from "./Common";
99
import { TestLogger } from "./TestLogger";
1010

1111
// We want to continue testing HttpConnection, but we don't export it anymore. So just pull it in directly from the source file.
1212
import { HttpConnection } from "@microsoft/signalr/dist/esm/HttpConnection";
1313
import "./LogBannerReporter";
1414

15+
const USED_ECHOENDPOINT_URL = shouldRunHttpsTests ? HTTPS_ECHOENDPOINT_URL : ECHOENDPOINT_URL;
16+
1517
const commonOptions: IHttpConnectionOptions = {
1618
logMessageContent: true,
1719
logger: TestLogger.instance,
@@ -20,7 +22,7 @@ const commonOptions: IHttpConnectionOptions = {
2022
describe("connection", () => {
2123
it("can connect to the server without specifying transport explicitly", (done) => {
2224
const message = "Hello World!";
23-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
25+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
2426
...commonOptions,
2527
});
2628

@@ -49,7 +51,7 @@ describe("connection", () => {
4951
const message = "Hello World!";
5052
// the url should be resolved relative to the document.location.host
5153
// and the leading '/' should be automatically added to the url
52-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
54+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
5355
...commonOptions,
5456
transport: transportType,
5557
});
@@ -78,7 +80,7 @@ describe("connection", () => {
7880
const message = "Hello World!";
7981

8082
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is not set.
81-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
83+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
8284
logger: TestLogger.instance,
8385
transport: transportType,
8486
});
@@ -113,7 +115,7 @@ describe("connection", () => {
113115
const message = "Hello World!";
114116

115117
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is set to true (even if commonOptions changes).
116-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
118+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
117119
logMessageContent: true,
118120
logger: TestLogger.instance,
119121
transport: transportType,

src/SignalR/clients/ts/FunctionalTests/ts/HubConnectionTests.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { AbortError, DefaultHttpClient, HttpClient, HttpRequest, HttpResponse, HttpTransportType, HubConnectionBuilder, IHttpConnectionOptions, JsonHubProtocol, NullLogger } from "@microsoft/signalr";
88
import { MessagePackHubProtocol } from "@microsoft/signalr-protocol-msgpack";
99

10-
import { eachTransport, eachTransportAndProtocol, ENDPOINT_BASE_HTTPS_URL, ENDPOINT_BASE_URL } from "./Common";
10+
import { eachTransport, eachTransportAndProtocol, ENDPOINT_BASE_HTTPS_URL, ENDPOINT_BASE_URL, shouldRunHttpsTests } from "./Common";
1111
import "./LogBannerReporter";
1212
import { TestLogger } from "./TestLogger";
1313

@@ -17,6 +17,7 @@ import * as RX from "rxjs";
1717

1818
const TESTHUBENDPOINT_URL = ENDPOINT_BASE_URL + "/testhub";
1919
const TESTHUBENDPOINT_HTTPS_URL = ENDPOINT_BASE_HTTPS_URL ? (ENDPOINT_BASE_HTTPS_URL + "/testhub") : undefined;
20+
const HTTPORHTTPS_TESTHUBENDPOINT_URL = shouldRunHttpsTests ? TESTHUBENDPOINT_HTTPS_URL : TESTHUBENDPOINT_URL;
2021

2122
const TESTHUB_NOWEBSOCKETS_ENDPOINT_URL = ENDPOINT_BASE_URL + "/testhub-nowebsockets";
2223
const TESTHUB_REDIRECT_ENDPOINT_URL = ENDPOINT_BASE_URL + "/redirect?numRedirects=0&baseUrl=" + ENDPOINT_BASE_URL;
@@ -25,17 +26,6 @@ const commonOptions: IHttpConnectionOptions = {
2526
logMessageContent: true,
2627
};
2728

28-
// Run test in Node or Chrome, but not on macOS
29-
const shouldRunHttpsTests =
30-
// Need to have an HTTPS URL
31-
!!TESTHUBENDPOINT_HTTPS_URL &&
32-
33-
// Run on Node, unless macOS
34-
(process && process.platform !== "darwin") &&
35-
36-
// Only run under Chrome browser
37-
(typeof navigator === "undefined" || navigator.userAgent.search("Chrome") !== -1);
38-
3929
function getConnectionBuilder(transportType?: HttpTransportType, url?: string, options?: IHttpConnectionOptions): HubConnectionBuilder {
4030
let actualOptions: IHttpConnectionOptions = options || {};
4131
if (transportType) {
@@ -690,7 +680,7 @@ describe("hubConnection", () => {
690680
}
691681

692682
it("preserves cookies between requests", async (done) => {
693-
const hubConnection = getConnectionBuilder(transportType).build();
683+
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
694684
await hubConnection.start();
695685
const cookieValue = await hubConnection.invoke<string>("GetCookie", "testCookie");
696686
const cookieValue2 = await hubConnection.invoke<string>("GetCookie", "testCookie2");
@@ -701,7 +691,7 @@ describe("hubConnection", () => {
701691
});
702692

703693
it("expired cookies are not preserved", async (done) => {
704-
const hubConnection = getConnectionBuilder(transportType).build();
694+
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
705695
await hubConnection.start();
706696
const cookieValue = await hubConnection.invoke<string>("GetCookie", "expiredCookie");
707697
expect(cookieValue).toBeNull();

0 commit comments

Comments
 (0)