Skip to content

release v1 #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 10 additions & 58 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import aspida from '@aspida/axios';
import aspidaFetch from '@aspida/node-fetch';
import axios from 'axios';
import { plainToInstance } from 'class-transformer';
import { validateOrReject } from 'class-validator';
import express from 'express';
import FormData from 'form-data';
import fs from 'fs';
Expand All @@ -22,30 +20,13 @@ const client = api(aspida(undefined, { baseURL }));
const fetchClient = api(aspidaFetch(undefined, { baseURL: subBaseURL, throwHttpErrors: true }));
let server: Server;
let subServer: Server;
let subServerPlainToInstanceCallCount = 0;
let subServerValidateOrRejectCallCount = 0;

beforeAll(cb => {
server = frourio(express()).listen(port, () => {
subServer = frourio(express(), {
basePath: subBasePath,
plainToInstance: (cls, object, options): object => {
subServerPlainToInstanceCallCount++;
return plainToInstance(cls, object, options);
},
validateOrReject: (instance, options): Promise<void> => {
subServerValidateOrRejectCallCount++;
return validateOrReject(instance, options);
},
}).listen(subPort, cb);
subServer = frourio(express(), { basePath: subBasePath }).listen(subPort, cb);
});
});

beforeEach(() => {
subServerPlainToInstanceCallCount = 0;
subServerValidateOrRejectCallCount = 0;
});

afterAll(cb => {
rimraf.sync('servers/all/.upload');
server.close(() => {
Expand Down Expand Up @@ -167,9 +148,6 @@ test('PUT: JSON', async () => {
});

test('POST: formdata', async () => {
expect(subServerPlainToInstanceCallCount).toBe(0);
expect(subServerValidateOrRejectCallCount).toBe(0);

const port = '3000';
const fileName = 'tsconfig.json';

Expand All @@ -184,9 +162,6 @@ test('POST: formdata', async () => {
expect(res1.data.port).toBe(port);
expect(res1.data.fileName).toBe(fileName);

expect(subServerPlainToInstanceCallCount).toBe(0);
expect(subServerValidateOrRejectCallCount).toBe(0);

const form2 = new FormData();
const fileST2 = fs.createReadStream(fileName);
form2.append('port', port);
Expand All @@ -197,10 +172,6 @@ test('POST: formdata', async () => {
});
expect(res2.data.port).toBe(port);
expect(res2.data.fileName).toBe(fileName);

// 2 = query + body
expect(subServerPlainToInstanceCallCount).toBe(2);
expect(subServerValidateOrRejectCallCount).toBe(2);
});

test('PUT: zod validations', async () => {
Expand Down Expand Up @@ -260,9 +231,7 @@ test('POST: multi file upload', async () => {
form.append('icon', fileST);
form.append('files', fileST);
form.append('files', fileST);
const res = await axios.post(`${baseURL}/multiForm`, form, {
headers: form.getHeaders(),
});
const res = await axios.post(`${baseURL}/multiForm`, form, { headers: form.getHeaders() });

expect(res.data).toEqual({
requiredArr: 0,
Expand All @@ -280,12 +249,10 @@ test('POST: 400', async () => {
const fileST = fs.createReadStream(fileName);
form.append('name', 'sample');
form.append('vals', 'dammy');
form.append('icon', fileST);
form.append('files', fileST);

await expect(
axios.post(`${baseURL}/multiForm`, form, {
headers: form.getHeaders(),
})
axios.post(`${baseURL}/multiForm`, form, { headers: form.getHeaders() })
).rejects.toHaveProperty('response.status', 400);
});

Expand All @@ -294,10 +261,7 @@ test('POST: nested validation', async () => {
body: {
id: 123,
name: 'foo',
location: {
country: 'JP',
stateProvince: 'Tokyo',
},
location: { country: 'JP', stateProvince: 'Tokyo' },
},
});
expect(res1.status).toBe(204);
Expand All @@ -310,10 +274,7 @@ test('POST: nested validation', async () => {
location: {
country: 'JP',
stateProvince: 'Tokyo',
extra1: {
extra1a: 'bar',
extra1b: 'baz',
},
extra1: { extra1a: 'bar', extra1b: 'baz' },
},
extra2: 'qux',
} as any,
Expand All @@ -328,10 +289,7 @@ test('POST: 400 (nested validation)', async () => {
body: {
id: '123',
name: 'foo',
location: {
country: 'JP',
stateProvince: 'Tokyo',
},
location: { country: 'JP', stateProvince: 'Tokyo' },
} as any,
})
).rejects.toHaveProperty('response.status', 400);
Expand All @@ -349,10 +307,7 @@ test('POST: 400 (nested validation)', async () => {
body: {
id: 123,
name: 'foo',
location: {
country: 'XX',
stateProvince: 'Tokyo',
},
location: { country: 'JPN', stateProvince: 'Tokyo' },
} as any,
})
).rejects.toHaveProperty('response.status', 400);
Expand All @@ -363,10 +318,7 @@ test('POST: 400 (nested validation)', async () => {
body: {
id: 123,
name: 'foo',
location: {
country: 'JP',
stateProvince: 1234,
},
location: { country: 'JP', stateProvince: 1234 },
} as any,
})
).rejects.toHaveProperty('response.status', 400);
Expand All @@ -389,7 +341,7 @@ test('controller dependency injection', async () => {
}))(express());

await expect(
injectedController.get({
injectedController.get.handler({
query: {
id,
requiredNum: 1,
Expand Down
Loading