-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathoutbound-sending-tests.js
211 lines (191 loc) · 5.56 KB
/
outbound-sending-tests.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
require(__dirname + '/test-helper')
var Connection = require(__dirname + '/../../../lib/connection')
var stream = new MemoryStream()
var con = new Connection({
stream: stream
})
assert.received = function (stream, buffer) {
assert.lengthIs(stream.packets, 1)
var packet = stream.packets.pop()
assert.equalBuffers(packet, buffer)
}
test('sends startup message', function () {
con.startup({
user: 'brian',
database: 'bang'
})
assert.received(stream, new BufferList()
.addInt16(3)
.addInt16(0)
.addCString('user')
.addCString('brian')
.addCString('database')
.addCString('bang')
.addCString('client_encoding')
.addCString("'utf-8'")
.addCString('').join(true))
})
test('sends password message', function () {
con.password('!')
assert.received(stream, new BufferList().addCString('!').join(true, 'p'))
})
test('sends SASLInitialResponseMessage message', function () {
con.sendSASLInitialResponseMessage('mech', 'data')
assert.received(stream, new BufferList().addCString('mech').addInt32(4).addString('data').join(true, 'p'))
})
test('sends SCRAMClientFinalMessage message', function () {
con.sendSCRAMClientFinalMessage('data')
assert.received(stream, new BufferList().addString('data').join(true, 'p'))
})
test('sends query message', function () {
var txt = 'select * from boom'
con.query(txt)
assert.received(stream, new BufferList().addCString(txt).join(true, 'Q'))
})
test('sends parse message', function () {
con.parse({text: '!'})
var expected = new BufferList()
.addCString('')
.addCString('!')
.addInt16(0).join(true, 'P')
assert.received(stream, expected)
})
test('sends parse message with named query', function () {
con.parse({
name: 'boom',
text: 'select * from boom',
types: []
})
var expected = new BufferList()
.addCString('boom')
.addCString('select * from boom')
.addInt16(0).join(true, 'P')
assert.received(stream, expected)
test('with multiple parameters', function () {
con.parse({
name: 'force',
text: 'select * from bang where name = $1',
types: [1, 2, 3, 4]
})
var expected = new BufferList()
.addCString('force')
.addCString('select * from bang where name = $1')
.addInt16(4)
.addInt32(1)
.addInt32(2)
.addInt32(3)
.addInt32(4).join(true, 'P')
assert.received(stream, expected)
})
})
test('bind messages', function () {
test('with no values', function () {
con.bind()
var expectedBuffer = new BufferList()
.addCString('')
.addCString('')
.addInt16(0)
.addInt16(0)
.addInt16(0)
.join(true, 'B')
assert.received(stream, expectedBuffer)
})
test('with named statement, portal, and values', function () {
con.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, 'zing']
})
var expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(0)
.addInt16(4)
.addInt32(1)
.add(Buffer.from('1'))
.addInt32(2)
.add(Buffer.from('hi'))
.addInt32(-1)
.addInt32(4)
.add(Buffer.from('zing'))
.addInt16(0)
.join(true, 'B')
assert.received(stream, expectedBuffer)
})
})
test('with named statement, portal, and buffer value', function () {
con.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, Buffer.from('zing', 'utf8')]
})
var expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4)// value count
.addInt16(0)// string
.addInt16(0)// string
.addInt16(0)// string
.addInt16(1)// binary
.addInt16(4)
.addInt32(1)
.add(Buffer.from('1'))
.addInt32(2)
.add(Buffer.from('hi'))
.addInt32(-1)
.addInt32(4)
.add(Buffer.from('zing', 'UTF-8'))
.addInt16(0)
.join(true, 'B')
assert.received(stream, expectedBuffer)
})
test('sends execute message', function () {
test('for unamed portal with no row limit', function () {
con.execute()
var expectedBuffer = new BufferList()
.addCString('')
.addInt32(0)
.join(true, 'E')
assert.received(stream, expectedBuffer)
})
test('for named portal with row limit', function () {
con.execute({
portal: 'my favorite portal',
rows: 100
})
var expectedBuffer = new BufferList()
.addCString('my favorite portal')
.addInt32(100)
.join(true, 'E')
assert.received(stream, expectedBuffer)
})
})
test('sends flush command', function () {
con.flush()
var expected = new BufferList().join(true, 'H')
assert.received(stream, expected)
})
test('sends sync command', function () {
con.sync()
var expected = new BufferList().join(true, 'S')
assert.received(stream, expected)
})
test('sends end command', function () {
con.end()
var expected = Buffer.from([0x58, 0, 0, 0, 4])
assert.received(stream, expected)
assert.equal(stream.closed, true)
})
test('sends describe command', function () {
test('describe statement', function () {
con.describe({type: 'S', name: 'bang'})
var expected = new BufferList().addChar('S').addCString('bang').join(true, 'D')
assert.received(stream, expected)
})
test('describe unnamed portal', function () {
con.describe({type: 'P'})
var expected = new BufferList().addChar('P').addCString('').join(true, 'D')
assert.received(stream, expected)
})
})