outbound-serializer.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const assert_1 = __importDefault(require("assert"));
  7. const serializer_1 = require("./serializer");
  8. const buffer_list_1 = __importDefault(require("./testing/buffer-list"));
  9. describe('serializer', () => {
  10. it('builds startup message', function () {
  11. const actual = serializer_1.serialize.startup({
  12. user: 'brian',
  13. database: 'bang',
  14. });
  15. assert_1.default.deepEqual(actual, new buffer_list_1.default()
  16. .addInt16(3)
  17. .addInt16(0)
  18. .addCString('user')
  19. .addCString('brian')
  20. .addCString('database')
  21. .addCString('bang')
  22. .addCString('client_encoding')
  23. .addCString('UTF8')
  24. .addCString('')
  25. .join(true));
  26. });
  27. it('builds password message', function () {
  28. const actual = serializer_1.serialize.password('!');
  29. assert_1.default.deepEqual(actual, new buffer_list_1.default().addCString('!').join(true, 'p'));
  30. });
  31. it('builds request ssl message', function () {
  32. const actual = serializer_1.serialize.requestSsl();
  33. const expected = new buffer_list_1.default().addInt32(80877103).join(true);
  34. assert_1.default.deepEqual(actual, expected);
  35. });
  36. it('builds SASLInitialResponseMessage message', function () {
  37. const actual = serializer_1.serialize.sendSASLInitialResponseMessage('mech', 'data');
  38. assert_1.default.deepEqual(actual, new buffer_list_1.default().addCString('mech').addInt32(4).addString('data').join(true, 'p'));
  39. });
  40. it('builds SCRAMClientFinalMessage message', function () {
  41. const actual = serializer_1.serialize.sendSCRAMClientFinalMessage('data');
  42. assert_1.default.deepEqual(actual, new buffer_list_1.default().addString('data').join(true, 'p'));
  43. });
  44. it('builds query message', function () {
  45. var txt = 'select * from boom';
  46. const actual = serializer_1.serialize.query(txt);
  47. assert_1.default.deepEqual(actual, new buffer_list_1.default().addCString(txt).join(true, 'Q'));
  48. });
  49. describe('parse message', () => {
  50. it('builds parse message', function () {
  51. const actual = serializer_1.serialize.parse({ text: '!' });
  52. var expected = new buffer_list_1.default().addCString('').addCString('!').addInt16(0).join(true, 'P');
  53. assert_1.default.deepEqual(actual, expected);
  54. });
  55. it('builds parse message with named query', function () {
  56. const actual = serializer_1.serialize.parse({
  57. name: 'boom',
  58. text: 'select * from boom',
  59. types: [],
  60. });
  61. var expected = new buffer_list_1.default().addCString('boom').addCString('select * from boom').addInt16(0).join(true, 'P');
  62. assert_1.default.deepEqual(actual, expected);
  63. });
  64. it('with multiple parameters', function () {
  65. const actual = serializer_1.serialize.parse({
  66. name: 'force',
  67. text: 'select * from bang where name = $1',
  68. types: [1, 2, 3, 4],
  69. });
  70. var expected = new buffer_list_1.default()
  71. .addCString('force')
  72. .addCString('select * from bang where name = $1')
  73. .addInt16(4)
  74. .addInt32(1)
  75. .addInt32(2)
  76. .addInt32(3)
  77. .addInt32(4)
  78. .join(true, 'P');
  79. assert_1.default.deepEqual(actual, expected);
  80. });
  81. });
  82. describe('bind messages', function () {
  83. it('with no values', function () {
  84. const actual = serializer_1.serialize.bind();
  85. var expectedBuffer = new buffer_list_1.default()
  86. .addCString('')
  87. .addCString('')
  88. .addInt16(0)
  89. .addInt16(0)
  90. .addInt16(0)
  91. .join(true, 'B');
  92. assert_1.default.deepEqual(actual, expectedBuffer);
  93. });
  94. it('with named statement, portal, and values', function () {
  95. const actual = serializer_1.serialize.bind({
  96. portal: 'bang',
  97. statement: 'woo',
  98. values: ['1', 'hi', null, 'zing'],
  99. });
  100. var expectedBuffer = new buffer_list_1.default()
  101. .addCString('bang') // portal name
  102. .addCString('woo') // statement name
  103. .addInt16(4)
  104. .addInt16(0)
  105. .addInt16(0)
  106. .addInt16(0)
  107. .addInt16(0)
  108. .addInt16(4)
  109. .addInt32(1)
  110. .add(Buffer.from('1'))
  111. .addInt32(2)
  112. .add(Buffer.from('hi'))
  113. .addInt32(-1)
  114. .addInt32(4)
  115. .add(Buffer.from('zing'))
  116. .addInt16(0)
  117. .join(true, 'B');
  118. assert_1.default.deepEqual(actual, expectedBuffer);
  119. });
  120. });
  121. it('with custom valueMapper', function () {
  122. const actual = serializer_1.serialize.bind({
  123. portal: 'bang',
  124. statement: 'woo',
  125. values: ['1', 'hi', null, 'zing'],
  126. valueMapper: () => null,
  127. });
  128. var expectedBuffer = new buffer_list_1.default()
  129. .addCString('bang') // portal name
  130. .addCString('woo') // statement name
  131. .addInt16(4)
  132. .addInt16(0)
  133. .addInt16(0)
  134. .addInt16(0)
  135. .addInt16(0)
  136. .addInt16(4)
  137. .addInt32(-1)
  138. .addInt32(-1)
  139. .addInt32(-1)
  140. .addInt32(-1)
  141. .addInt16(0)
  142. .join(true, 'B');
  143. assert_1.default.deepEqual(actual, expectedBuffer);
  144. });
  145. it('with named statement, portal, and buffer value', function () {
  146. const actual = serializer_1.serialize.bind({
  147. portal: 'bang',
  148. statement: 'woo',
  149. values: ['1', 'hi', null, Buffer.from('zing', 'utf8')],
  150. });
  151. var expectedBuffer = new buffer_list_1.default()
  152. .addCString('bang') // portal name
  153. .addCString('woo') // statement name
  154. .addInt16(4) // value count
  155. .addInt16(0) // string
  156. .addInt16(0) // string
  157. .addInt16(0) // string
  158. .addInt16(1) // binary
  159. .addInt16(4)
  160. .addInt32(1)
  161. .add(Buffer.from('1'))
  162. .addInt32(2)
  163. .add(Buffer.from('hi'))
  164. .addInt32(-1)
  165. .addInt32(4)
  166. .add(Buffer.from('zing', 'utf-8'))
  167. .addInt16(0)
  168. .join(true, 'B');
  169. assert_1.default.deepEqual(actual, expectedBuffer);
  170. });
  171. describe('builds execute message', function () {
  172. it('for unamed portal with no row limit', function () {
  173. const actual = serializer_1.serialize.execute();
  174. var expectedBuffer = new buffer_list_1.default().addCString('').addInt32(0).join(true, 'E');
  175. assert_1.default.deepEqual(actual, expectedBuffer);
  176. });
  177. it('for named portal with row limit', function () {
  178. const actual = serializer_1.serialize.execute({
  179. portal: 'my favorite portal',
  180. rows: 100,
  181. });
  182. var expectedBuffer = new buffer_list_1.default().addCString('my favorite portal').addInt32(100).join(true, 'E');
  183. assert_1.default.deepEqual(actual, expectedBuffer);
  184. });
  185. });
  186. it('builds flush command', function () {
  187. const actual = serializer_1.serialize.flush();
  188. var expected = new buffer_list_1.default().join(true, 'H');
  189. assert_1.default.deepEqual(actual, expected);
  190. });
  191. it('builds sync command', function () {
  192. const actual = serializer_1.serialize.sync();
  193. var expected = new buffer_list_1.default().join(true, 'S');
  194. assert_1.default.deepEqual(actual, expected);
  195. });
  196. it('builds end command', function () {
  197. const actual = serializer_1.serialize.end();
  198. var expected = Buffer.from([0x58, 0, 0, 0, 4]);
  199. assert_1.default.deepEqual(actual, expected);
  200. });
  201. describe('builds describe command', function () {
  202. it('describe statement', function () {
  203. const actual = serializer_1.serialize.describe({ type: 'S', name: 'bang' });
  204. var expected = new buffer_list_1.default().addChar('S').addCString('bang').join(true, 'D');
  205. assert_1.default.deepEqual(actual, expected);
  206. });
  207. it('describe unnamed portal', function () {
  208. const actual = serializer_1.serialize.describe({ type: 'P' });
  209. var expected = new buffer_list_1.default().addChar('P').addCString('').join(true, 'D');
  210. assert_1.default.deepEqual(actual, expected);
  211. });
  212. });
  213. describe('builds close command', function () {
  214. it('describe statement', function () {
  215. const actual = serializer_1.serialize.close({ type: 'S', name: 'bang' });
  216. var expected = new buffer_list_1.default().addChar('S').addCString('bang').join(true, 'C');
  217. assert_1.default.deepEqual(actual, expected);
  218. });
  219. it('describe unnamed portal', function () {
  220. const actual = serializer_1.serialize.close({ type: 'P' });
  221. var expected = new buffer_list_1.default().addChar('P').addCString('').join(true, 'C');
  222. assert_1.default.deepEqual(actual, expected);
  223. });
  224. });
  225. describe('copy messages', function () {
  226. it('builds copyFromChunk', () => {
  227. const actual = serializer_1.serialize.copyData(Buffer.from([1, 2, 3]));
  228. const expected = new buffer_list_1.default().add(Buffer.from([1, 2, 3])).join(true, 'd');
  229. assert_1.default.deepEqual(actual, expected);
  230. });
  231. it('builds copy fail', () => {
  232. const actual = serializer_1.serialize.copyFail('err!');
  233. const expected = new buffer_list_1.default().addCString('err!').join(true, 'f');
  234. assert_1.default.deepEqual(actual, expected);
  235. });
  236. it('builds copy done', () => {
  237. const actual = serializer_1.serialize.copyDone();
  238. const expected = new buffer_list_1.default().join(true, 'c');
  239. assert_1.default.deepEqual(actual, expected);
  240. });
  241. });
  242. it('builds cancel message', () => {
  243. const actual = serializer_1.serialize.cancel(3, 4);
  244. const expected = new buffer_list_1.default().addInt16(1234).addInt16(5678).addInt32(3).addInt32(4).join(true);
  245. assert_1.default.deepEqual(actual, expected);
  246. });
  247. });
  248. //# sourceMappingURL=outbound-serializer.test.js.map