-
-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathtest-protocol-errors.test.mts
More file actions
87 lines (80 loc) · 2.89 KB
/
test-protocol-errors.test.mts
File metadata and controls
87 lines (80 loc) · 2.89 KB
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
// This file was modified by Oracle on January 21, 2021.
// The connection with the mock server needs to happen in the same host where
// the tests are running in order to avoid connecting a potential MySQL server
// instance running in the host identified by the MYSQL_HOST environment
// variable.
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
import type { FieldPacket, RowDataPacket } from '../../../index.js';
import { describe, it, skip, strict } from 'poku';
import { createConnection, createServer } from '../../common.test.mjs';
if (typeof Deno !== 'undefined') skip('Deno: process is not terminated');
const query = 'SELECT 1';
await describe('Protocol Errors', async () => {
await it('should handle unexpected packet errors', async () => {
let fields: FieldPacket[];
let error: Error & { fatal?: boolean; code?: string };
let rows: RowDataPacket[];
await new Promise<void>((resolve) => {
const server = createServer(
() => {
const connection = createConnection({
// The mock server is running on the same host machine.
// We need to explicitly define the host to avoid connecting to a potential
// different host provided via MYSQL_HOST that identifies a real MySQL
// server instance.
host: 'localhost',
// @ts-expect-error: internal access
port: server._port,
ssl: false,
});
connection.query<RowDataPacket[]>(query, (err, _rows, _fields) => {
if (err) return;
rows = _rows;
fields = _fields;
});
connection.on('error', (err) => {
error = err;
// @ts-expect-error: internal access
if (server._server._handle) {
// @ts-expect-error: TODO: implement typings
server.close();
}
resolve();
});
},
(conn) => {
conn.on('query', () => {
conn.writeTextResult(
[{ 1: '1' }],
[
{
catalog: 'def',
schema: '',
table: '',
orgTable: '',
name: '1',
orgName: '',
characterSet: 63,
columnLength: 1,
columnType: 8,
flags: 129,
decimals: 0,
},
]
);
// this is extra (incorrect) packet - client should emit error on receiving it
conn.writeOk();
});
}
);
});
strict.deepEqual(rows!, [{ 1: 1 }]);
strict.equal(fields![0].name, '1');
strict.equal(
error!.message,
'Unexpected packet while no commands in the queue'
);
strict.equal(error!.fatal, true);
strict.equal(error!.code, 'PROTOCOL_UNEXPECTED_PACKET');
});
});