Skip to content

Commit 4b7e8fa

Browse files
committed
Add test for #6
1 parent 2ec184a commit 4b7e8fa

File tree

1 file changed

+114
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)