Skip to content

Commit f2b72b0

Browse files
Fix SignalR typescript tests with Chrome SameSite reaction (#25283)
* Fix Typescript tests * fixup
1 parent 2916f4b commit f2b72b0

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

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

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

171183
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
@@ -60,6 +60,7 @@ console.log(`Using SignalR HTTPS Server: '${ENDPOINT_BASE_HTTPS_URL}'`);
6060
console.log(`Jasmine DEFAULT_TIMEOUT_INTERVAL: ${jasmine.DEFAULT_TIMEOUT_INTERVAL}`);
6161

6262
export const ECHOENDPOINT_URL = ENDPOINT_BASE_URL + "/echo";
63+
export const HTTPS_ECHOENDPOINT_URL = ENDPOINT_BASE_HTTPS_URL + "/echo";
6364

6465
export function getHttpTransportTypes(): HttpTransportType[] {
6566
const transportTypes = [];
@@ -131,3 +132,14 @@ export function eachHttpClient(action: (transport: HttpClient) => void) {
131132
return action(t);
132133
});
133134
}
135+
136+
// Run test in Node or Chrome, but not on macOS
137+
export const shouldRunHttpsTests =
138+
// Need to have an HTTPS URL
139+
!!ENDPOINT_BASE_HTTPS_URL &&
140+
141+
// Run on Node, unless macOS
142+
(process && process.platform !== "darwin") &&
143+
144+
// Only run under Chrome browser
145+
(typeof navigator === "undefined" || navigator.userAgent.search("Chrome") !== -1);

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

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

77
import { HttpTransportType, IHttpConnectionOptions, TransferFormat } from "@microsoft/signalr";
8-
import { DEFAULT_TIMEOUT_INTERVAL, eachHttpClient, eachTransport, ECHOENDPOINT_URL } from "./Common";
8+
import { DEFAULT_TIMEOUT_INTERVAL, eachHttpClient, 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.
@@ -15,6 +15,8 @@ import "./LogBannerReporter";
1515

1616
jasmine.DEFAULT_TIMEOUT_INTERVAL = DEFAULT_TIMEOUT_INTERVAL;
1717

18+
const USED_ECHOENDPOINT_URL = shouldRunHttpsTests ? HTTPS_ECHOENDPOINT_URL : ECHOENDPOINT_URL;
19+
1820
const commonOptions: IHttpConnectionOptions = {
1921
logMessageContent: true,
2022
logger: TestLogger.instance,
@@ -23,7 +25,7 @@ const commonOptions: IHttpConnectionOptions = {
2325
describe("connection", () => {
2426
it("can connect to the server without specifying transport explicitly", (done) => {
2527
const message = "Hello World!";
26-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
28+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
2729
...commonOptions,
2830
});
2931

@@ -53,7 +55,7 @@ describe("connection", () => {
5355
const message = "Hello World!";
5456
// the url should be resolved relative to the document.location.host
5557
// and the leading '/' should be automatically added to the url
56-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
58+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
5759
...commonOptions,
5860
httpClient,
5961
transport: transportType,
@@ -83,7 +85,7 @@ describe("connection", () => {
8385
const message = "Hello World!";
8486

8587
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is not set.
86-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
88+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
8789
httpClient,
8890
logger: TestLogger.instance,
8991
transport: transportType,
@@ -119,7 +121,7 @@ describe("connection", () => {
119121
const message = "Hello World!";
120122

121123
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is set to true (even if commonOptions changes).
122-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
124+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
123125
httpClient,
124126
logMessageContent: true,
125127
logger: TestLogger.instance,
@@ -167,7 +169,7 @@ describe("connection", () => {
167169
const message = "Hello World!";
168170

169171
// The server will set some response headers for the '/negotiate' endpoint
170-
const connection = new HttpConnection(ECHOENDPOINT_URL, {
172+
const connection = new HttpConnection(USED_ECHOENDPOINT_URL, {
171173
...commonOptions,
172174
httpClient,
173175
transport: transportType,

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AbortError, DefaultHttpClient, HttpClient, HttpRequest, HttpResponse, H
88
import { MessagePackHubProtocol } from "@microsoft/signalr-protocol-msgpack";
99
import { getUserAgentHeader, Platform } from "@microsoft/signalr/dist/esm/Utils";
1010

11-
import { DEFAULT_TIMEOUT_INTERVAL, eachTransport, eachTransportAndProtocolAndHttpClient, ENDPOINT_BASE_HTTPS_URL, ENDPOINT_BASE_URL } from "./Common";
11+
import { DEFAULT_TIMEOUT_INTERVAL, eachTransport, eachTransportAndProtocolAndHttpClient, ENDPOINT_BASE_HTTPS_URL, ENDPOINT_BASE_URL, shouldRunHttpsTests } from "./Common";
1212
import "./LogBannerReporter";
1313
import { TestLogger } from "./TestLogger";
1414

@@ -18,6 +18,7 @@ import * as RX from "rxjs";
1818

1919
const TESTHUBENDPOINT_URL = ENDPOINT_BASE_URL + "/testhub";
2020
const TESTHUBENDPOINT_HTTPS_URL = ENDPOINT_BASE_HTTPS_URL ? (ENDPOINT_BASE_HTTPS_URL + "/testhub") : undefined;
21+
const HTTPORHTTPS_TESTHUBENDPOINT_URL = shouldRunHttpsTests ? TESTHUBENDPOINT_HTTPS_URL : TESTHUBENDPOINT_URL;
2122

2223
const TESTHUB_NOWEBSOCKETS_ENDPOINT_URL = ENDPOINT_BASE_URL + "/testhub-nowebsockets";
2324
const TESTHUB_REDIRECT_ENDPOINT_URL = ENDPOINT_BASE_URL + "/redirect?numRedirects=0&baseUrl=" + ENDPOINT_BASE_URL;
@@ -28,17 +29,6 @@ const commonOptions: IHttpConnectionOptions = {
2829
logMessageContent: true,
2930
};
3031

31-
// Run test in Node or Chrome, but not on macOS
32-
const shouldRunHttpsTests =
33-
// Need to have an HTTPS URL
34-
!!TESTHUBENDPOINT_HTTPS_URL &&
35-
36-
// Run on Node, unless macOS
37-
(process && process.platform !== "darwin") &&
38-
39-
// Only run under Chrome browser
40-
(typeof navigator === "undefined" || navigator.userAgent.search("Chrome") !== -1);
41-
4232
function getConnectionBuilder(transportType?: HttpTransportType, url?: string, options?: IHttpConnectionOptions): HubConnectionBuilder {
4333
let actualOptions: IHttpConnectionOptions = options || {};
4434
if (transportType) {
@@ -599,7 +589,7 @@ describe("hubConnection", () => {
599589
}
600590

601591
it("preserves cookies between requests", async (done) => {
602-
const hubConnection = getConnectionBuilder(transportType).build();
592+
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
603593
await hubConnection.start();
604594
const cookieValue = await hubConnection.invoke<string>("GetCookie", "testCookie");
605595
const cookieValue2 = await hubConnection.invoke<string>("GetCookie", "testCookie2");
@@ -610,7 +600,7 @@ describe("hubConnection", () => {
610600
});
611601

612602
it("expired cookies are not preserved", async (done) => {
613-
const hubConnection = getConnectionBuilder(transportType).build();
603+
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
614604
await hubConnection.start();
615605
const cookieValue = await hubConnection.invoke<string>("GetCookie", "expiredCookie");
616606
expect(cookieValue).toBeNull();

0 commit comments

Comments
 (0)