/
ConnectionTests.ts
169 lines (146 loc) · 7.41 KB
/
ConnectionTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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.
// This code uses a lot of `.then` instead of `await` and TSLint doesn't like it.
// tslint:disable:no-floating-promises
import { HttpTransportType, IHttpConnectionOptions, TransferFormat } from "@microsoft/signalr";
import { eachHttpClient, eachTransport, ECHOENDPOINT_URL } from "./Common";
import { TestLogger } from "./TestLogger";
// We want to continue testing HttpConnection, but we don't export it anymore. So just pull it in directly from the source file.
import { HttpConnection } from "@microsoft/signalr/dist/esm/HttpConnection";
import { formatArrayBuffer } from "@microsoft/signalr/dist/esm/Utils";
import "./LogBannerReporter";
const commonOptions: IHttpConnectionOptions = {
logMessageContent: true,
logger: TestLogger.instance,
};
describe("connection", () => {
it("can connect to the server without specifying transport explicitly", (done) => {
const message = "Hello World!";
const connection = new HttpConnection(ECHOENDPOINT_URL, {
...commonOptions,
});
connection.onreceive = async (data: any) => {
if (data === message) {
connection.stop();
}
};
connection.onclose = (error: any) => {
expect(error).toBeUndefined();
done();
};
connection.start(TransferFormat.Text).then(() => {
connection.send(message);
}).catch((e) => {
fail(e);
done();
});
});
eachTransport((transportType) => {
eachHttpClient((httpClient) => {
if (transportType === HttpTransportType.HttpStreaming && !httpClient.supportsStreaming) {
return;
}
describe(`over ${HttpTransportType[transportType]} with ${(httpClient.constructor as any).name}`, () => {
it("can send and receive messages", (done) => {
const message = "Hello World!";
// the url should be resolved relative to the document.location.host
// and the leading '/' should be automatically added to the url
const connection = new HttpConnection(ECHOENDPOINT_URL, {
...commonOptions,
httpClient,
transport: transportType,
});
connection.onreceive = (data: any) => {
if ((typeof data === "string" && data === message) ||
(data instanceof ArrayBuffer && new TextDecoder("utf-8").decode(data) === message)) {
connection.stop();
}
};
connection.onclose = (error: any) => {
expect(error).toBeUndefined();
done();
};
connection.start(TransferFormat.Text).then(() => {
connection.send(message);
}).catch((e: any) => {
fail(e);
done();
});
});
it("does not log content of messages sent or received by default", (done) => {
TestLogger.saveLogsAndReset();
const message = "Hello World!";
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is not set.
const connection = new HttpConnection(ECHOENDPOINT_URL, {
httpClient,
logger: TestLogger.instance,
transport: transportType,
});
connection.onreceive = (data: any) => {
if ((typeof data === "string" && data === message) ||
(data instanceof ArrayBuffer && new TextDecoder("utf-8").decode(data) === message)) {
connection.stop();
}
};
// @ts-ignore: We don't use the error parameter intentionally.
connection.onclose = (error) => {
// Search the logs for the message content
expect(TestLogger.instance.currentLog.messages.length).toBeGreaterThan(0);
// @ts-ignore: We don't use the _ or __ parameters intentionally.
for (const [_, __, logMessage] of TestLogger.instance.currentLog.messages) {
expect(logMessage).not.toContain(message);
}
done();
};
connection.start(TransferFormat.Text).then(() => {
connection.send(message);
}).catch((e) => {
fail(e);
done();
});
});
it("does log content of messages sent or received when enabled", (done) => {
TestLogger.saveLogsAndReset();
const message = "Hello World!";
const encodedMessage = formatArrayBuffer(new TextEncoder().encode(message).buffer);
// DON'T use commonOptions because we want to specifically test the scenario where logMessageContent is set to true (even if commonOptions changes).
const connection = new HttpConnection(ECHOENDPOINT_URL, {
httpClient,
logMessageContent: true,
logger: TestLogger.instance,
transport: transportType,
});
connection.onreceive = (data: any) => {
if ((typeof data === "string" && data === message) ||
(data instanceof ArrayBuffer && new TextDecoder("utf-8").decode(data) === message)) {
connection.stop();
}
};
// @ts-ignore: We don't use the error parameter intentionally.
connection.onclose = (error) => {
// Search the logs for the message content
let matches = 0;
expect(TestLogger.instance.currentLog.messages.length).toBeGreaterThan(0);
// @ts-ignore: We don't use the _ or __ parameters intentionally.
for (const [_, __, logMessage] of TestLogger.instance.currentLog.messages) {
if (logMessage.indexOf(message) !== -1) {
matches += 1;
} else if (logMessage.indexOf(encodedMessage.toString()) !== -1) {
matches += 1;
}
}
// One match for send, one for receive.
expect(matches).toEqual(2);
done();
};
connection.start(TransferFormat.Text).then(() => {
connection.send(message);
}).catch((e: any) => {
fail(e);
done();
});
});
});
});
});
});