adapt-authoring-server 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,142 +1,142 @@
1
- const { App } = require('adapt-authoring-core');
2
- const express = require('express');
3
- const Router = require('../lib/Router');
4
- const should = require('should');
5
- const supertest = require('supertest');
6
-
7
- describe('Server router', function() {
8
- before(function() {
9
- this.createRouter = (r = 'test') => new Router(r, express());
10
- this.testRequest = (router, done, expectedStatus = 200) => {
11
- const l = router.parentRouter.listen(undefined, function() {
12
- supertest(router.parentRouter)
13
- .get(`${router.path}`)
14
- .expect(expectedStatus)
15
- .end((e, res) => l.close(done))
16
- });
17
- };
18
- });
19
- describe('#addRoute()', function() {
20
- const r1 = { route: 'test1', handlers: { get: () => {} } };
21
- const r2 = { route: 'test2', handlers: { get: () => {} } };
22
- const r3 = { route: 'test3', handlers: { get: () => {} } };
23
- it('should add a route function to the stack', function() {
24
- const router = this.createRouter();
25
- router.addRoute(r1);
26
- router.routes.should.containEql(r1);
27
- });
28
- it('should add multiple routes to the stack', function() {
29
- const router = this.createRouter();
30
- router.addRoute(r1, r2);
31
- router.routes.should.containEql(r2);
32
- });
33
- it('should return router reference so as to be chainable', function() {
34
- const router = this.createRouter();
35
- const r = router.addRoute(r2);
36
- r.should.equal(router);
37
- });
38
- it('should call route handler on client request', function(done) {
39
- const router = this.createRouter();
40
- router.addRoute({
41
- route: '/',
42
- handlers: { get: (req, res, next) => res.end() }
43
- });
44
- router.init();
45
- this.testRequest(router, done);
46
- });
47
- });
48
- describe('#addMiddleware()', function() {
49
- const m1 = (req, res, next) => next();
50
- const m2 = (req, res, next) => next();
51
- it('should add a middleware function to the stack', function() {
52
- const router = this.createRouter();
53
- router.addMiddleware(m1);
54
- router.middleware.should.containEql(m1);
55
- });
56
- it('should add multiple middleware functions to the stack', function() {
57
- const router = this.createRouter();
58
- router.addMiddleware(m1, m2);
59
- router.middleware.should.containEql(m2);
60
- });
61
- it('should return router reference so as to be chainable', function() {
62
- const router = this.createRouter();
63
- const r = router.addMiddleware(m2);
64
- r.should.equal(router);
65
- });
66
- it('should call custom middleware on client request', function(done) {
67
- const router = this.createRouter();
68
- router.addRoute({
69
- route: '/',
70
- handlers: { get: (req, res, next) => res.status(500).end() }
71
- });
72
- router.addMiddleware((req, res, next) => res.status(200).end());
73
- router.init();
74
- this.testRequest(router, done);
75
- });
76
- });
77
- describe('#createChildRouter()', function() {
78
- let parent, child;
79
- before(function() {
80
- parent = this.createRouter();
81
- child = parent.createChildRouter('child');
82
- });
83
- it('should return a router instance', function() {
84
- child.should.be.instanceof(Router);
85
- });
86
- it('should expose specified route', function() {
87
- child.route.should.equal('child');
88
- });
89
- it('should be added to child routers', function() {
90
- parent.childRouters.should.containEql(child);
91
- });
92
- it('should reference current router as parent', function() {
93
- child.parentRouter.should.equal(parent);
94
- });
95
- });
96
- describe('#path()', function() {
97
- it('should generate the endpoint of the router', function() {
98
- const child = new Router('child', new Router('parent', {}));
99
- child.path.should.equal('/parent/child');
100
- });
101
- });
102
- describe('#handleRequest()', function() {
103
- it('should invoke listeners to Server#requestHook', function(done) {
104
- let isDone = false;
105
- App.instance.getModule('server').requestHook.tap(() => {
106
- if(!isDone) done();
107
- isDone = true;
108
- });
109
- const router = this.createRouter();
110
- router.addRoute({
111
- route: '/',
112
- handlers: { get: (req, res, next) => {} }
113
- });
114
- router.init();
115
- this.testRequest(router, () => {}, 500);
116
- });
117
- });
118
- describe('#genericNotFoundHandler()', function() {
119
- it('should return 404 HTTP status', function(done) {
120
- const router = this.createRouter();
121
- router.addRoute({
122
- route: '/',
123
- handlers: { get: (req, res, next) => res.end() }
124
- });
125
- router.addMiddleware(router.genericNotFoundHandler);
126
- router.init();
127
- this.testRequest(router, done, 404);
128
- });
129
- });
130
- describe('#genericErrorHandler()', function() {
131
- it('should return error HTTP status', function(done) {
132
- const router = this.createRouter();
133
- router.addRoute({
134
- route: '/',
135
- handlers: { get: (req, res, next) => next(new Error('Something bad happened')) }
136
- });
137
- router.addMiddleware(router.genericErrorHandler);
138
- router.init();
139
- this.testRequest(router, done, 500);
140
- });
141
- });
142
- });
1
+ const { App } = require('adapt-authoring-core');
2
+ const express = require('express');
3
+ const Router = require('../lib/Router');
4
+ const should = require('should');
5
+ const supertest = require('supertest');
6
+
7
+ describe('Server router', function() {
8
+ before(function() {
9
+ this.createRouter = (r = 'test') => new Router(r, express());
10
+ this.testRequest = (router, done, expectedStatus = 200) => {
11
+ const l = router.parentRouter.listen(undefined, function() {
12
+ supertest(router.parentRouter)
13
+ .get(`${router.path}`)
14
+ .expect(expectedStatus)
15
+ .end((e, res) => l.close(done))
16
+ });
17
+ };
18
+ });
19
+ describe('#addRoute()', function() {
20
+ const r1 = { route: 'test1', handlers: { get: () => {} } };
21
+ const r2 = { route: 'test2', handlers: { get: () => {} } };
22
+ const r3 = { route: 'test3', handlers: { get: () => {} } };
23
+ it('should add a route function to the stack', function() {
24
+ const router = this.createRouter();
25
+ router.addRoute(r1);
26
+ router.routes.should.containEql(r1);
27
+ });
28
+ it('should add multiple routes to the stack', function() {
29
+ const router = this.createRouter();
30
+ router.addRoute(r1, r2);
31
+ router.routes.should.containEql(r2);
32
+ });
33
+ it('should return router reference so as to be chainable', function() {
34
+ const router = this.createRouter();
35
+ const r = router.addRoute(r2);
36
+ r.should.equal(router);
37
+ });
38
+ it('should call route handler on client request', function(done) {
39
+ const router = this.createRouter();
40
+ router.addRoute({
41
+ route: '/',
42
+ handlers: { get: (req, res, next) => res.end() }
43
+ });
44
+ router.init();
45
+ this.testRequest(router, done);
46
+ });
47
+ });
48
+ describe('#addMiddleware()', function() {
49
+ const m1 = (req, res, next) => next();
50
+ const m2 = (req, res, next) => next();
51
+ it('should add a middleware function to the stack', function() {
52
+ const router = this.createRouter();
53
+ router.addMiddleware(m1);
54
+ router.middleware.should.containEql(m1);
55
+ });
56
+ it('should add multiple middleware functions to the stack', function() {
57
+ const router = this.createRouter();
58
+ router.addMiddleware(m1, m2);
59
+ router.middleware.should.containEql(m2);
60
+ });
61
+ it('should return router reference so as to be chainable', function() {
62
+ const router = this.createRouter();
63
+ const r = router.addMiddleware(m2);
64
+ r.should.equal(router);
65
+ });
66
+ it('should call custom middleware on client request', function(done) {
67
+ const router = this.createRouter();
68
+ router.addRoute({
69
+ route: '/',
70
+ handlers: { get: (req, res, next) => res.status(500).end() }
71
+ });
72
+ router.addMiddleware((req, res, next) => res.status(200).end());
73
+ router.init();
74
+ this.testRequest(router, done);
75
+ });
76
+ });
77
+ describe('#createChildRouter()', function() {
78
+ let parent, child;
79
+ before(function() {
80
+ parent = this.createRouter();
81
+ child = parent.createChildRouter('child');
82
+ });
83
+ it('should return a router instance', function() {
84
+ child.should.be.instanceof(Router);
85
+ });
86
+ it('should expose specified route', function() {
87
+ child.route.should.equal('child');
88
+ });
89
+ it('should be added to child routers', function() {
90
+ parent.childRouters.should.containEql(child);
91
+ });
92
+ it('should reference current router as parent', function() {
93
+ child.parentRouter.should.equal(parent);
94
+ });
95
+ });
96
+ describe('#path()', function() {
97
+ it('should generate the endpoint of the router', function() {
98
+ const child = new Router('child', new Router('parent', {}));
99
+ child.path.should.equal('/parent/child');
100
+ });
101
+ });
102
+ describe('#handleRequest()', function() {
103
+ it('should invoke listeners to Server#requestHook', function(done) {
104
+ let isDone = false;
105
+ App.instance.getModule('server').requestHook.tap(() => {
106
+ if(!isDone) done();
107
+ isDone = true;
108
+ });
109
+ const router = this.createRouter();
110
+ router.addRoute({
111
+ route: '/',
112
+ handlers: { get: (req, res, next) => {} }
113
+ });
114
+ router.init();
115
+ this.testRequest(router, () => {}, 500);
116
+ });
117
+ });
118
+ describe('#genericNotFoundHandler()', function() {
119
+ it('should return 404 HTTP status', function(done) {
120
+ const router = this.createRouter();
121
+ router.addRoute({
122
+ route: '/',
123
+ handlers: { get: (req, res, next) => res.end() }
124
+ });
125
+ router.addMiddleware(router.genericNotFoundHandler);
126
+ router.init();
127
+ this.testRequest(router, done, 404);
128
+ });
129
+ });
130
+ describe('#genericErrorHandler()', function() {
131
+ it('should return error HTTP status', function(done) {
132
+ const router = this.createRouter();
133
+ router.addRoute({
134
+ route: '/',
135
+ handlers: { get: (req, res, next) => next(new Error('Something bad happened')) }
136
+ });
137
+ router.addMiddleware(router.genericErrorHandler);
138
+ router.init();
139
+ this.testRequest(router, done, 500);
140
+ });
141
+ });
142
+ });
@@ -1,51 +1,51 @@
1
- const { Hook } = require('adapt-authoring-core');
2
- const Router = require('../lib/Router');
3
- const ServerModule = require('../lib/ServerModule');
4
- const should = require('should');
5
- const supertest = require('supertest');
6
-
7
- describe('Server module', function() {
8
- before(function() {
9
- this.server = new ServerModule(global.ADAPT.app, { name: require('../package.json').name });
10
- });
11
- describe('#constructor()', function() {
12
- it('should expose the Express application', function() {
13
- false.should.be.true();
14
- });
15
- it('should expose a `root` Router', function() {
16
- this.server.root.should.be.an.instanceof(Router);
17
- });
18
- it('should expose an `api` Router', function() {
19
- this.server.api.should.be.an.instanceof(Router);
20
- });
21
- it('should expose a hook to modify requests', function() {
22
- this.server.requestHook.should.be.an.instanceof(Hook);
23
- });
24
- });
25
- describe('#url()', function() {
26
- it('should return the URL of the server', function() {
27
- this.server.url.should.be.a.String();
28
- });
29
- });
30
- describe('#static()', function() {
31
- it('should expose Express#static', function() {
32
- false.should.be.true();
33
- });
34
- });
35
- describe('#listen()', function() {
36
- it('should accept requests on the specified URL/port', function(done) {
37
- supertest(this.server.expressApp)
38
- .get(`${this.server.api.path}`)
39
- .expect(200)
40
- .end(done);
41
- });
42
- it('should not accept requests on unspecified URLs/ports', function(done) {
43
- false.should.be.true();
44
- });
45
- });
46
- describe('#close()', function() {
47
- it('should stop accepting requests', function() {
48
- false.should.be.true();
49
- });
50
- });
51
- });
1
+ const { Hook } = require('adapt-authoring-core');
2
+ const Router = require('../lib/Router');
3
+ const ServerModule = require('../lib/ServerModule');
4
+ const should = require('should');
5
+ const supertest = require('supertest');
6
+
7
+ describe('Server module', function() {
8
+ before(function() {
9
+ this.server = new ServerModule(global.ADAPT.app, { name: require('../package.json').name });
10
+ });
11
+ describe('#constructor()', function() {
12
+ it('should expose the Express application', function() {
13
+ false.should.be.true();
14
+ });
15
+ it('should expose a `root` Router', function() {
16
+ this.server.root.should.be.an.instanceof(Router);
17
+ });
18
+ it('should expose an `api` Router', function() {
19
+ this.server.api.should.be.an.instanceof(Router);
20
+ });
21
+ it('should expose a hook to modify requests', function() {
22
+ this.server.requestHook.should.be.an.instanceof(Hook);
23
+ });
24
+ });
25
+ describe('#url()', function() {
26
+ it('should return the URL of the server', function() {
27
+ this.server.url.should.be.a.String();
28
+ });
29
+ });
30
+ describe('#static()', function() {
31
+ it('should expose Express#static', function() {
32
+ false.should.be.true();
33
+ });
34
+ });
35
+ describe('#listen()', function() {
36
+ it('should accept requests on the specified URL/port', function(done) {
37
+ supertest(this.server.expressApp)
38
+ .get(`${this.server.api.path}`)
39
+ .expect(200)
40
+ .end(done);
41
+ });
42
+ it('should not accept requests on unspecified URLs/ports', function(done) {
43
+ false.should.be.true();
44
+ });
45
+ });
46
+ describe('#close()', function() {
47
+ it('should stop accepting requests', function() {
48
+ false.should.be.true();
49
+ });
50
+ });
51
+ });