Skip to content

Commit 58e7a07

Browse files
committed
Add test for #8
1 parent d455ce7 commit 58e7a07

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright (c) 2016 Fabio Massaioli, Robert Groh and other contributors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the 'Software'), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
'use strict';
23+
/*global describe, it, before, after */
24+
/*jshint node: true */
25+
/*jshint expr: true*/
26+
27+
var expect = require('chai').expect,
28+
request = require('request'),
29+
path = require('path');
30+
31+
var fcgiFramework = require('../../../index.js'); //this we want to test
32+
33+
function randomInt(low, high) {
34+
return Math.floor(Math.random() * (high - low + 1) + low);
35+
}
36+
37+
38+
describe('multiwrite Server (no content-length)', function setup() {
39+
var port = 0, //will choose a random (and hopefully free) port
40+
socketPath = path.join(__dirname, 'multiwriteServer_Socket' + randomInt(1000, 2000));
41+
42+
before(function startFastCgiApplication(done) {
43+
function answerWithError(res, err) {
44+
res.writeHead(500, {
45+
'Content-Type': 'text/plain; charset=utf-8',
46+
'Content-Length': err.stack.length + 1
47+
});
48+
res.end(err.stack + '\n');
49+
}
50+
51+
fcgiFramework.createServer(function multiwrite(req, res) {
52+
req.resume();
53+
req.on('end', function () {
54+
try {
55+
res.writeHead(200, {
56+
'Content-Type': 'text/plain; charset=utf-8'
57+
});
58+
59+
res.write("a");
60+
res.write("b");
61+
res.end("c");
62+
} catch (err) {
63+
answerWithError(res, err);
64+
}
65+
});
66+
67+
req.on('error', answerWithError.bind(undefined, res));
68+
}).listen(socketPath, function cgiStarted(err) {
69+
if (err) {
70+
done(err);
71+
} else {
72+
console.log('cgi app listen on socket:' + socketPath);
73+
74+
var http = require('http');
75+
var fcgiHandler = require('fcgi-handler');
76+
77+
var server = http.createServer(function (req, res) {
78+
fcgiHandler.connect({
79+
path: socketPath
80+
}, function (err, fcgiProcess) {
81+
if (err) {
82+
answerWithError(res, err);
83+
} else {
84+
//route all request to fcgi application
85+
fcgiProcess.handle(req, res, { /*empty Options*/ });
86+
}
87+
});
88+
});
89+
server.listen(port, function httpServerStarted(err) {
90+
port = server.address().port;
91+
done(err);
92+
});
93+
}
94+
});
95+
});
96+
97+
it('should answer with the expected response', function checkResponse(done) {
98+
request({
99+
uri: 'http://localhost:' + port,
100+
method: 'GET'
101+
}, function (err, res, body) {
102+
expect(res.statusCode).to.be.equal(200);
103+
expect(body).to.be.equal("abc");
104+
done(err);
105+
});
106+
});
107+
108+
after(function removeSocketPath(done) {
109+
require('fs').unlink(socketPath, done);
110+
});
111+
});

0 commit comments

Comments
 (0)