Skip to content

Commit 99cdefd

Browse files
committed
feat: Tests adjusts
1 parent b6daa66 commit 99cdefd

File tree

7 files changed

+58
-73
lines changed

7 files changed

+58
-73
lines changed

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './src/server'
1+
export * from './src/SecJS'
22

3-
export * from './src/Contracts/RouteContract'
4-
export * from './src/Contracts/HandlerContract'
3+
export * from './src/Context/Request/SecRequest'
4+
export * from './src/Context/Response/SecResponse'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/http",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "",
55
"scripts": {
66
"build": "tsc",

src/Context/Request/SecRequest.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
import { SecRequestContract } from '@secjs/contracts'
12
import { Route } from '@secjs/utils/src/Classes/Route'
2-
import { IncomingMessage } from 'http'
3-
import { SecRequestContract, InternalRouteContract } from '@secjs/contracts'
43

54
export class SecRequest implements SecRequestContract {
65
private _ip = ''
@@ -14,20 +13,17 @@ export class SecRequest implements SecRequestContract {
1413
private _originalUrl = ''
1514
private routeUtils = new Route()
1615

17-
constructor(
18-
body: any,
19-
route: InternalRouteContract,
20-
request: IncomingMessage,
21-
) {
22-
this._ip = request.socket.remoteAddress
23-
this._body = body ? JSON.parse(body) : {}
16+
constructor(request: any) {
17+
this._body = request.body
2418
this._method = request.method
25-
this._params = this.routeUtils.getParamsValue(route.path, request.url) || {}
26-
this._queries = this.routeUtils.getQueryParamsValue(request.url) || {}
19+
this._originalUrl = request.url
2720
this._headers = request.headers
21+
this._baseUrl = request.route.path
22+
this._ip = request.socket.remoteAddress
2823
this._fullUrl = this.routeUtils.removeQueryParams(request.url)
29-
this._baseUrl = route.path
30-
this._originalUrl = request.url
24+
this._queries = this.routeUtils.getQueryParamsValue(request.url) || {}
25+
this._params =
26+
this.routeUtils.getParamsValue(request.route.path, request.url) || {}
3127
}
3228

3329
payload(payload: string, defaultValue?: string): any {

src/Context/Response/SecResponse.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class SecResponse implements SecResponseContract {
2323

2424
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2525
json(data?: any): void {
26+
this.vanillaResponse.setHeader('Accept', 'application/json')
27+
this.vanillaResponse.setHeader('Content-Type', 'application/json')
28+
2629
this.vanillaResponse.end(JSON.stringify(data))
2730
}
2831

src/SecJS.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import constants from './constants'
33
import { Route } from '@secjs/utils/src/Classes/Route'
44
import { SecRequest } from './Context/Request/SecRequest'
55
import { SecResponse } from './Context/Response/SecResponse'
6+
import { createServer, IncomingMessage, Server } from 'http'
67
import { InternalRouteContract, SecHandlerContract } from '@secjs/contracts'
7-
import { createServer, IncomingMessage, Server, ServerResponse } from 'http'
88

99
export class SecJS {
1010
private nodeServer: Server
@@ -18,6 +18,8 @@ export class SecJS {
1818
buffers.push(chunk)
1919
}
2020

21+
if (!buffers.length) return {}
22+
2123
return JSON.parse(Buffer.concat(buffers).toString())
2224
}
2325

@@ -46,24 +48,20 @@ export class SecJS {
4648
}
4749

4850
listen(port?: number, cb?: () => void): void {
49-
this.nodeServer = createServer(
50-
async (request: IncomingMessage, response: ServerResponse) => {
51-
const { url, method } = request
52-
53-
response.writeHead(200, {
54-
Accept: 'application/json',
55-
'Content-Type': 'application/json',
56-
})
57-
58-
const route = this.getRoute(url, method)
59-
60-
return route.handler({
61-
next: () => console.log('next'),
62-
request: new SecRequest(this.getBody(request), route, request),
63-
response: new SecResponse(response),
64-
})
65-
},
66-
)
51+
this.nodeServer = createServer(async (request: any, response: any) => {
52+
const { url, method } = request
53+
54+
const route = this.getRoute(url, method)
55+
56+
request.route = route
57+
request.body = await this.getBody(request)
58+
59+
return route.handler({
60+
next: () => console.log('next'),
61+
request: new SecRequest(request),
62+
response: new SecResponse(response),
63+
})
64+
})
6765

6866
this.nodeServer.listen(port || constants.PORT, cb)
6967
}

src/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export default {
1313
params: [],
1414
matcher: /\//,
1515
handler: ({ response }: SecContextContract): any => {
16-
return response
17-
.status(404)
18-
.json(JSON.stringify({ message: 'Not found!' }))
16+
return response.status(404).json({ message: 'Not found!' })
1917
},
2018
},
2119
}

tests/server.spec.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,54 @@
11
import supertest from 'supertest'
22

3-
import { Sec } from '../src/server'
4-
import { Context } from './../src/Contracts/HandlerContract'
3+
import { SecJS } from '../src/SecJS'
4+
import { SecContextContract } from '@secjs/contracts'
55

66
describe('\n Server', () => {
7-
it('should be able to create a Http server using SecJS', async () => {
8-
const server = new Sec()
7+
let server: SecJS
8+
9+
beforeEach(() => {
10+
server = new SecJS()
11+
12+
server.get('/tests', (ctx: SecContextContract) => {
13+
ctx.response.status(200).json({ data: [{ id: 1, name: 'Test 1' }] })
14+
})
15+
server.get(
16+
'/tests/:id/unitaries/:unitaries_id',
17+
(ctx: SecContextContract) => {
18+
ctx.response
19+
.status(200)
20+
.json({ data: { id: 1, name: 'Unit Test 1', testId: 1 } })
21+
},
22+
)
923

1024
server.listen()
25+
})
26+
27+
afterEach(() => {
28+
server.close()
29+
})
1130

31+
it('should be able to create a Http server using SecJS', async () => {
1232
const response = await supertest('http://localhost:4040')
1333
.get('/')
1434
.expect(404)
1535

1636
expect(response.status).toBe(404)
1737
expect(response.body).toStrictEqual({ message: 'Not found!' })
1838
expect(response.get('Content-Type')).toBe('application/json')
19-
20-
server.close()
2139
})
2240

2341
it('should be able to create routes with handlers', async () => {
24-
const server = new Sec()
25-
26-
server.get('/tests', (ctx: Context) => {
27-
ctx.response.writeHead(200, { 'Content-Type': 'application/json' })
28-
29-
ctx.response.write(JSON.stringify({ data: [{ id: 1, name: 'Test 1' }] }))
30-
ctx.response.end()
31-
})
32-
33-
server.listen()
34-
3542
const response = await supertest('http://localhost:4040')
3643
.get('/tests')
3744
.expect(200)
3845

3946
expect(response.status).toBe(200)
4047
expect(response.body).toStrictEqual({ data: [{ id: 1, name: 'Test 1' }] })
4148
expect(response.get('Content-Type')).toBe('application/json')
42-
43-
server.close()
4449
})
4550

4651
it('should be able to create routes with params', async () => {
47-
const server = new Sec()
48-
49-
server.get('/tests/:id/unitaries/:unitaries_id', (ctx: Context) => {
50-
ctx.response.writeHead(200, { 'Content-Type': 'application/json' })
51-
52-
ctx.response.write(
53-
JSON.stringify({ data: { id: 1, name: 'Unit Test 1', testId: 1 } }),
54-
)
55-
ctx.response.end()
56-
})
57-
58-
server.listen()
59-
6052
const response = await supertest('http://localhost:4040')
6153
.get('/tests/1/unitaries/1')
6254
.expect(200)
@@ -66,7 +58,5 @@ describe('\n Server', () => {
6658
data: { id: 1, name: 'Unit Test 1', testId: 1 },
6759
})
6860
expect(response.get('Content-Type')).toBe('application/json')
69-
70-
server.close()
7161
})
7262
})

0 commit comments

Comments
 (0)