Skip to content

Commit

Permalink
feat: added templates to verify
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Oct 1, 2024
1 parent 4aaa381 commit 5166654
Show file tree
Hide file tree
Showing 39 changed files with 2,719 additions and 485 deletions.
24 changes: 24 additions & 0 deletions packages/server-client/__tests__/__dataSets__/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,28 @@ export default [
statusText: 'OK',
},
},
{
label: 'make delete request with content-type header but no body',
request: ['/my/path', 'DELETE'],
response: [
204,
null,
undefined,
{
headers: {
'Content-Type': 'application/json',
}
}
],
clientMethod: 'sendDeleteRequest',
parameters: [`${BASE_URL}/my/path`],
expected: {
config: expect.anything(),
data: null,
headers: expect.anything(),
request: expect.anything(),
status: 204,
statusText: 'No Content',
},
},
];
45 changes: 29 additions & 16 deletions packages/server-client/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,26 +443,39 @@ export class Client {
}
log('Request succeeded');


const contentLength = parseInt(response.headers.get('content-length') || '0');
const [contentType] = (response.headers.get('content-type') || '').split(
';',
);
log(`Response content type: ${contentType}`);

switch (contentType) {
case ContentType.FORM_URLENCODED:
log('Decoding form data');
decoded = response.body
? new URLSearchParams(await response.text())
: '';
break;
case ContentType.JSON:
log('Decoding JSON');
decoded = await response.json();
break;
default:
log('Decoding text');
decoded = await response.text();
log(`Response content type: ${contentType}`);
try {
const body = await response.text();

switch (contentType) {
case ContentType.FORM_URLENCODED:
log('Decoding form data');
decoded = response.body
? new URLSearchParams(body)
: '';
break;
case ContentType.JSON:
log('Decoding JSON');
decoded = JSON.parse(body);
break;
default:
log('Decoding text');
decoded = body;
}
} catch (error) {
log('Failed to decode body', error);
if (contentLength > 0) {
throw new VetchError(
'Failed to decode response body',
request,
response,
);
}
}

log('Decoded body', decoded);
Expand Down
56 changes: 43 additions & 13 deletions packages/verify2/__tests__/__dataSets__/cancel.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
import {
SDKTestCase,
keyAuth,
validateBearerAuth,
} from '../../../../testHelpers';
import {
Verify2,
} from '../../lib/';

export default [
{
label: 'cancel request',
request: ['/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1', 'DELETE'],
response: [204],
baseUrl: 'https://api.nexmo.com',
reqHeaders: {
authorization: validateBearerAuth,
},
client: new Verify2(keyAuth),
generator: false,
error: false,
requests: [
['/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1', 'DELETE'],
],
responses: [
[204],
],
method: 'post',
clientMethod: 'cancel',
parameters: ['091e717f-8715-41a0-a3f0-cc04912deaa1'],
expected: true,
},
{
label: 'error when request not found',
request: ['/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1', 'DELETE'],
response: [
404,
{
title: 'Not Found',
type: 'https://developer.vonage.com/api-errors#not-found',
detail: 'Request <id> was not found or it has been verified already.',
instance: 'bf0ca0bf927b3b52e3cb03217e1a1ddf',
},
baseUrl: 'https://api.nexmo.com',
reqHeaders: {
authorization: validateBearerAuth,
},
client: new Verify2(keyAuth),
generator: false,
error: false,
requests: [
['/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1', 'DELETE'],
],
responses: [
[
404,
{
title: 'Not Found',
type: 'https://developer.vonage.com/api-errors#not-found',
detail: 'Request <id> was not found or it has been verified already.',
instance: 'bf0ca0bf927b3b52e3cb03217e1a1ddf',
},
],
],
method: 'delete',
clientMethod: 'cancel',
parameters: ['091e717f-8715-41a0-a3f0-cc04912deaa1'],
expected: false,
},
];
] as SDKTestCase<Verify2>[];
85 changes: 55 additions & 30 deletions packages/verify2/__tests__/__dataSets__/check.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
import { CheckCodeRequest, CheckRequestResponse } from '../../lib/types';
import {
SDKTestCase,
keyAuth,
validateBearerAuth,
} from '../../../../testHelpers';
import { Verify2 } from '../../lib';

export default [
{
label: 'check code',
request: [
'/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1',
'POST',
{
code: '123456',
} as CheckCodeRequest,
baseUrl: 'https://api.nexmo.com',
reqHeaders: {
authorization: validateBearerAuth,
},
client: new Verify2(keyAuth),
generator: false,
error: false,
requests: [
[
'/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1',
'POST',
{
code: '123456',
} as CheckCodeRequest,
],
],
response: [
200,
{
request_id: '091e717f-8715-41a0-a3f0-cc04912deaa1',
status: 'complete',
} as CheckRequestResponse,
responses: [
[
200,
{
request_id: '091e717f-8715-41a0-a3f0-cc04912deaa1',
status: 'complete',
} as CheckRequestResponse,
],
],
method: 'post',
clientMethod: 'checkCode',
parameters: ['091e717f-8715-41a0-a3f0-cc04912deaa1', '123456'],
expected: 'complete',
},
{
label: 'error when request not found',
request: [
'/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1',
'POST',
{
code: '123456',
} as CheckCodeRequest,
baseUrl: 'https://api.nexmo.com',
reqHeaders: {
authorization: validateBearerAuth,
},
client: new Verify2(keyAuth),
generator: false,
error: 'Request failed with status code 404',
requests: [
[
'/v2/verify/091e717f-8715-41a0-a3f0-cc04912deaa1',
'POST',
{
code: '123456',
} as CheckCodeRequest,
],
],
response: [
404,
{
title: 'Not Found',
type: 'https://developer.vonage.com/api-errors#not-found',
detail: 'Request <id> was not found or it has been verified already.',
instance: 'bf0ca0bf927b3b52e3cb03217e1a1ddf',
},
responses: [
[
404,
{
title: 'Not Found',
type: 'https://developer.vonage.com/api-errors#not-found',
detail: 'Request <id> was not found or it has been verified already.',
instance: 'bf0ca0bf927b3b52e3cb03217e1a1ddf',
},
],
],
method: 'post',
clientMethod: 'checkCode',
parameters: ['091e717f-8715-41a0-a3f0-cc04912deaa1', '123456'],
expected: true,
error: 'Request failed with status code 404',
},
];
] as SDKTestCase<Verify2>[];
16 changes: 13 additions & 3 deletions packages/verify2/__tests__/__dataSets__/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import requestTests from './verify';
import checkTests from './check';
import cancelTests from './cancel';
import templateTests from './templates';
import fragmentTests from './templateFragments';

export default [
{
label: 'Request tests',
name: 'Request tests',
tests: requestTests,
},
{
label: 'Check tests',
name: 'Check tests',
tests: checkTests,
},
{
label: 'Cancel tests',
name: 'Cancel tests',
tests: cancelTests,
},
{
name: 'Templates tests',
tests: templateTests,
},
{
name: 'Template Fragment tests',
tests: fragmentTests,
}
];
Loading

0 comments on commit 5166654

Please sign in to comment.