Skip to content

Commit 99ae47b

Browse files
authored
Add ability to log message content (#1792)
1 parent 7c2d9e8 commit 99ae47b

File tree

8 files changed

+222
-74
lines changed

8 files changed

+222
-74
lines changed

SignalR.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1919
EndProjectSection
2020
EndProject
2121
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{C4BC9889-B49F-41B6-806B-F84941B2549B}"
22+
ProjectSection(SolutionItems) = preProject
23+
EndProjectSection
2224
EndProject
2325
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignalRSamples", "samples\SignalRSamples\SignalRSamples.csproj", "{C4AEAB04-F341-4539-B6C0-52368FB4BF9E}"
2426
EndProject

clients/ts/FunctionalTests/ts/ConnectionTests.ts

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
import { HttpConnection, LogLevel, TransferFormat, TransportType } from "@aspnet/signalr";
4+
import { HttpConnection, IHttpConnectionOptions, LogLevel, TransferFormat, TransportType } from "@aspnet/signalr";
55
import { eachTransport, ECHOENDPOINT_URL } from "./Common";
66
import { TestLogger } from "./TestLogger";
77

8+
const commonOptions: IHttpConnectionOptions = {
9+
logMessageContent: true,
10+
logger: TestLogger.instance,
11+
};
12+
813
describe("connection", () => {
914
it("can connect to the server without specifying transport explicitly", (done) => {
1015
const message = "Hello World!";
1116
const connection = new HttpConnection(ECHOENDPOINT_URL, {
12-
logger: TestLogger.instance,
17+
...commonOptions,
1318
});
1419

1520
let received = "";
@@ -34,33 +39,103 @@ describe("connection", () => {
3439
});
3540

3641
eachTransport((transportType) => {
37-
it("over " + TransportType[transportType] + " can send and receive messages", (done) => {
38-
const message = "Hello World!";
39-
// the url should be resolved relative to the document.location.host
40-
// and the leading '/' should be automatically added to the url
41-
const connection = new HttpConnection("echo", {
42-
logger: TestLogger.instance,
43-
transport: transportType,
42+
describe(`over ${TransportType[transportType]}`, () => {
43+
it("can send and receive messages", (done) => {
44+
const message = "Hello World!";
45+
// the url should be resolved relative to the document.location.host
46+
// and the leading '/' should be automatically added to the url
47+
const connection = new HttpConnection("echo", {
48+
...commonOptions,
49+
transport: transportType,
50+
});
51+
52+
let received = "";
53+
connection.onreceive = (data) => {
54+
received += data;
55+
if (data === message) {
56+
connection.stop();
57+
}
58+
};
59+
60+
connection.onclose = (error) => {
61+
expect(error).toBeUndefined();
62+
done();
63+
};
64+
65+
connection.start(TransferFormat.Text).then(() => {
66+
connection.send(message);
67+
}).catch((e) => {
68+
fail(e);
69+
done();
70+
});
4471
});
4572

46-
let received = "";
47-
connection.onreceive = (data) => {
48-
received += data;
49-
if (data === message) {
50-
connection.stop();
51-
}
52-
};
53-
54-
connection.onclose = (error) => {
55-
expect(error).toBeUndefined();
56-
done();
57-
};
58-
59-
connection.start(TransferFormat.Text).then(() => {
60-
connection.send(message);
61-
}).catch((e) => {
62-
fail(e);
63-
done();
73+
it("does not log content of messages sent or received by default", (done) => {
74+
const message = "Hello World!";
75+
76+
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is not set.
77+
const connection = new HttpConnection("echo", {
78+
transport: transportType,
79+
});
80+
81+
connection.onreceive = (data) => {
82+
if (data === message) {
83+
connection.stop();
84+
}
85+
};
86+
87+
connection.onclose = (error) => {
88+
// Search the logs for the message content
89+
for (const [_, logMessage] of TestLogger.instance.currentLog.messages) {
90+
expect(logMessage).not.toContain(message);
91+
}
92+
done();
93+
};
94+
95+
connection.start(TransferFormat.Text).then(() => {
96+
connection.send(message);
97+
}).catch((e) => {
98+
fail(e);
99+
done();
100+
});
101+
});
102+
103+
it("does log content of messages sent or received when enabled", (done) => {
104+
const message = "Hello World!";
105+
106+
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is set to true (even if commonOptions changes).
107+
const connection = new HttpConnection("echo", {
108+
logMessageContent: true,
109+
logger: TestLogger.instance,
110+
transport: transportType,
111+
});
112+
113+
connection.onreceive = (data) => {
114+
if (data === message) {
115+
connection.stop();
116+
}
117+
};
118+
119+
connection.onclose = (error) => {
120+
// Search the logs for the message content
121+
let matches = 0;
122+
for (const [_, logMessage] of TestLogger.instance.currentLog.messages) {
123+
if (logMessage.indexOf(message) !== -1) {
124+
matches += 1;
125+
}
126+
}
127+
128+
// One match for send, one for receive.
129+
expect(matches).toEqual(2);
130+
done();
131+
};
132+
133+
connection.start(TransferFormat.Text).then(() => {
134+
connection.send(message);
135+
}).catch((e) => {
136+
fail(e);
137+
done();
138+
});
64139
});
65140
});
66141
});

clients/ts/FunctionalTests/ts/HubConnectionTests.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
import { HubConnection, JsonHubProtocol, LogLevel, TransportType } from "@aspnet/signalr";
4+
import { HubConnection, IHubConnectionOptions, JsonHubProtocol, LogLevel, TransportType } from "@aspnet/signalr";
55
import { MessagePackHubProtocol } from "@aspnet/signalr-protocol-msgpack";
66

77
import { eachTransport, eachTransportAndProtocol } from "./Common";
@@ -10,14 +10,19 @@ import { TestLogger } from "./TestLogger";
1010
const TESTHUBENDPOINT_URL = "/testhub";
1111
const TESTHUB_NOWEBSOCKETS_ENDPOINT_URL = "/testhub-nowebsockets";
1212

13+
const commonOptions: IHubConnectionOptions = {
14+
logMessageContent: true,
15+
logger: TestLogger.instance,
16+
};
17+
1318
describe("hubConnection", () => {
1419
eachTransportAndProtocol((transportType, protocol) => {
1520
describe("using " + protocol.name + " over " + TransportType[transportType] + " transport", () => {
1621
it("can invoke server method and receive result", (done) => {
1722
const message = "你好,世界!";
1823

1924
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
20-
logger: TestLogger.instance,
25+
...commonOptions,
2126
protocol,
2227
transport: transportType,
2328
});
@@ -44,7 +49,7 @@ describe("hubConnection", () => {
4449
const message = "你好,世界!";
4550

4651
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
47-
logger: TestLogger.instance,
52+
...commonOptions,
4853
protocol,
4954
transport: transportType,
5055
});
@@ -67,7 +72,7 @@ describe("hubConnection", () => {
6772

6873
it("can invoke server method structural object and receive structural result", (done) => {
6974
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
70-
logger: TestLogger.instance,
75+
...commonOptions,
7176
protocol,
7277
transport: transportType,
7378
});
@@ -93,7 +98,7 @@ describe("hubConnection", () => {
9398

9499
it("can stream server method and receive result", (done) => {
95100
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
96-
logger: TestLogger.instance,
101+
...commonOptions,
97102
protocol,
98103
transport: transportType,
99104
});
@@ -127,7 +132,7 @@ describe("hubConnection", () => {
127132
it("rethrows an exception from the server when invoking", (done) => {
128133
const errorMessage = "An unexpected error occurred invoking 'ThrowException' on the server. InvalidOperationException: An error occurred.";
129134
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
130-
logger: TestLogger.instance,
135+
...commonOptions,
131136
protocol,
132137
transport: transportType,
133138
});
@@ -151,7 +156,7 @@ describe("hubConnection", () => {
151156

152157
it("throws an exception when invoking streaming method with invoke", (done) => {
153158
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
154-
logger: TestLogger.instance,
159+
...commonOptions,
155160
protocol,
156161
transport: transportType,
157162
});
@@ -175,7 +180,7 @@ describe("hubConnection", () => {
175180

176181
it("throws an exception when receiving a streaming result for method called with invoke", (done) => {
177182
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
178-
logger: TestLogger.instance,
183+
...commonOptions,
179184
protocol,
180185
transport: transportType,
181186
});
@@ -200,7 +205,7 @@ describe("hubConnection", () => {
200205
it("rethrows an exception from the server when streaming", (done) => {
201206
const errorMessage = "An unexpected error occurred invoking 'StreamThrowException' on the server. InvalidOperationException: An error occurred.";
202207
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
203-
logger: TestLogger.instance,
208+
...commonOptions,
204209
protocol,
205210
transport: transportType,
206211
});
@@ -229,7 +234,7 @@ describe("hubConnection", () => {
229234

230235
it("throws an exception when invoking hub method with stream", (done) => {
231236
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
232-
logger: TestLogger.instance,
237+
...commonOptions,
233238
protocol,
234239
transport: transportType,
235240
});
@@ -258,7 +263,7 @@ describe("hubConnection", () => {
258263

259264
it("can receive server calls", (done) => {
260265
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
261-
logger: TestLogger.instance,
266+
...commonOptions,
262267
protocol,
263268
transport: transportType,
264269
});
@@ -290,7 +295,7 @@ describe("hubConnection", () => {
290295

291296
it("can receive server calls without rebinding handler when restarted", (done) => {
292297
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
293-
logger: TestLogger.instance,
298+
...commonOptions,
294299
protocol,
295300
transport: transportType,
296301
});
@@ -347,7 +352,7 @@ describe("hubConnection", () => {
347352

348353
it("closed with error if hub cannot be created", (done) => {
349354
const hubConnection = new HubConnection("http://" + document.location.host + "/uncreatable", {
350-
logger: TestLogger.instance,
355+
...commonOptions,
351356
protocol,
352357
transport: transportType,
353358
});
@@ -361,7 +366,7 @@ describe("hubConnection", () => {
361366

362367
it("can handle different types", (done) => {
363368
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
364-
logger: TestLogger.instance,
369+
...commonOptions,
365370
protocol,
366371
transport: transportType,
367372
});
@@ -409,7 +414,7 @@ describe("hubConnection", () => {
409414
const message = "你好,世界!";
410415

411416
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
412-
logger: TestLogger.instance,
417+
...commonOptions,
413418
protocol,
414419
transport: transportType,
415420
});
@@ -464,7 +469,7 @@ describe("hubConnection", () => {
464469
const jwtToken = await getJwtToken("http://" + document.location.host + "/generateJwtToken");
465470
const hubConnection = new HubConnection("/authorizedhub", {
466471
accessTokenFactory: () => jwtToken,
467-
logger: TestLogger.instance,
472+
...commonOptions,
468473
transport: transportType,
469474
});
470475
hubConnection.onclose((error) => {
@@ -488,7 +493,7 @@ describe("hubConnection", () => {
488493
if (transportType !== TransportType.LongPolling) {
489494
it("terminates if no messages received within timeout interval", (done) => {
490495
const hubConnection = new HubConnection(TESTHUBENDPOINT_URL, {
491-
logger: TestLogger.instance,
496+
...commonOptions,
492497
timeoutInMilliseconds: 100,
493498
transport: transportType,
494499
});
@@ -512,7 +517,7 @@ describe("hubConnection", () => {
512517
if (typeof EventSource !== "undefined") {
513518
it("allows Server-Sent Events when negotiating for JSON protocol", async (done) => {
514519
const hubConnection = new HubConnection(TESTHUB_NOWEBSOCKETS_ENDPOINT_URL, {
515-
logger: TestLogger.instance,
520+
...commonOptions,
516521
protocol: new JsonHubProtocol(),
517522
});
518523

@@ -530,7 +535,7 @@ describe("hubConnection", () => {
530535

531536
it("skips Server-Sent Events when negotiating for MsgPack protocol", async (done) => {
532537
const hubConnection = new HubConnection(TESTHUB_NOWEBSOCKETS_ENDPOINT_URL, {
533-
logger: TestLogger.instance,
538+
...commonOptions,
534539
protocol: new MessagePackHubProtocol(),
535540
});
536541

0 commit comments

Comments
 (0)