Skip to content

Commit

Permalink
Merge branches 'master' and 'develop' of https://github.com/htdangkho…
Browse files Browse the repository at this point in the history
…a/pure-http into develop
  • Loading branch information
htdangkhoa committed May 3, 2023
2 parents c977de5 + e6b99a7 commit 91ecfe7
Show file tree
Hide file tree
Showing 13 changed files with 1,534 additions and 1,828 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ app.get('/', (req, res) => {
app.listen(3000);
```

Exist server:
Existing server:

```js
const http = require('http');
Expand Down
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ declare module 'pure-http' {

path?: string;

query?: Record<string, string>;
query?: Record<string, any>;

params?: {
[key: string]: any,
wild?: string
};

body?: any;

Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pure-http",
"version": "3.3.0",
"version": "3.3.1",
"description": "The simple web framework for Node.js with zero dependencies.",
"keywords": [
"pure-http",
Expand Down Expand Up @@ -41,30 +41,30 @@
},
"dependencies": {},
"devDependencies": {
"@tinyhttp/app": "^1.2.17",
"@tinyhttp/app": "1.3.15",
"body-parser": "^1.19.0",
"commander": "^7.1.0",
"commander": "^8.2.0",
"connect-timeout": "^1.9.0",
"consolidate": "^0.16.0",
"cookie": "^0.4.1",
"cookie-parser": "^1.4.5",
"coveralls": "^3.1.0",
"coveralls": "^3.1.1",
"ejs": "^3.1.6",
"eslint": "^7.22.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.3.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^24.4.2",
"eslint-plugin-prettier": "^4.0.0",
"express": "^4.17.1",
"fastify": "^3.14.0",
"fastify-express": "^0.3.2",
"husky": "^5.1.3",
"jest": "^26.6.3",
"fastify": "^3.21.6",
"fastify-express": "^0.3.3",
"husky": "^7.0.2",
"jest": "^27.2.2",
"pem": "^1.14.4",
"prettier": "^2.2.1",
"prettier": "^2.4.1",
"serve-static": "^1.14.1",
"supertest": "^6.1.3",
"supertest": "^6.1.6",
"swig": "^1.4.2",
"wrk": "^1.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/basic/error.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('The middleware is not a function.', () => {
it(`Should be throw the error.`, async (done) => {
it(`Should be throw the error.`, (done) => {
// eslint-disable-next-line global-require
const pureHttp = require('../..');

Expand Down
8 changes: 2 additions & 6 deletions tests/basic/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@ describe('POST /', () => {
});

describe('ALL /status', () => {
it('An object should be returned.', async (done) => {
it('An object should be returned.', async () => {
const res = await request.post('/status');

expect(res.body).toMatchObject({ success: true });

expect(res.status).toBe(302);

done();
});
});

describe('ALL /get-header', () => {
it('The status should be 503.', async (done) => {
it('The status should be 503.', async () => {
const res = await request.get('/get-header');

expect(typeof res.text).toBe('string');

done();
});
});

Expand Down
32 changes: 8 additions & 24 deletions tests/cookie/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const pureHttp = require('../..');

describe('cookie', () => {
describe('res.cookie', () => {
it(`The 'foo' should be equal 'bar'.`, async (done) => {
it(`The 'foo' should be equal 'bar'.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', 'bar');
Expand All @@ -19,11 +19,9 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(cookies.includes('foo=bar; Path=/')).toBe(true);

done();
});

it(`The 'foo' should be equal 'bar' with sameSite=true.`, async (done) => {
it(`The 'foo' should be equal 'bar' with sameSite=true.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', 'bar', { sameSite: true });
Expand All @@ -37,11 +35,9 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(cookies.includes('foo=bar; Path=/; SameSite=Strict')).toBe(true);

done();
});

it(`The 'foo' should be equal 'bar' with sameSite=strict.`, async (done) => {
it(`The 'foo' should be equal 'bar' with sameSite=strict.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', 'bar', { sameSite: 'strict' });
Expand All @@ -55,11 +51,9 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(cookies.includes('foo=bar; Path=/; SameSite=Strict')).toBe(true);

done();
});

it(`The 'foo' should be equal 'bar' with sameSite=lax.`, async (done) => {
it(`The 'foo' should be equal 'bar' with sameSite=lax.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', 'bar', { sameSite: 'lax' });
Expand All @@ -73,11 +67,9 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(cookies.includes('foo=bar; Path=/; SameSite=Lax')).toBe(true);

done();
});

it(`The 'foo' should be equal 'bar' with sameSite=none.`, async (done) => {
it(`The 'foo' should be equal 'bar' with sameSite=none.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', 'bar', { sameSite: 'none' });
Expand All @@ -91,11 +83,9 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(cookies.includes('foo=bar; Path=/; SameSite=None')).toBe(true);

done();
});

it(`The 'foo' should be equal 'bar' with signed=true.`, async (done) => {
it(`The 'foo' should be equal 'bar' with signed=true.`, async () => {
const app = pureHttp();
app.use(cookieParser('cat'));
app.use('/', (req, res) => {
Expand All @@ -112,11 +102,9 @@ describe('cookie', () => {
const expectValue = encodeURIComponent(`s:${sign('bar', 'cat')}`);

expect(cookies.includes(`foo=${expectValue}; Path=/`)).toBe(true);

done();
});

it(`The 'foo' should be equal '{ "ping": "pong" }'.`, async (done) => {
it(`The 'foo' should be equal '{ "ping": "pong" }'.`, async () => {
const app = pureHttp();
app.use('/', (req, res) => {
res.cookie('foo', { ping: 'pong' });
Expand All @@ -134,8 +122,6 @@ describe('cookie', () => {
);

expect(cookies.includes(`foo=${expectValue}; Path=/`)).toBe(true);

done();
});

it(`The status should be 500 with wrong maxAge type.`, async () => {
Expand Down Expand Up @@ -191,7 +177,7 @@ describe('cookie', () => {
});

describe('res.clearCookie', () => {
it(`The 'foo' should be empty.`, async (done) => {
it(`The 'foo' should be empty.`, async () => {
const app = pureHttp();
app.use((req, res, next) => {
res.cookie('foo', 'bar');
Expand All @@ -210,8 +196,6 @@ describe('cookie', () => {
const cookies = res.headers['set-cookie'];

expect(!!cookies.find((cookie) => cookie.startsWith('foo=;'))).toBe(true);

done();
});
});

Expand Down
8 changes: 2 additions & 6 deletions tests/error-handling/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('ALL /error', () => {
});

describe('ALL /not-found', () => {
it(`The status should be 404 and the response text should be 'Not found.'.`, async (done) => {
it(`The status should be 404 and the response text should be 'Not found.'.`, async () => {
const app = require('./server')();
app.use(notFoundMiddleware());

Expand All @@ -51,14 +51,12 @@ describe('ALL /not-found', () => {
expect(res.statusCode).toBe(404);

expect(res.text).toBe('Not found.');

done();
});
});

// Only error middleware
describe('ALL /error', () => {
it(`The status should be 500 and the response text should be 'Error.'.`, async (done) => {
it(`The status should be 500 and the response text should be 'Error.'.`, async () => {
const app = require('./server')();
app.use(errorMiddleware());

Expand All @@ -69,8 +67,6 @@ describe('ALL /error', () => {
expect(res.statusCode).toBe(500);

expect(res.text).toBe('Error.');

done();
});
});

Expand Down
4 changes: 1 addition & 3 deletions tests/mime/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ const path = require('path');
const { getMimeType } = require('../../lib/mime');

describe(`Get the mimetype from file's path.`, () => {
it(`Should be return 'text/html'.`, async (done) => {
it(`Should be return 'text/html'.`, async () => {
const pathname = path.resolve(process.cwd(), 'tests/mime/.env');

const ext = getMimeType(pathname);

expect(ext).toBe('text/html');

done();
});
});
8 changes: 2 additions & 6 deletions tests/params/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@ const supertest = require('supertest');
const pureHttp = require('../..');

describe('params', () => {
it(`The uid from response should be '1'.`, async (done) => {
it(`The uid from response should be '1'.`, async () => {
const app = pureHttp();
app.use('/wild/:uid/*', (req, res) => res.send({ uid: req.params.uid }));

const request = supertest(app);
const res = await request.get('/wild/1/*');

expect(res.body.uid).toBe('1');

done();
});

it(`The uid from response should be '1'.`, async (done) => {
it(`The uid from response should be '1'.`, async () => {
const app = pureHttp();
app.use('/movies/:name?/:year?', (req, res) => res.send(req.params));

const request = supertest(app);
const res = await request.get('/movies/star-wars');

expect(res.body.name).toBe('star-wars');

done();
});

it(`The status should be 200.`, async () => {
Expand Down
8 changes: 2 additions & 6 deletions tests/protocol/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const supertest = require('supertest');
const pureHttp = require('../..');

describe('ALL /protocol', () => {
it(`should respect X-Forwarded-Proto.`, async (done) => {
it(`should respect X-Forwarded-Proto.`, async () => {
const app = pureHttp();
app.use((req, res) => {
res.send(req.protocol);
Expand All @@ -14,11 +14,9 @@ describe('ALL /protocol', () => {
const res = await request.get('/').set('X-Forwarded-Proto', 'https');

expect(res.text).toBe('https');

done();
});

it(`should default to the socket addr if X-Forwarded-Proto not present.`, async (done) => {
it(`should default to the socket addr if X-Forwarded-Proto not present.`, async () => {
const app = pureHttp();
app.use((req, res) => {
req.connection.encrypted = true;
Expand All @@ -31,7 +29,5 @@ describe('ALL /protocol', () => {
const res = await request.get('/');

expect(res.text).toBe('https');

done();
});
});
Loading

0 comments on commit 91ecfe7

Please sign in to comment.