Skip to content

Commit 696b3bd

Browse files
[Backport] Fix browser cookie tests to only run with HTTPS (#39681)
* Fix browser cookie tests to only run with HTTPS (#39665) * fixup
1 parent f5cec0e commit 696b3bd

File tree

1 file changed

+42
-65
lines changed

1 file changed

+42
-65
lines changed

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

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -413,25 +413,27 @@ describe("hubConnection", () => {
413413
});
414414
});
415415

416-
it("closed with error or start fails if hub cannot be created", async (done) => {
416+
it("closed with error or start fails if hub cannot be created", async () => {
417417
const hubConnection = getConnectionBuilder(transportType, ENDPOINT_BASE_URL + "/uncreatable")
418418
.withHubProtocol(protocol)
419419
.build();
420420

421421
const expectedErrorMessage = "Server returned an error on close: Connection closed with an error. InvalidOperationException: Unable to resolve service for type 'System.Object' while attempting to activate 'FunctionalTests.UncreatableHub'.";
422422

423+
const closePromise = new PromiseSource();
423424
// Either start will fail or onclose will be called. Never both.
424425
hubConnection.onclose((error) => {
425426
expect(error!.message).toEqual(expectedErrorMessage);
426-
done();
427+
closePromise.resolve();
427428
});
428429

429430
try {
430431
await hubConnection.start();
431432
} catch (error) {
432433
expect(error!.message).toEqual(expectedErrorMessage);
433-
done();
434+
closePromise.resolve();
434435
}
436+
await closePromise;
435437
});
436438

437439
it("can handle different types", (done) => {
@@ -565,7 +567,7 @@ describe("hubConnection", () => {
565567
});
566568
});
567569

568-
it("can stream from client to server with rxjs", async (done) => {
570+
it("can stream from client to server with rxjs", async () => {
569571
const hubConnection = getConnectionBuilder(transportType)
570572
.withHubProtocol(protocol)
571573
.build();
@@ -579,10 +581,9 @@ describe("hubConnection", () => {
579581
subject.complete();
580582
expect(await resultPromise).toBe("Hello world!");
581583
await hubConnection.stop();
582-
done();
583584
});
584585

585-
it("can stream from client to server and close with error with rxjs", async (done) => {
586+
it("can stream from client to server and close with error with rxjs", async () => {
586587
const hubConnection = getConnectionBuilder(transportType)
587588
.withHubProtocol(protocol)
588589
.build();
@@ -602,15 +603,14 @@ describe("hubConnection", () => {
602603
} finally {
603604
await hubConnection.stop();
604605
}
605-
done();
606606
});
607607
});
608608
});
609609

610610
eachTransport((transportType) => {
611611
describe("over " + HttpTransportType[transportType] + " transport", () => {
612612

613-
it("can connect to hub with authorization", async (done) => {
613+
it("can connect to hub with authorization", async () => {
614614
const message = "你好,世界!";
615615

616616
try {
@@ -620,9 +620,10 @@ describe("hubConnection", () => {
620620
accessTokenFactory: () => jwtToken,
621621
}).build();
622622

623+
const closePromise = new PromiseSource();
623624
hubConnection.onclose((error) => {
624625
expect(error).toBe(undefined);
625-
done();
626+
closePromise.resolve();
626627
});
627628
await hubConnection.start();
628629
const response = await hubConnection.invoke("Echo", message);
@@ -631,24 +632,24 @@ describe("hubConnection", () => {
631632

632633
await hubConnection.stop();
633634

634-
done();
635+
await closePromise;
635636
} catch (err) {
636637
fail(err);
637-
done();
638638
}
639639
});
640640

641-
it("can connect to hub with authorization using async token factory", async (done) => {
641+
it("can connect to hub with authorization using async token factory", async () => {
642642
const message = "你好,世界!";
643643

644644
try {
645645
const hubConnection = getConnectionBuilder(transportType, ENDPOINT_BASE_URL + "/authorizedhub", {
646646
accessTokenFactory: () => getJwtToken(ENDPOINT_BASE_URL + "/generateJwtToken"),
647647
}).build();
648648

649+
const closePromise = new PromiseSource();
649650
hubConnection.onclose((error) => {
650651
expect(error).toBe(undefined);
651-
done();
652+
closePromise.resolve();
652653
});
653654
await hubConnection.start();
654655
const response = await hubConnection.invoke("Echo", message);
@@ -657,48 +658,49 @@ describe("hubConnection", () => {
657658

658659
await hubConnection.stop();
659660

660-
done();
661+
await closePromise;
661662
} catch (err) {
662663
fail(err);
663-
done();
664664
}
665665
});
666666

667667
if (transportType !== HttpTransportType.LongPolling) {
668-
it("terminates if no messages received within timeout interval", async (done) => {
668+
it("terminates if no messages received within timeout interval", async () => {
669669
const hubConnection = getConnectionBuilder(transportType).build();
670670
hubConnection.serverTimeoutInMilliseconds = 100;
671671

672+
const closePromise = new PromiseSource();
672673
hubConnection.onclose((error) => {
673674
expect(error).toEqual(new Error("Server timeout elapsed without receiving a message from the server."));
674-
done();
675+
closePromise.resolve();
675676
});
676677

677678
await hubConnection.start();
679+
await closePromise;
678680
});
679681
}
680682

681-
it("preserves cookies between requests", async (done) => {
682-
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
683-
await hubConnection.start();
684-
const cookieValue = await hubConnection.invoke<string>("GetCookie", "testCookie");
685-
const cookieValue2 = await hubConnection.invoke<string>("GetCookie", "testCookie2");
686-
expect(cookieValue).toEqual("testValue");
687-
expect(cookieValue2).toEqual("testValue2");
688-
await hubConnection.stop();
689-
done();
690-
});
683+
if (shouldRunHttpsTests) {
684+
it("preserves cookies between requests", async () => {
685+
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
686+
await hubConnection.start();
687+
const cookieValue = await hubConnection.invoke<string>("GetCookie", "testCookie");
688+
const cookieValue2 = await hubConnection.invoke<string>("GetCookie", "testCookie2");
689+
expect(cookieValue).toEqual("testValue");
690+
expect(cookieValue2).toEqual("testValue2");
691+
await hubConnection.stop();
692+
});
693+
}
691694

692-
it("expired cookies are not preserved", async (done) => {
695+
it("expired cookies are not preserved", async () => {
693696
const hubConnection = getConnectionBuilder(transportType, HTTPORHTTPS_TESTHUBENDPOINT_URL).build();
694697
await hubConnection.start();
695698
const cookieValue = await hubConnection.invoke<string>("GetCookie", "expiredCookie");
696699
expect(cookieValue).toBeNull();
697700
await hubConnection.stop();
698-
done();
699701
});
700702

701-
it("can reconnect", async (done) => {
703+
it("can reconnect", async () => {
702704
try {
703705
const reconnectingPromise = new PromiseSource();
704706
const reconnectedPromise = new PromiseSource<string | undefined>();
@@ -731,16 +733,13 @@ describe("hubConnection", () => {
731733
expect(response).toEqual("test");
732734

733735
await hubConnection.stop();
734-
735-
done();
736736
} catch (err) {
737737
fail(err);
738-
done();
739738
}
740739
});
741740
});
742741

743-
it("can change url in reconnecting state", async (done) => {
742+
it("can change url in reconnecting state", async () => {
744743
try {
745744
const reconnectingPromise = new PromiseSource();
746745
const hubConnection = getConnectionBuilder(transportType)
@@ -762,16 +761,13 @@ describe("hubConnection", () => {
762761
expect(hubConnection.baseUrl).toBe("http://example123.com");
763762

764763
await hubConnection.stop();
765-
766-
done();
767764
} catch (err) {
768765
fail(err);
769-
done();
770766
}
771767
});
772768
});
773769

774-
it("can reconnect after negotiate redirect", async (done) => {
770+
it("can reconnect after negotiate redirect", async () => {
775771
try {
776772
const reconnectingPromise = new PromiseSource();
777773
const reconnectedPromise = new PromiseSource<string | undefined>();
@@ -809,15 +805,12 @@ describe("hubConnection", () => {
809805
expect(postReconnectRedirects).toBeGreaterThan(preReconnectRedirects);
810806

811807
await hubConnection.stop();
812-
813-
done();
814808
} catch (err) {
815809
fail(err);
816-
done();
817810
}
818811
});
819812

820-
it("can reconnect after skipping negotiation", async (done) => {
813+
it("can reconnect after skipping negotiation", async () => {
821814
try {
822815
const reconnectingPromise = new PromiseSource();
823816
const reconnectedPromise = new PromiseSource<string | undefined>();
@@ -852,15 +845,12 @@ describe("hubConnection", () => {
852845
expect(response).toEqual("test");
853846

854847
await hubConnection.stop();
855-
856-
done();
857848
} catch (err) {
858849
fail(err);
859-
done();
860850
}
861851
});
862852

863-
it("connection id matches server side connection id", async (done) => {
853+
it("connection id matches server side connection id", async () => {
864854
try {
865855
const reconnectingPromise = new PromiseSource();
866856
const reconnectedPromise = new PromiseSource<string | undefined>();
@@ -899,15 +889,12 @@ describe("hubConnection", () => {
899889

900890
await hubConnection.stop();
901891
expect(hubConnection.connectionId).toBeNull();
902-
903-
done();
904892
} catch (err) {
905893
fail(err);
906-
done();
907894
}
908895
});
909896

910-
it("connection id is alwys null is negotiation is skipped", async (done) => {
897+
it("connection id is alwys null is negotiation is skipped", async () => {
911898
try {
912899
const hubConnection = getConnectionBuilder(
913900
HttpTransportType.WebSockets,
@@ -925,16 +912,13 @@ describe("hubConnection", () => {
925912
await hubConnection.stop();
926913

927914
expect(hubConnection.connectionId).toBeNull();
928-
929-
done();
930915
} catch (err) {
931916
fail(err);
932-
done();
933917
}
934918
});
935919

936920
if (typeof EventSource !== "undefined") {
937-
it("allows Server-Sent Events when negotiating for JSON protocol", async (done) => {
921+
it("allows Server-Sent Events when negotiating for JSON protocol", async () => {
938922
const hubConnection = getConnectionBuilder(undefined, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)
939923
.withHubProtocol(new JsonHubProtocol())
940924
.build();
@@ -946,14 +930,13 @@ describe("hubConnection", () => {
946930
expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("ServerSentEvents");
947931

948932
await hubConnection.stop();
949-
done();
950933
} catch (e) {
951934
fail(e);
952935
}
953936
});
954937
}
955938

956-
it("skips Server-Sent Events when negotiating for MessagePack protocol", async (done) => {
939+
it("skips Server-Sent Events when negotiating for MessagePack protocol", async () => {
957940
const hubConnection = getConnectionBuilder(undefined, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)
958941
.withHubProtocol(new MessagePackHubProtocol())
959942
.build();
@@ -965,16 +948,14 @@ describe("hubConnection", () => {
965948
expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("LongPolling");
966949

967950
await hubConnection.stop();
968-
done();
969951
} catch (e) {
970952
fail(e);
971953
}
972954
});
973955

974-
it("transport falls back from WebSockets to SSE or LongPolling", async (done) => {
956+
it("transport falls back from WebSockets to SSE or LongPolling", async () => {
975957
// Skip test on Node as there will always be a WebSockets implementation on Node
976958
if (typeof window === "undefined") {
977-
done();
978959
return;
979960
}
980961

@@ -1000,11 +981,10 @@ describe("hubConnection", () => {
1000981
fail(e);
1001982
} finally {
1002983
(window as any).WebSocket = oldWebSocket;
1003-
done();
1004984
}
1005985
});
1006986

1007-
it("over LongPolling it sends DELETE request and waits for poll to terminate", async (done) => {
987+
it("over LongPolling it sends DELETE request and waits for poll to terminate", async () => {
1008988
// Create an HTTP client to capture the poll
1009989
const defaultClient = new DefaultHttpClient(TestLogger.instance);
1010990

@@ -1052,14 +1032,12 @@ describe("hubConnection", () => {
10521032
} catch (e) {
10531033
fail(e);
10541034
} finally {
1055-
done();
10561035
}
10571036
});
10581037

1059-
it("populates the Content-Type header when sending XMLHttpRequest", async (done) => {
1038+
it("populates the Content-Type header when sending XMLHttpRequest", async () => {
10601039
// Skip test on Node as this header isn't set (it was added for React-Native)
10611040
if (typeof window === "undefined") {
1062-
done();
10631041
return;
10641042
}
10651043
const hubConnection = getConnectionBuilder(HttpTransportType.LongPolling, TESTHUB_NOWEBSOCKETS_ENDPOINT_URL)
@@ -1075,7 +1053,6 @@ describe("hubConnection", () => {
10751053
expect(await hubConnection.invoke("GetContentTypeHeader")).toEqual("text/plain;charset=UTF-8");
10761054

10771055
await hubConnection.stop();
1078-
done();
10791056
} catch (e) {
10801057
fail(e);
10811058
}

0 commit comments

Comments
 (0)