Skip to content

Commit f093b03

Browse files
committed
test: simplify tests setup and add test for provider
1 parent a31b093 commit f093b03

18 files changed

+392
-432
lines changed

npm-audit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h5 class="card-title">
5555
<div class="card">
5656
<div class="card-body">
5757
<h5 class="card-title">
58-
July 22nd 2020, 9:46:15 am
58+
July 23rd 2020, 5:46:38 am
5959
</h5>
6060
<p class="card-text">Last updated</p>
6161
</div>

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"author": "virk,adonisjs",
3535
"license": "MIT",
3636
"devDependencies": {
37+
"@adonisjs/application": "^2.0.0",
38+
"@adonisjs/config": "^1.1.0",
3739
"@adonisjs/encryption": "^2.0.6",
3840
"@adonisjs/fold": "^6.3.5",
3941
"@adonisjs/logger": "^2.1.0",

providers/HttpServerProvider.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,73 @@
99

1010
import { IocContract } from '@adonisjs/fold'
1111

12-
import { Server } from '../src/Server'
13-
import { Request } from '../src/Request'
14-
import { Response } from '../src/Response'
15-
import { HttpContext } from '../src/HttpContext'
16-
import { MiddlewareStore } from '../src/MiddlewareStore'
17-
1812
export default class HttpServerProvider {
19-
constructor(protected $container: IocContract) {}
13+
constructor(protected container: IocContract) {}
2014

2115
/**
2216
* Register request and response bindings to the container
2317
*/
24-
protected $registerRequestResponse() {
25-
this.$container.bind('Adonis/Core/Request', () => Request)
26-
this.$container.bind('Adonis/Core/Response', () => Response)
18+
protected registerRequestResponse() {
19+
this.container.singleton('Adonis/Core/Request', () => {
20+
return require('../src/Request').Request
21+
})
22+
23+
this.container.singleton('Adonis/Core/Response', () => {
24+
return require('../src/Response').Response
25+
})
2726
}
2827

2928
/**
3029
* Registering middleware store to the container
3130
*/
32-
protected $registerMiddlewareStore() {
33-
this.$container.bind('Adonis/Core/MiddlewareStore', () => MiddlewareStore)
31+
protected registerMiddlewareStore() {
32+
this.container.bind('Adonis/Core/MiddlewareStore', () => {
33+
return require('../src/MiddlewareStore').MiddlewareStore
34+
})
3435
}
3536

3637
/**
3738
* Registering the HTTP context
3839
*/
39-
protected $registerHTTPContext() {
40-
this.$container.bind('Adonis/Core/HttpContext', () => HttpContext)
40+
protected registerHTTPContext() {
41+
this.container.bind('Adonis/Core/HttpContext', () => {
42+
return require('../src/HttpContext').HttpContext
43+
})
4144
}
4245

4346
/**
4447
* Register the HTTP server
4548
*/
46-
protected $registerHttpServer() {
47-
this.$container.singleton('Adonis/Core/Server', () => {
48-
const Logger = this.$container.use('Adonis/Core/Logger')
49-
const Profiler = this.$container.use('Adonis/Core/Profiler')
50-
const Config = this.$container.use('Adonis/Core/Config')
51-
const Encryption = this.$container.use('Adonis/Core/Encryption')
49+
protected registerHttpServer() {
50+
this.container.singleton('Adonis/Core/Server', () => {
51+
const { Server } = require('../src/Server')
5252

53-
const config = Object.assign({ secret: Config.get('app.appKey') }, Config.get('app.http', {}))
54-
return new Server(this.$container, Logger, Profiler, Encryption, config)
53+
const Logger = this.container.use('Adonis/Core/Logger')
54+
const Profiler = this.container.use('Adonis/Core/Profiler')
55+
const Config = this.container.use('Adonis/Core/Config')
56+
const Encryption = this.container.use('Adonis/Core/Encryption')
57+
return new Server(this.container, Logger, Profiler, Encryption, Config.get('app.http', {}))
5558
})
5659
}
5760

5861
/**
5962
* Register the router. The router points to the instance of router used
6063
* by the middleware
6164
*/
62-
protected $registerRouter() {
63-
this.$container.singleton('Adonis/Core/Route', () => {
64-
return this.$container.use('Adonis/Core/Server').router
65+
protected registerRouter() {
66+
this.container.singleton('Adonis/Core/Route', () => {
67+
return this.container.use('Adonis/Core/Server').router
6568
})
6669
}
6770

6871
/**
6972
* Registering all bindings
7073
*/
7174
public register() {
72-
this.$registerRequestResponse()
73-
this.$registerMiddlewareStore()
74-
this.$registerHttpServer()
75-
this.$registerHTTPContext()
76-
this.$registerRouter()
75+
this.registerRequestResponse()
76+
this.registerMiddlewareStore()
77+
this.registerHttpServer()
78+
this.registerHTTPContext()
79+
this.registerRouter()
7780
}
7881
}

test-helpers/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @adonisjs/http-server
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import proxyaddr from 'proxy-addr'
11+
import { ServerConfig } from '@ioc:Adonis/Core/Server'
12+
import { RequestConfig } from '@ioc:Adonis/Core/Request'
13+
import { ResponseConfig } from '@ioc:Adonis/Core/Response'
14+
import { FakeLogger } from '@adonisjs/logger/build/standalone'
15+
import { Profiler } from '@adonisjs/profiler/build/standalone'
16+
import { Encryption } from '@adonisjs/encryption/build/standalone'
17+
18+
export const appSecret = 'averylongrandom32charslongsecret'
19+
20+
export const loggerConfig = {
21+
name: 'http-server',
22+
enabled: true,
23+
level: 'debug',
24+
}
25+
26+
export const requestConfig: RequestConfig = {
27+
allowMethodSpoofing: false,
28+
trustProxy: proxyaddr.compile('loopback'),
29+
subdomainOffset: 2,
30+
generateRequestId: true,
31+
}
32+
33+
export const responseConfig: ResponseConfig = {
34+
etag: false,
35+
jsonpCallbackName: 'callback',
36+
cookie: {
37+
maxAge: 90,
38+
path: '/',
39+
httpOnly: true,
40+
sameSite: false,
41+
secure: false,
42+
},
43+
}
44+
45+
export const serverConfig: ServerConfig = Object.assign({}, requestConfig, responseConfig)
46+
47+
export const logger = new FakeLogger(loggerConfig)
48+
export const profiler = new Profiler(__dirname, logger, { enabled: false })
49+
export const encryption = new Encryption({ secret: appSecret })

test/cookie-parser.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
*/
99

1010
import test from 'japa'
11-
import { Encryption } from '@adonisjs/encryption/build/standalone'
1211

12+
import { encryption } from '../test-helpers'
1313
import { CookieParser } from '../src/Cookie/Parser'
1414
import { CookieSerializer } from '../src/Cookie/Serializer'
1515

16-
const SECRET = Math.random().toFixed(36).substring(2, 38)
17-
const encryption = new Encryption({ secret: SECRET })
1816
const serializer = new CookieSerializer(encryption)
1917

2018
test.group('Cookie | parse', () => {

test/cookie-serializer.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99

1010
import test from 'japa'
1111
import { base64, MessageBuilder } from '@poppinss/utils'
12-
import { Encryption } from '@adonisjs/encryption/build/standalone'
1312

13+
import { encryption } from '../test-helpers'
1414
import { CookieSerializer } from '../src/Cookie/Serializer'
1515

16-
const SECRET = Math.random().toFixed(36).substring(2, 38)
17-
const encryption = new Encryption({ secret: SECRET })
18-
1916
test.group('Cookie | serialize', () => {
2017
test('serialize and sign cookie', (assert) => {
2118
const serializer = new CookieSerializer(encryption)

test/encrypted-cookie.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
*/
99

1010
import test from 'japa'
11-
import { Encryption } from '@adonisjs/encryption/build/standalone'
12-
import { pack, unpack, canUnpack } from '../src/Cookie/Drivers/Encrypted'
1311

14-
const SECRET = 'averylongradom32charactersstring'
15-
const encryption = new Encryption({ secret: SECRET })
12+
import { encryption } from '../test-helpers'
13+
import { pack, unpack, canUnpack } from '../src/Cookie/Drivers/Encrypted'
1614

1715
test.group('EncryptedCookie | Pack', () => {
1816
test('pack cookie as encrypted cookie', (assert) => {

test/http-context.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
*/
99

1010
import test from 'japa'
11-
import { HttpContext as BaseHttpContext } from '../src/HttpContext'
12-
import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext'
1311
import { Logger } from '@adonisjs/logger/build/standalone'
1412
import { Profiler } from '@adonisjs/profiler/build/standalone'
15-
import { Encryption } from '@adonisjs/encryption/build/standalone'
13+
import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext'
14+
15+
import { encryption } from '../test-helpers'
16+
import { HttpContext as BaseHttpContext } from '../src/HttpContext'
1617

1718
const HttpContext = (BaseHttpContext as any) as HttpContextConstructorContract
18-
const encryption = new Encryption({
19-
secret: 'averylongrandom32charslongsecret',
20-
})
2119

2220
test.group('Http Context', () => {
2321
test('create fake Http context instance', async (assert) => {

test/http-server-provider.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @adonisjs/events
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import test from 'japa'
11+
import { join } from 'path'
12+
import { Registrar, Ioc } from '@adonisjs/fold'
13+
import { Config } from '@adonisjs/config/build/standalone'
14+
import { Application } from '@adonisjs/application/build/standalone'
15+
16+
import { Router } from '../src/Router'
17+
import { Server } from '../src/Server'
18+
import { Request } from '../src/Request'
19+
import { Response } from '../src/Response'
20+
import { HttpContext } from '../src/HttpContext'
21+
import { MiddlewareStore } from '../src/MiddlewareStore'
22+
import { appSecret, serverConfig } from '../test-helpers'
23+
24+
test.group('Http Server Provider', () => {
25+
test('register http server provider', async (assert) => {
26+
const ioc = new Ioc()
27+
ioc.bind('Adonis/Core/Config', () => {
28+
return new Config({
29+
app: {
30+
http: serverConfig,
31+
appKey: appSecret,
32+
logger: {
33+
name: 'adonisjs',
34+
level: 'info',
35+
enabled: false,
36+
},
37+
},
38+
})
39+
})
40+
41+
ioc.bind('Adonis/Core/Application', () => {
42+
return new Application(__dirname, ioc, {}, {})
43+
})
44+
45+
const registrar = new Registrar(ioc, join(__dirname, '..'))
46+
47+
await registrar
48+
.useProviders([
49+
'@adonisjs/logger',
50+
'@adonisjs/profiler',
51+
'@adonisjs/encryption',
52+
'./providers/HttpServerProvider',
53+
])
54+
.registerAndBoot()
55+
56+
assert.instanceOf(ioc.use('Adonis/Core/Route'), Router)
57+
assert.deepEqual(ioc.use('Adonis/Core/Request'), Request)
58+
assert.deepEqual(ioc.use('Adonis/Core/Response'), Response)
59+
assert.instanceOf(ioc.use('Adonis/Core/Server'), Server)
60+
assert.deepEqual(ioc.use('Adonis/Core/MiddlewareStore'), MiddlewareStore)
61+
assert.deepEqual(ioc.use('Adonis/Core/HttpContext'), HttpContext)
62+
})
63+
})

0 commit comments

Comments
 (0)