2
* Guzzle node.js test server to return queued responses to HTTP requests and
3
* expose a RESTful API for enqueueing responses and retrieving the requests
4
* that have been received.
6
* - Delete all requests that have been received:
7
* DELETE /guzzle-server/requests
11
* PUT /guzzle-server/responses
12
* Host: 127.0.0.1:8125
14
* [{ "statusCode": 200, "reasonPhrase": "OK", "headers": {}, "body": "" }]
16
* - Get the received requests
17
* GET /guzzle-server/requests
18
* Host: 127.0.0.1:8125
20
* - Shutdown the server
21
* DELETE /guzzle-server
22
* Host: 127.0.0.1:8125
24
* @package Guzzle PHP <http://www.guzzlephp.org>
25
* @license See the LICENSE file that was distributed with this source code.
28
var http = require("http");
31
* Guzzle node.js server
34
var GuzzleServer = function(port, log) {
42
var controlRequest = function(request, req, res) {
43
if (req.url == '/guzzle-server/perf') {
44
res.writeHead(200, "OK", {"Content-Length": 16});
45
res.end("Body of response");
46
} else if (req.method == "DELETE") {
47
if (req.url == "/guzzle-server/requests") {
48
// Clear the received requests
50
res.writeHead(200, "OK", { "Content-Length": 0 });
53
console.log("Flushing requests");
55
} else if (req.url == "/guzzle-server") {
56
// Shutdown the server
57
res.writeHead(200, "OK", { "Content-Length": 0, "Connection": "close" });
60
console.log("Shutting down");
64
} else if (req.method == "GET") {
65
if (req.url === "/guzzle-server/requests") {
66
// Get received requests
67
var data = that.requests.join("\n----[request]\n");
68
res.writeHead(200, "OK", { "Content-Length": data.length });
71
console.log("Sending receiving requests");
74
} else if (req.method == "PUT") {
75
if (req.url == "/guzzle-server/responses") {
77
console.log("Adding responses...");
79
// Received response to queue
80
var data = request.split("\r\n\r\n")[1];
83
console.log("No response data was provided");
85
res.writeHead(400, "NO RESPONSES IN REQUEST", { "Content-Length": 0 });
87
that.responses = eval("(" + data + ")");
89
console.log(that.responses);
91
res.writeHead(200, "OK", { "Content-Length": 0 });
98
var receivedRequest = function(request, req, res) {
99
if (req.url.indexOf("/guzzle-server") === 0) {
100
controlRequest(request, req, res);
101
} else if (req.url.indexOf("/guzzle-server") == -1 && !that.responses.length) {
103
res.end("No responses in queue");
105
var response = that.responses.shift();
106
res.writeHead(response.statusCode, response.reasonPhrase, response.headers);
107
res.end(new Buffer(response.body, 'base64'));
108
that.requests.push(request);
112
this.start = function() {
114
that.server = http.createServer(function(req, res) {
116
var request = req.method + " " + req.url + " HTTP/" + req.httpVersion + "\r\n";
117
for (var i in req.headers) {
118
request += i + ": " + req.headers[i] + "\r\n";
122
// Receive each chunk of the request body
123
req.addListener("data", function(chunk) {
127
// Called when the request completes
128
req.addListener("end", function() {
129
receivedRequest(request, req, res);
132
that.server.listen(port, "127.0.0.1");
135
console.log("Server running at http://127.0.0.1:8125/");
140
// Get the port from the arguments
141
port = process.argv.length >= 3 ? process.argv[2] : 8125;
142
log = process.argv.length >= 4 ? process.argv[3] : false;
145
server = new GuzzleServer(port, log);