2010-09-29 12:18:46 +08:00
|
|
|
require(__dirname+'/test-helper');
|
|
|
|
test('Parser on single messages', function() {
|
2010-09-29 14:01:52 +08:00
|
|
|
var authOkData = [0x52, 00, 00, 00, 08, 00, 00, 00, 00];
|
|
|
|
var authenticationOkBuffer = Buffer(authOkData);
|
2010-09-30 13:14:41 +08:00
|
|
|
|
|
|
|
var firstString = [0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0];
|
|
|
|
var secondString = [0x55, 0x54, 0x46, 0x38, 0];
|
2010-09-29 14:01:52 +08:00
|
|
|
var paramStatusData = [0x53, 0, 0, 0, 0x19].concat(firstString).concat(secondString);
|
|
|
|
var parameterStatusBuffer = Buffer(paramStatusData);
|
2010-09-30 13:14:41 +08:00
|
|
|
|
2010-09-29 14:01:52 +08:00
|
|
|
var backendKeyData = [0x4b, 0, 0, 0, 0x0c, 0, 0, 0, 1, 0, 0, 0, 2];
|
|
|
|
var backendKeyDataBuffer = Buffer(backendKeyData);
|
2010-09-30 13:14:41 +08:00
|
|
|
|
2010-09-29 14:01:52 +08:00
|
|
|
var readyForQueryData = [0x5a, 0, 0, 0, 5, 'I'.charCodeAt(0)];
|
|
|
|
var readyForQueryBuffer = Buffer(readyForQueryData)
|
2010-09-29 12:18:46 +08:00
|
|
|
|
2010-09-30 13:36:07 +08:00
|
|
|
|
|
|
|
var expectedAuthenticationOkayMessage = {
|
|
|
|
name: 'AuthenticationOk',
|
|
|
|
id: 'R',
|
|
|
|
length: 8
|
|
|
|
};
|
|
|
|
|
|
|
|
var expectedParameterStatusMessage = {
|
2010-09-30 13:32:44 +08:00
|
|
|
name: 'ParameterStatus',
|
|
|
|
id: 'S',
|
|
|
|
length: 25,
|
|
|
|
parameterName: 'client_encoding',
|
|
|
|
parameterValue: 'UTF8'
|
2010-09-30 13:36:07 +08:00
|
|
|
};
|
2010-09-29 13:20:10 +08:00
|
|
|
|
2010-09-30 13:36:07 +08:00
|
|
|
var expectedBackendKeyDataMessage = {
|
2010-09-30 13:32:44 +08:00
|
|
|
name: 'BackendKeyData',
|
|
|
|
id: 'K',
|
|
|
|
processID: 1,
|
|
|
|
secretKey: 2
|
2010-09-30 13:36:07 +08:00
|
|
|
};
|
2010-09-30 13:14:41 +08:00
|
|
|
|
2010-09-30 13:36:07 +08:00
|
|
|
var expectedReadyForQueryMessage = {
|
2010-09-30 13:32:44 +08:00
|
|
|
name: 'ReadyForQuery',
|
|
|
|
id: 'Z',
|
|
|
|
length: 5,
|
|
|
|
status: 'I'
|
2010-09-30 13:36:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
test('parses AuthenticationOk message', function() {
|
2010-09-30 13:40:06 +08:00
|
|
|
var result = new Parser(authenticationOkBuffer).parse()[0];
|
2010-09-30 13:36:07 +08:00
|
|
|
assert.same(result, expectedAuthenticationOkayMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parses ParameterStatus message', function() {
|
2010-09-30 13:40:06 +08:00
|
|
|
var result = new Parser(parameterStatusBuffer).parse()[0];
|
2010-09-30 13:36:07 +08:00
|
|
|
assert.same(result, expectedParameterStatusMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parses BackendKeyData message', function() {
|
2010-09-30 13:40:06 +08:00
|
|
|
var result = new Parser(backendKeyDataBuffer).parse()[0];
|
2010-09-30 13:36:07 +08:00
|
|
|
assert.same(result, expectedBackendKeyDataMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parses ReadyForQuery message', function() {
|
2010-09-30 13:40:06 +08:00
|
|
|
var result = new Parser(readyForQueryBuffer).parse()[0];
|
2010-09-30 13:36:07 +08:00
|
|
|
assert.same(result, expectedReadyForQueryMessage);
|
2010-09-30 13:14:41 +08:00
|
|
|
});
|
|
|
|
|
2010-09-29 14:01:52 +08:00
|
|
|
test('parses multiple messages', function() {
|
|
|
|
var message = authOkData
|
|
|
|
.concat(paramStatusData)
|
|
|
|
.concat(backendKeyData)
|
|
|
|
.concat(readyForQueryData);
|
|
|
|
var buffer = Buffer(message);
|
|
|
|
var result = new Parser(buffer).parse();
|
|
|
|
assert.equal(result.length, 4);
|
|
|
|
assert.same(result[0], expectedAuthenticationOkayMessage);
|
|
|
|
assert.same(result[1], expectedParameterStatusMessage);
|
|
|
|
assert.same(result[2], expectedBackendKeyDataMessage);
|
|
|
|
assert.same(result[3], expectedReadyForQueryMessage);
|
|
|
|
});
|
|
|
|
|
2010-09-29 13:20:10 +08:00
|
|
|
test('parses normal CString', function() {
|
2010-09-29 13:30:35 +08:00
|
|
|
var result = new Parser(Buffer([33,0])).parseCString();
|
2010-09-29 13:20:10 +08:00
|
|
|
assert.equal(result,"!");
|
|
|
|
});
|
2010-09-29 14:01:52 +08:00
|
|
|
|
|
|
|
var resultText = stringToHex("SELECT 3\0");
|
|
|
|
var length = resultText.length + 4;
|
|
|
|
var commandCompleteData = [0x43, 0, 0, 0, length].concat(resultText);
|
|
|
|
|
|
|
|
test('parses CommandComplete message', function() {
|
|
|
|
var result = new Parser(Buffer(commandCompleteData)).parse()[0];
|
|
|
|
assert.same(result, {
|
|
|
|
length: 13,
|
|
|
|
id: 'C',
|
|
|
|
text: "SELECT 3"
|
|
|
|
});
|
|
|
|
});
|
2010-09-29 13:20:10 +08:00
|
|
|
|
|
|
|
test('parses empty CString', function() {
|
2010-09-29 13:30:35 +08:00
|
|
|
var result = new Parser(Buffer([0])).parseCString();
|
2010-09-29 13:20:10 +08:00
|
|
|
assert.equal(result, '');
|
|
|
|
});
|
2010-09-29 13:30:35 +08:00
|
|
|
|
|
|
|
test('parses length', function() {
|
|
|
|
var parser = new Parser(Buffer([0,0,0,3]));
|
|
|
|
var result = parser.parseLength();
|
|
|
|
assert.equal(result, 3);
|
|
|
|
assert.equal(parser.offset, 4);
|
|
|
|
});
|
2010-09-29 13:43:28 +08:00
|
|
|
|
|
|
|
test('parsing empty buffer returns false', function() {
|
|
|
|
var parser = new Parser(Buffer(0));
|
|
|
|
assert.equal(parser.parse(), false);
|
|
|
|
});
|
2010-09-29 12:18:46 +08:00
|
|
|
});
|
|
|
|
|