1+ /*
2+ * Licensed under the Apache License, Version 2.0 (the "License");
3+ * you may not use this file except in compliance with the License.
4+ * You may obtain a copy of the License at
5+ *
6+ * http://www.apache.org/licenses/LICENSE-2.0
7+ *
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ */
14+
15+ 'use strict' ;
16+
17+ const createServer = require ( '..' ) ;
18+ const http = require ( 'http' ) ;
19+ const io = require ( 'socket.io-client' ) ;
20+
21+ const chai = require ( 'chai' ) ;
22+ chai . should ( ) ;
23+ chai . use ( require ( 'chai-as-promised' ) ) ;
24+ chai . use ( require ( 'chai-http' ) ) ;
25+ const sinon = require ( 'sinon' ) ;
26+
27+ describe ( '#createServer' , ( ) => {
28+
29+ let sandbox ;
30+
31+ beforeEach ( ( ) => {
32+ sandbox = sinon . sandbox . create ( ) ;
33+ } ) ;
34+
35+ afterEach ( ( ) => {
36+ sandbox . restore ( ) ;
37+ } ) ;
38+
39+ it ( 'should start a server and return the port' , async ( ) => {
40+ const app = await createServer ( 0 , false ) ;
41+ app . get ( 'port' ) . should . be . greaterThan ( 0 ) ;
42+ } ) ;
43+
44+ it ( 'should start a socket.io server' , async ( ) => {
45+ const app = await createServer ( 0 , false ) ;
46+ const port = app . get ( 'port' ) ;
47+ const socket = io ( `http://localhost:${ port } ` , {
48+ autoConnect : false
49+ } ) ;
50+ await new Promise ( ( resolve , reject ) => {
51+ socket . once ( 'connect' , ( ) => {
52+ resolve ( ) ;
53+ } ) ;
54+ socket . open ( ) ;
55+ } ) ;
56+ const result = await new Promise ( ( resolve , reject ) => {
57+ socket . emit ( '/api/ping' , ( error , result ) => {
58+ if ( error ) {
59+ return reject ( error ) ;
60+ }
61+ resolve ( result ) ;
62+ } ) ;
63+ } ) ;
64+ result . version . should . be . a ( 'string' ) ;
65+ await new Promise ( ( resolve , reject ) => {
66+ socket . once ( 'disconnect' , ( ) => {
67+ resolve ( ) ;
68+ } ) ;
69+ socket . close ( ) ;
70+ } ) ;
71+ } ) ;
72+
73+ it ( 'should start a server in test mode' , async ( ) => {
74+ const app = await createServer ( 0 , true ) ;
75+ const result = await chai . request ( app ) . get ( '/api/getSampleList' ) ;
76+ result . body . should . deep . equal ( [ { name : 'basic-sample-network' } ] ) ;
77+ } ) ;
78+
79+ it ( 'should throw an error if listen throws an error' , async ( ) => {
80+ const server = http . createServer ( ) ;
81+ sandbox . stub ( http , 'createServer' ) . returns ( server ) ;
82+ sinon . stub ( server , 'listen' ) . throws ( new Error ( 'such throw error' ) ) ;
83+ await createServer ( 0 , false )
84+ . should . be . rejectedWith ( / s u c h t h r o w e r r o r / ) ;
85+ } ) ;
86+
87+ it ( 'should throw an error if listen calls the callback with an error' , async ( ) => {
88+ const server = http . createServer ( ) ;
89+ sandbox . stub ( http , 'createServer' ) . returns ( server ) ;
90+ sinon . stub ( server , 'listen' ) . yields ( new Error ( 'such callback error' ) ) ;
91+ await createServer ( 0 , false )
92+ . should . be . rejectedWith ( / s u c h c a l l b a c k e r r o r / ) ;
93+ } ) ;
94+
95+ } ) ;
0 commit comments