1
+ // LICENSE : MIT
2
+ "use strict" ;
3
+ import assert from "power-assert" ;
4
+ import connect from "connect" ;
5
+ import errorHandler from "../../src/connect/errorHandler" ;
6
+ import nosniff from "../../src/connect/nosniff" ;
7
+ import hello from "../../src/connect/hello" ;
8
+ import echo from "../../src/connect/echo" ;
9
+ import http from "http" ;
10
+ import fetch from "node-fetch" ;
11
+ describe ( "connect" , function ( ) {
12
+ var responseText = "test" ;
13
+ var server ;
14
+ describe ( "errorHandler" , function ( ) {
15
+ beforeEach ( function ( done ) {
16
+ var app = connect ( ) ;
17
+ app . use ( errorHandler ( ) ) ;
18
+ app . use ( ( req , res , next ) => {
19
+ next ( new Error ( "wrong" ) ) ;
20
+ } ) ;
21
+ server = http . createServer ( app ) . listen ( 3000 , done ) ;
22
+ } ) ;
23
+ afterEach ( function ( ) {
24
+ server && server . close ( ) ;
25
+ } ) ;
26
+ it ( "should return 500 status response" , function ( ) {
27
+ return fetch ( "http://localhost:3000" )
28
+ . then ( res => res . status )
29
+ . then ( status => {
30
+ assert ( status , 500 ) ;
31
+ } ) ;
32
+ } ) ;
33
+
34
+ } ) ;
35
+ describe ( "hello" , function ( ) {
36
+ beforeEach ( function ( done ) {
37
+ var app = connect ( ) ;
38
+ app . use ( errorHandler ( ) ) ;
39
+ app . use ( hello ( responseText ) ) ;
40
+ server = http . createServer ( app ) . listen ( 3000 , done ) ;
41
+ } ) ;
42
+ afterEach ( function ( ) {
43
+ server && server . close ( ) ;
44
+ } ) ;
45
+ it ( "should return response text" , function ( ) {
46
+ return fetch ( "http://localhost:3000" )
47
+ . then ( res => res . text ( ) )
48
+ . then ( text => {
49
+ assert . equal ( text , responseText ) ;
50
+ } ) ;
51
+ } ) ;
52
+ } ) ;
53
+ describe ( "sniff" , function ( ) {
54
+ beforeEach ( function ( done ) {
55
+ var app = connect ( ) ;
56
+ app . use ( nosniff ( ) ) ;
57
+ app . use ( hello ( responseText ) ) ;
58
+ server = http . createServer ( app ) . listen ( 3000 , done ) ;
59
+ } ) ;
60
+ afterEach ( function ( ) {
61
+ server && server . close ( ) ;
62
+ } ) ;
63
+ it ( "should return response has `X-Content-Type-Options` header" , function ( ) {
64
+ return fetch ( "http://localhost:3000" )
65
+ . then ( res => {
66
+ assert . equal ( res . headers . get ( "x-content-type-options" ) , "nosniff" ) ;
67
+ } ) ;
68
+ } ) ;
69
+ } ) ;
70
+ describe ( "echo" , function ( ) {
71
+ beforeEach ( function ( done ) {
72
+ var app = connect ( ) ;
73
+ app . use ( echo ( ) ) ;
74
+ server = http . createServer ( app ) . listen ( 3000 , done ) ;
75
+ } ) ;
76
+ afterEach ( function ( ) {
77
+ server && server . close ( ) ;
78
+ } ) ;
79
+ it ( "should return request as response" , function ( ) {
80
+ var requestBody = {
81
+ key : "value"
82
+ } ;
83
+ return fetch ( "http://localhost:3000" , {
84
+ method : "POST" ,
85
+ headers : {
86
+ "Accept" : "application/json" ,
87
+ "Content-Type" : "application/json"
88
+ } ,
89
+ body : JSON . stringify ( requestBody )
90
+ } ) . then ( res => {
91
+ return res . json ( ) ;
92
+ } ) . then ( json => {
93
+ assert . deepEqual ( json , requestBody ) ;
94
+ } ) ;
95
+ } ) ;
96
+ } ) ;
97
+ } ) ;
0 commit comments