|
| 1 | +import { connect } from '../dist/index' |
| 2 | +import { fetch, MockAgent, setGlobalDispatcher } from 'undici' |
| 3 | + |
| 4 | +import database from '../golden/database.json' |
| 5 | +import dual from '../golden/dual.json' |
| 6 | + |
| 7 | +const mockHosts = ['http://localhost:8080', 'https://example.com'] |
| 8 | +const EXECUTE_PATH = '/psdb.v1alpha1.Database/Execute' |
| 9 | + |
| 10 | +const mockAgent = new MockAgent() |
| 11 | +mockAgent.disableNetConnect() |
| 12 | + |
| 13 | +setGlobalDispatcher(mockAgent) |
| 14 | + |
| 15 | +const config = { |
| 16 | + username: 'someuser', |
| 17 | + password: 'password', |
| 18 | + host: 'example.com', |
| 19 | + fetch |
| 20 | +} |
| 21 | + |
| 22 | +// Provide the base url to the request |
| 23 | +const mockPool = mockAgent.get((value) => mockHosts.includes(value)) |
| 24 | +const mockSession = 42 |
| 25 | + |
| 26 | +describe('golden', () => { |
| 27 | + test('runs e2e database tests', async () => { |
| 28 | + const mockResponse = { |
| 29 | + session: mockSession, |
| 30 | + result: database, |
| 31 | + timing: 1 |
| 32 | + } |
| 33 | + |
| 34 | + const want = { |
| 35 | + id: '1', |
| 36 | + a: 1, |
| 37 | + b: 1, |
| 38 | + c: 1, |
| 39 | + d: 1, |
| 40 | + e: '1', |
| 41 | + f: '1.1', |
| 42 | + g: '1.1', |
| 43 | + h: 1.1, |
| 44 | + i: 1.1, |
| 45 | + j: uint8ArrayFromHex('0x07'), |
| 46 | + k: '1000-01-01', |
| 47 | + l: '1000-01-01 01:01:01', |
| 48 | + m: '1970-01-01 00:01:01', |
| 49 | + n: '01:01:01', |
| 50 | + o: 2006, |
| 51 | + p: 'p', |
| 52 | + q: 'q', |
| 53 | + r: uint8ArrayFromHex('0x72000000'), |
| 54 | + s: uint8ArrayFromHex('0x73'), |
| 55 | + t: uint8ArrayFromHex('0x74'), |
| 56 | + u: uint8ArrayFromHex('0x75'), |
| 57 | + v: uint8ArrayFromHex('0x76'), |
| 58 | + w: uint8ArrayFromHex('0x77'), |
| 59 | + x: 'x', |
| 60 | + y: 'y', |
| 61 | + z: 'z', |
| 62 | + aa: 'aa', |
| 63 | + ab: 'foo', |
| 64 | + ac: 'foo,bar', |
| 65 | + ad: { ad: null }, |
| 66 | + ae: uint8ArrayFromHex( |
| 67 | + '0x0000000001020000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000' |
| 68 | + ), |
| 69 | + af: uint8ArrayFromHex('0x000000000101000000000000000000F03F000000000000F03F'), |
| 70 | + ag: uint8ArrayFromHex( |
| 71 | + '0x0000000001020000000300000000000000000000000000000000000000000000000000F03F000000000000F03F00000000000000400000000000000000' |
| 72 | + ), |
| 73 | + ah: uint8ArrayFromHex( |
| 74 | + '0x00000000010300000002000000040000000000000000000000000000000000000000000000000000000000000000000840000000000000084000000000000000000000000000000000000000000000000004000000000000000000F03F000000000000F03F000000000000F03F00000000000000400000000000000040000000000000F03F000000000000F03F000000000000F03F' |
| 75 | + ), |
| 76 | + ai: 1, |
| 77 | + aj: 1, |
| 78 | + ak: 1, |
| 79 | + al: '1', |
| 80 | + xa: 'xa', |
| 81 | + xb: 'xb', |
| 82 | + xc: uint8ArrayFromHex('0x78630000'), |
| 83 | + xd: 'xd', |
| 84 | + NULL: null |
| 85 | + } |
| 86 | + |
| 87 | + mockPool.intercept({ path: EXECUTE_PATH, method: 'POST' }).reply(200, (opts: any) => { |
| 88 | + expect(opts.headers['Authorization']).toMatch(/Basic /) |
| 89 | + const bodyObj = JSON.parse(opts.body.toString()) |
| 90 | + expect(bodyObj.session).toEqual(null) |
| 91 | + return mockResponse |
| 92 | + }) |
| 93 | + |
| 94 | + const connection = connect(config) |
| 95 | + const got = await connection.execute('xxx') |
| 96 | + |
| 97 | + expect(got.rows[0]).toEqual(want) |
| 98 | + }) |
| 99 | + |
| 100 | + test('runs e2e dual tests', async () => { |
| 101 | + const mockResponse = { |
| 102 | + session: mockSession, |
| 103 | + result: dual, |
| 104 | + timing: 1 |
| 105 | + } |
| 106 | + |
| 107 | + const want = { |
| 108 | + a: 'ÿ' |
| 109 | + } |
| 110 | + |
| 111 | + mockPool.intercept({ path: EXECUTE_PATH, method: 'POST' }).reply(200, (opts: any) => { |
| 112 | + expect(opts.headers['Authorization']).toMatch(/Basic /) |
| 113 | + const bodyObj = JSON.parse(opts.body.toString()) |
| 114 | + expect(bodyObj.session).toEqual(null) |
| 115 | + return mockResponse |
| 116 | + }) |
| 117 | + |
| 118 | + const connection = connect(config) |
| 119 | + const got = await connection.execute('xxx') |
| 120 | + |
| 121 | + expect(got.rows[0]).toEqual(want) |
| 122 | + }) |
| 123 | +}) |
| 124 | + |
| 125 | +function uint8ArrayFromHex(text: string) { |
| 126 | + if (text.startsWith('0x')) { |
| 127 | + text = text.slice(2) |
| 128 | + } |
| 129 | + return Uint8Array.from((text.match(/.{1,2}/g) ?? []).map((byte) => parseInt(byte, 16))) |
| 130 | +} |
0 commit comments