This commit is contained in:
2024-07-19 12:38:11 +08:00
parent fccf33d953
commit c216c0a62f
34 changed files with 1302 additions and 1196 deletions

View File

@ -1,49 +1,49 @@
import { spawn } from "child_process";
import { spawn } from 'child_process';
import Axios from "axios";
import Axios from 'axios';
const Instance = Axios.create({
baseURL: "http://localhost:3000"
})
baseURL: 'http://localhost:3000',
});
spawn(`deno`, [
"run", "--allow-net", `${__dirname}/../dist/test.deno.js`
]);
spawn(`deno`, ['run', '--allow-net', `${__dirname}/../dist/test.deno.js`]);
const randomString = () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const randomString = () =>
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
describe("Test server", () => {
test("normal 200 response", async () => {
describe('Test server', () => {
test('normal 200 response', async () => {
expect.assertions(2);
await new Promise((r) => setTimeout(r, 200));
const { data, status } = await Instance.get("/");
const { data, status } = await Instance.get('/');
expect(status).toEqual(200);
expect(data).toEqual("200 OK");
})
expect(data).toEqual('200 OK');
});
test("post response", async () => {
test('post response', async () => {
expect.assertions(2);
const string = randomString();
const { data, status } = await Instance.post("/post", string);
const { data, status } = await Instance.post('/post', string);
expect(status).toEqual(200);
expect(data).toEqual(string);
})
});
test("change header and status code", async () => {
test('change header and status code', async () => {
expect.assertions(3);
const { data, status, headers } = await Instance.get("/header");
const { data, status, headers } = await Instance.get('/header');
expect(status).toEqual(204);
expect(headers["itis"]).toEqual("work");
expect(data).toEqual("");
})
expect(headers['itis']).toEqual('work');
expect(data).toEqual('');
});
test("get param", async () => {
test('get param', async () => {
expect.assertions(2);
const string = randomString();
@ -51,14 +51,14 @@ describe("Test server", () => {
expect(status).toEqual(200);
expect(data).toEqual(string);
})
});
test("chain interrupted", async () => {
test('chain interrupted', async () => {
expect.assertions(2);
const { data, status } = await Instance.get(`/info/foo`);
expect(status).toEqual(200);
expect(data).toEqual("hit");
})
})
expect(data).toEqual('hit');
});
});

View File

@ -1,45 +1,47 @@
import _ from "./test-server";
import _ from './test-server';
import Axios from "axios";
import Axios from 'axios';
_.listen(3000);
const Instance = Axios.create({
baseURL: "http://localhost:3000"
})
baseURL: 'http://localhost:3000',
});
const randomString = () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
const randomString = () =>
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
describe("Test server", () => {
test("normal 200 response", async () => {
describe('Test server', () => {
test('normal 200 response', async () => {
expect.assertions(2);
const { data, status } = await Instance.get("/");
const { data, status } = await Instance.get('/');
expect(status).toEqual(200);
expect(data).toEqual("200 OK");
})
expect(data).toEqual('200 OK');
});
test("post response", async () => {
test('post response', async () => {
expect.assertions(2);
const string = randomString();
const { data, status } = await Instance.post("/post", string);
const { data, status } = await Instance.post('/post', string);
expect(status).toEqual(200);
expect(data).toEqual(string);
})
});
test("change header and status code", async () => {
test('change header and status code', async () => {
expect.assertions(3);
const { data, status, headers } = await Instance.get("/header");
const { data, status, headers } = await Instance.get('/header');
expect(status).toEqual(204);
expect(headers["itis"]).toEqual("work");
expect(data).toEqual("");
})
expect(headers['itis']).toEqual('work');
expect(data).toEqual('');
});
test("get param", async () => {
test('get param', async () => {
expect.assertions(2);
const string = randomString();
@ -47,14 +49,14 @@ describe("Test server", () => {
expect(status).toEqual(200);
expect(data).toEqual(string);
})
});
test("chain interrupted", async () => {
test('chain interrupted', async () => {
expect.assertions(2);
const { data, status } = await Instance.get(`/info/foo`);
expect(status).toEqual(200);
expect(data).toEqual("hit");
})
})
expect(data).toEqual('hit');
});
});

View File

@ -1,3 +1,3 @@
import App from "./test-server";
import App from './test-server';
App.listen(3000);
App.listen(3000);

View File

@ -2,24 +2,49 @@ import * as handlersJS from '../index';
const App = new handlersJS.rootRouter();
App.binding("/", App.create("GET", async () => "200 OK"));
App.binding(
'/',
App.create('GET', async () => '200 OK'),
);
App.binding("/post", App.create("POST", async (request: handlersJS.request<any>) => request.body));
App.binding(
'/post',
App.create(
'POST',
async (request: handlersJS.request<any>) => request.body,
),
);
App.binding("/header", App.create("GET", async () => {
const response = new handlersJS.response<any>("");
response.status = 204;
response.headers.set("itis", "work");
return response;
}));
App.binding(
'/header',
App.create('GET', async () => {
const response = new handlersJS.response<any>('');
response.status = 204;
response.headers.set('itis', 'work');
return response;
}),
);
App
.route("/info/(.*)")
.binding("/foo", App.create("GET", (): Promise<handlersJS.response<any>> => new Promise(resolve => {
throw new handlersJS.response("hit")
})))
.binding("/(.*)", App.create("GET", async (request: handlersJS.request<any>) => request.params[0] ?? "not found"));
App.route('/info/(.*)')
.binding(
'/foo',
App.create(
'GET',
(): Promise<handlersJS.response<any>> =>
new Promise((resolve) => {
throw new handlersJS.response('hit');
}),
),
)
.binding(
'/(.*)',
App.create(
'GET',
async (request: handlersJS.request<any>) =>
request.params[0] ?? 'not found',
),
);
App.useMappingAdapter();
export default App;
export default App;