File tree Expand file tree Collapse file tree 4 files changed +77
-0
lines changed
samples/node/web/test/koa-test Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ const Koa = require ( 'koa' ) ;
2
+
3
+ const app = new Koa ( ) ;
4
+
5
+ app . use ( async ( ctx , next ) => {
6
+ const start = new Date ( ) . getTime ( ) ;
7
+ await next ( ) ;
8
+ const ms = new Date ( ) . getTime ( ) - start ;
9
+ console . log ( `${ ctx . request . method } ${ ctx . request . url } : ${ ms } ms` ) ;
10
+ ctx . response . set ( 'X-Response-Time' , `${ ms } ms` ) ;
11
+ } ) ;
12
+
13
+ app . use ( async ( ctx , next ) => {
14
+ var name = ctx . request . query . name || 'world' ;
15
+ ctx . response . type = 'text/html' ;
16
+ ctx . response . body = `<h1>Hello, ${ name } !</h1>` ;
17
+ } ) ;
18
+
19
+ module . exports = app ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " koa-test" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " Test Koa 2 app" ,
5
+ "main" : " start.js" ,
6
+ "scripts" : {
7
+ "start" : " node --use_strict start.js" ,
8
+ "test" : " mocha"
9
+ },
10
+ "keywords" : [
11
+ " koa" ,
12
+ " async" ,
13
+ " mocha" ,
14
+ " test"
15
+ ],
16
+ "author" : " Michael Liao" ,
17
+ "license" : " Apache-2.0" ,
18
+ "repository" : {
19
+ "type" : " git" ,
20
+ "url" : " https://github.com/michaelliao/learn-javascript.git"
21
+ },
22
+ "dependencies" : {
23
+ "koa" : " 2.0.0"
24
+ },
25
+ "devDependencies" : {
26
+ "mocha" : " 3.0.2" ,
27
+ "supertest" : " 3.0.0"
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ const app = require ( './app' ) ;
2
+
3
+ app . listen ( 3000 ) ;
4
+ console . log ( 'app started at port 3000...' ) ;
Original file line number Diff line number Diff line change
1
+ const
2
+ request = require ( 'supertest' ) ,
3
+ app = require ( '../app' ) ;
4
+
5
+ describe ( '#test koa app' , ( ) => {
6
+
7
+ let server = app . listen ( 9900 ) ;
8
+
9
+ describe ( '#test server' , ( ) => {
10
+
11
+ it ( '#test GET /' , async ( ) => {
12
+ let res = await request ( server )
13
+ . get ( '/' )
14
+ . expect ( 'Content-Type' , / t e x t \/ h t m l / )
15
+ . expect ( 200 , '<h1>Hello, world!</h1>' ) ;
16
+ } ) ;
17
+
18
+ it ( '#test GET /path?name=Bob' , async ( ) => {
19
+ let res = await request ( server )
20
+ . get ( '/path?name=Bob' )
21
+ . expect ( 'Content-Type' , / t e x t \/ h t m l / )
22
+ . expect ( 200 , '<h1>Hello, Bob!</h1>' ) ;
23
+ } ) ;
24
+ } ) ;
25
+ } ) ;
You can’t perform that action at this time.
0 commit comments