@@ -4,6 +4,7 @@ import httpMocks from 'node-mocks-http';
4
4
import { promises as fs } from 'fs' ;
5
5
import proc from 'child_process' ;
6
6
import sinon from 'sinon' ;
7
+ import ee from 'events' ;
7
8
import compile from '../../src/routes/compile' ;
8
9
9
10
chai . should ( ) ;
@@ -14,10 +15,11 @@ return
14
15
` ;
15
16
const ID = '3ab771baa15b807fc028f9766eb111b0' ;
16
17
17
- describe ( 'Compile route ' , ( ) => {
18
+ describe ( 'Compile endpoint ' , ( ) => {
18
19
let exec ;
19
20
let fswriteFile ;
20
21
let fsreadFile ;
22
+
21
23
beforeEach ( ( ) => {
22
24
fswriteFile = sinon . stub ( fs , 'writeFile' ) ;
23
25
fsreadFile = sinon . stub ( fs , 'readFile' ) . returns ( Buffer . from ( '<EXE DATA>' ) ) ;
@@ -28,31 +30,53 @@ describe('Compile route', () => {
28
30
sinon . restore ( ) ;
29
31
} ) ;
30
32
33
+ it ( 'should reject a empty request' , ( done ) => {
34
+ const res = httpMocks . createResponse ( {
35
+ eventEmitter : ee . EventEmitter ,
36
+ } ) ;
37
+ const req = httpMocks . createRequest ( {
38
+ method : 'POST' ,
39
+ url : '/' ,
40
+ body : '' ,
41
+ } ) ;
42
+ // TODO: undo bodge with body producing {}
43
+ req . body = '' ;
44
+
45
+ res . once ( 'end' , ( ) => {
46
+ res . statusCode . should . equal ( 400 ) ;
47
+
48
+ done ( ) ;
49
+ } ) ;
50
+
51
+ compile . router . handle ( req , res ) ;
52
+ } ) ;
53
+
31
54
it ( 'should convert a valid request' , ( done ) => {
32
- const response = httpMocks . createResponse ( ) ;
33
- const request = httpMocks . createRequest ( {
55
+ const res = httpMocks . createResponse ( {
56
+ eventEmitter : ee . EventEmitter ,
57
+ } ) ;
58
+ const req = httpMocks . createRequest ( {
34
59
method : 'POST' ,
35
60
url : '/' ,
36
61
body : SCRIPT ,
37
62
} ) ;
38
63
39
- // TODO: use for negative test case
40
- // response.on('end', () => {
41
- // response._getData().should.equal('world');
42
- // done();
43
- // });
64
+ res . sendFileBuffer = ( name , data ) => {
65
+ name . should . be . equal ( `${ ID } .exe` ) ;
66
+ data . should . be . instanceof ( Buffer ) ;
67
+
68
+ // boilerplate to behave as real express middleware
69
+ res . status ( 200 ) . send ( ) ;
70
+ } ;
44
71
45
- response . sendFileBuffer = ( name , data ) => {
72
+ res . once ( 'end' , ( ) => {
46
73
sinon . assert . calledWith ( fswriteFile , `/tmp/${ ID } .ahk` , SCRIPT ) ;
47
74
sinon . assert . calledWith ( exec , `xvfb-run -a wine Ahk2Exe.exe /in /tmp/${ ID } .ahk` ) ;
48
75
sinon . assert . calledWith ( fsreadFile , `/tmp/${ ID } .exe` ) ;
49
76
50
- name . should . be . equal ( `${ ID } .exe` ) ;
51
- data . should . be . instanceof ( Buffer ) ;
52
-
53
77
done ( ) ;
54
- } ;
78
+ } ) ;
55
79
56
- compile . router . handle ( request , response ) ;
80
+ compile . router . handle ( req , res ) ;
57
81
} ) ;
58
82
} ) ;
0 commit comments