|
| 1 | +import 'mocha' |
| 2 | +import { assert } from 'chai' |
| 3 | +import { nanoid } from 'nanoid' |
| 4 | +import { sign } from 'jsonwebtoken' |
| 5 | +import { ContentType } from 'allure2-js-commons' |
| 6 | + |
| 7 | +import { FunctionsClient } from '../../src/index' |
| 8 | + |
| 9 | +import { Relay, runRelay } from '../relay/container' |
| 10 | +import { attach, log } from '../utils/allure' |
| 11 | +import { MirrorResponse } from '../models/mirrorResponse' |
| 12 | + |
| 13 | +describe('params reached to function', () => { |
| 14 | + let relay: Relay |
| 15 | + const jwtSecret = nanoid(10) |
| 16 | + const apiKey = sign({ name: 'anon' }, jwtSecret) |
| 17 | + |
| 18 | + before(async () => { |
| 19 | + relay = await runRelay('mirror', jwtSecret) |
| 20 | + }) |
| 21 | + |
| 22 | + after(async () => { |
| 23 | + relay && relay.container && (await relay.container.stop()) |
| 24 | + }) |
| 25 | + |
| 26 | + it('invoke mirror', async () => { |
| 27 | + log('create FunctionsClient') |
| 28 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 29 | + Authorization: `Bearer ${apiKey}`, |
| 30 | + }) |
| 31 | + |
| 32 | + log('invoke mirror') |
| 33 | + const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { responseType: 'json' }) |
| 34 | + |
| 35 | + log('assert no error') |
| 36 | + assert.isNull(error) |
| 37 | + |
| 38 | + const expected = { |
| 39 | + url: 'http://localhost:8000/mirror', |
| 40 | + method: 'POST', |
| 41 | + headers: data?.headers ?? [], |
| 42 | + body: '', |
| 43 | + } |
| 44 | + attach( |
| 45 | + 'check data from function', |
| 46 | + `expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`, |
| 47 | + ContentType.TEXT |
| 48 | + ) |
| 49 | + assert.deepEqual(data, expected) |
| 50 | + }) |
| 51 | + |
| 52 | + it('invoke mirror with client header', async () => { |
| 53 | + log('create FunctionsClient') |
| 54 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`, { |
| 55 | + Authorization: `Bearer ${apiKey}`, |
| 56 | + CustomHeader: 'check me', |
| 57 | + }) |
| 58 | + |
| 59 | + log('invoke mirror') |
| 60 | + const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { responseType: 'json' }) |
| 61 | + |
| 62 | + log('assert no error') |
| 63 | + assert.isNull(error) |
| 64 | + |
| 65 | + const expected = { |
| 66 | + url: 'http://localhost:8000/mirror', |
| 67 | + method: 'POST', |
| 68 | + headers: data?.headers ?? [], |
| 69 | + body: '', |
| 70 | + } |
| 71 | + attach( |
| 72 | + 'check data from function', |
| 73 | + `expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`, |
| 74 | + ContentType.TEXT |
| 75 | + ) |
| 76 | + assert.deepEqual(data, expected) |
| 77 | + attach( |
| 78 | + 'check headers from function', |
| 79 | + `expected to include: ${['customheader', 'check me']}\n actual: ${JSON.stringify( |
| 80 | + data?.headers |
| 81 | + )}`, |
| 82 | + ContentType.TEXT |
| 83 | + ) |
| 84 | + assert.isTrue( |
| 85 | + (data?.headers as [Array<string>]).filter( |
| 86 | + ([k, v]) => k === 'customheader' && v === 'check me' |
| 87 | + ).length > 0 |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + it('invoke mirror with invoke header', async () => { |
| 92 | + log('create FunctionsClient') |
| 93 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`) |
| 94 | + |
| 95 | + log('invoke mirror') |
| 96 | + const customHeader = nanoid() |
| 97 | + const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { |
| 98 | + responseType: 'json', |
| 99 | + headers: { |
| 100 | + 'custom-header': customHeader, |
| 101 | + Authorization: `Bearer ${apiKey}`, |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + log('assert no error') |
| 106 | + assert.isNull(error) |
| 107 | + |
| 108 | + const expected = { |
| 109 | + url: 'http://localhost:8000/mirror', |
| 110 | + method: 'POST', |
| 111 | + headers: data?.headers ?? [], |
| 112 | + body: '', |
| 113 | + } |
| 114 | + attach( |
| 115 | + 'check data from function', |
| 116 | + `expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`, |
| 117 | + ContentType.TEXT |
| 118 | + ) |
| 119 | + assert.deepEqual(data, expected) |
| 120 | + attach( |
| 121 | + 'check headers from function', |
| 122 | + `expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify( |
| 123 | + data?.headers |
| 124 | + )}`, |
| 125 | + ContentType.TEXT |
| 126 | + ) |
| 127 | + assert.isTrue( |
| 128 | + (data?.headers as [Array<string>]).filter( |
| 129 | + ([k, v]) => k === 'custom-header' && v === customHeader |
| 130 | + ).length > 0 |
| 131 | + ) |
| 132 | + }) |
| 133 | + |
| 134 | + it('invoke mirror with body formData', async () => { |
| 135 | + log('create FunctionsClient') |
| 136 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`) |
| 137 | + attach('setAuth', apiKey, ContentType.TEXT) |
| 138 | + fclient.setAuth(apiKey) |
| 139 | + |
| 140 | + log('invoke mirror') |
| 141 | + var form = new URLSearchParams() |
| 142 | + form.append(nanoid(5), nanoid(10)) |
| 143 | + form.append(nanoid(7), nanoid(5)) |
| 144 | + form.append(nanoid(15), nanoid()) |
| 145 | + const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { |
| 146 | + responseType: 'json', |
| 147 | + body: form, |
| 148 | + headers: { |
| 149 | + 'content-type': 'application/x-www-form-urlencoded', |
| 150 | + }, |
| 151 | + }) |
| 152 | + |
| 153 | + log('assert no error') |
| 154 | + assert.isNull(error) |
| 155 | + |
| 156 | + const body = [] |
| 157 | + for (const e of form.entries()) { |
| 158 | + body.push(e) |
| 159 | + } |
| 160 | + const expected = { |
| 161 | + url: 'http://localhost:8000/mirror', |
| 162 | + method: 'POST', |
| 163 | + headers: data?.headers ?? [], |
| 164 | + body: body, |
| 165 | + } |
| 166 | + attach( |
| 167 | + 'check data from function', |
| 168 | + `expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`, |
| 169 | + ContentType.TEXT |
| 170 | + ) |
| 171 | + assert.deepEqual(data, expected) |
| 172 | + }) |
| 173 | + |
| 174 | + it('invoke mirror with body json', async () => { |
| 175 | + log('create FunctionsClient') |
| 176 | + const fclient = new FunctionsClient(`http://localhost:${relay.container.getMappedPort(8081)}`) |
| 177 | + attach('setAuth', apiKey, ContentType.TEXT) |
| 178 | + fclient.setAuth(apiKey) |
| 179 | + |
| 180 | + log('invoke mirror') |
| 181 | + const body = { |
| 182 | + one: nanoid(10), |
| 183 | + two: nanoid(5), |
| 184 | + three: nanoid(), |
| 185 | + num: 11, |
| 186 | + flag: false, |
| 187 | + } |
| 188 | + const { data, error } = await fclient.invoke<MirrorResponse>('mirror', { |
| 189 | + responseType: 'json', |
| 190 | + body: JSON.stringify(body), |
| 191 | + headers: { |
| 192 | + 'content-type': 'application/json', |
| 193 | + }, |
| 194 | + }) |
| 195 | + |
| 196 | + log('assert no error') |
| 197 | + assert.isNull(error) |
| 198 | + |
| 199 | + const expected = { |
| 200 | + url: 'http://localhost:8000/mirror', |
| 201 | + method: 'POST', |
| 202 | + headers: data?.headers ?? [], |
| 203 | + body: body, |
| 204 | + } |
| 205 | + attach( |
| 206 | + 'check data from function', |
| 207 | + `expected: ${JSON.stringify(expected)}\n actual: ${JSON.stringify(data)}`, |
| 208 | + ContentType.TEXT |
| 209 | + ) |
| 210 | + assert.deepEqual(data, expected) |
| 211 | + }) |
| 212 | +}) |
0 commit comments