mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-25 10:33:18 +01:00
[Common] Adding keySearch method to Collection [Http] Moving POST curl option logic from EntityEnclosingRequest to the curl factory method Directly using the HTTP request object when creating curl handles in the factory method No longer adding a read callback when sending POSTs through CURLOPT_POSTFIELDS In an effort to mitigate random segfaults and bus errors, using a queue for removing curl and close handles from a multi object so that they are only removed when the multi handle has finished sending all requests. Calling reset() each time a curl multi handle has finished all requests. EntityEnclosingRequest::getPostFields() now returns a Collection object Simplifying the EntityEnclosingRequest::getPostFiles method Adding an array cache to header objects Moving the header comparison DSL from the Tests namespace to Guzzle\Http\Message\HeaderComparison, and adding tests. Adding message, cookie, and url parser interfaces and default implementations. Using a parser registry to manage globally registered parsers. Removing parsing from Response and RequestFactory. Renaming protocol_version to version Changing the Guzzle\Http\Message\Response::setProtocol() method to accept a protocol and version in separate args. Simplifying the chunked encoding handling in RequestFactory Moving cookie parsing out of the cookie plugin and into Guzzle\Http\Parser\Cookie\CookieParser Removing regexps from the cookie parser and simply using stripos Moving the parseQuery method out of Url and on to QueryString::fromString() as a static factory method. Adding more logging to node.js test webserver Adding pecl_http message parser. Installing pecl when travis boots up
165 lines
5.4 KiB
JavaScript
165 lines
5.4 KiB
JavaScript
/**
|
|
* Guzzle node.js test server to return queued responses to HTTP requests and
|
|
* expose a RESTful API for enqueueing responses and retrieving the requests
|
|
* that have been received.
|
|
*
|
|
* - Delete all requests that have been received:
|
|
* DELETE /guzzle-server/requests
|
|
* Host: 127.0.0.1:8124
|
|
*
|
|
* - Enqueue responses
|
|
* PUT /guzzle-server/responses
|
|
* Host: 127.0.0.1:8124
|
|
*
|
|
* [{ "statusCode": 200, "reasonPhrase": "OK", "headers": {}, "body": "" }]
|
|
*
|
|
* - Get the recevied requests
|
|
* GET /guzzle-server/requests
|
|
* Host: 127.0.0.1:8124
|
|
*
|
|
* - Shutdown the server
|
|
* DELETE /guzzle-server
|
|
* Host: 127.0.0.1:8124
|
|
*
|
|
* @package Guzzle PHP <http://www.guzzlephp.org>
|
|
* @license See the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
var http = require("http");
|
|
|
|
/**
|
|
* Guzzle node.js server
|
|
* @class
|
|
*/
|
|
var GuzzleServer = function(port) {
|
|
|
|
this.port = port;
|
|
this.log = log;
|
|
this.responses = [];
|
|
this.requests = [];
|
|
var that = this;
|
|
|
|
/**
|
|
* Handle a Guzzle Server control request
|
|
* @param (String) request HTTP request as a string
|
|
* @param (ServerRequest) req Received server request
|
|
* @param (ServerResponse) res Outgoing server response
|
|
*/
|
|
var controlRequest = function(request, req, res) {
|
|
if (req.method == "DELETE") {
|
|
if (req.url == "/guzzle-server/requests") {
|
|
// Clear the received requests
|
|
that.requests = [];
|
|
res.writeHead(200, "OK", { "Content-Length": 0 });
|
|
res.end();
|
|
if (this.log) {
|
|
console.log("Flushing requests");
|
|
}
|
|
} else if (req.url == "/guzzle-server") {
|
|
// Shutdown the server
|
|
res.writeHead(200, "OK", { "Content-Length": 0, "Connection": "close" });
|
|
res.end();
|
|
if (this.log) {
|
|
console.log("Shutting down");
|
|
}
|
|
that.server.close();
|
|
}
|
|
} else if (req.method == "GET") {
|
|
if (req.url === "/guzzle-server/requests") {
|
|
// Get received requests
|
|
var data = that.requests.join("\n----[request]\n");
|
|
res.writeHead(200, "OK", { "Content-Length": data.length });
|
|
res.end(data);
|
|
if (that.log) {
|
|
console.log("Sending receiving requests");
|
|
}
|
|
}
|
|
} else if (req.method == "PUT") {
|
|
if (req.url == "/guzzle-server/responses") {
|
|
if (that.log) {
|
|
console.log("Adding responses...");
|
|
}
|
|
// Received respones to queue
|
|
var data = request.split("\r\n\r\n")[1];
|
|
if (!data) {
|
|
if (that.log) {
|
|
console.log("No response data was provided");
|
|
}
|
|
res.writeHead(500, "NO RESPONSES IN REQUEST", { "Content-Length": 0 });
|
|
} else {
|
|
that.responses = eval("(" + data + ")");
|
|
if (that.log) {
|
|
console.log(that.responses);
|
|
}
|
|
res.writeHead(200, "OK", { "Content-Length": 0 });
|
|
}
|
|
res.end();
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Received a complete request
|
|
* @param (String) request HTTP request as a string
|
|
* @param (ServerRequest) req Received server request
|
|
* @param (ServerResponse) res Outgoing server response
|
|
*/
|
|
var receivedRequest = function(request, req, res) {
|
|
if (req.url.indexOf("/guzzle-server") === 0) {
|
|
controlRequest(request, req, res);
|
|
} else {
|
|
var response = that.responses.shift();
|
|
res.writeHead(response.statusCode, response.reasonPhrase, response.headers);
|
|
res.end(response.body);
|
|
that.requests.push(request);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Start the node.js Guzzle server
|
|
*/
|
|
this.start = function() {
|
|
|
|
that.server = http.createServer(function(req, res) {
|
|
|
|
// If this is not a control request and no responses are in queue, return 500 response
|
|
if (req.url.indexOf("/guzzle-server") == -1 && !that.responses.length) {
|
|
res.writeHead(500);
|
|
res.end("No responses in queue");
|
|
return;
|
|
}
|
|
|
|
// Begin building the request message as a string
|
|
var request = req.method + " " + req.url + " HTTP/" + req.httpVersion + "\r\n";
|
|
// Add the request headers
|
|
for (var i in req.headers) {
|
|
request += i + ": " + req.headers[i] + "\r\n";
|
|
}
|
|
request += "\r\n";
|
|
|
|
// Receive each chunk of the request body
|
|
req.addListener("data", function(chunk) {
|
|
request += chunk;
|
|
});
|
|
|
|
// Called when the request completes
|
|
req.addListener("end", function() {
|
|
receivedRequest(request, req, res);
|
|
});
|
|
});
|
|
that.server.listen(port, "127.0.0.1");
|
|
|
|
if (this.log) {
|
|
console.log("Server running at http://127.0.0.1:8124/");
|
|
}
|
|
};
|
|
};
|
|
|
|
// Get the port from the arguments
|
|
port = process.argv.length >= 3 ? process.argv[2] : 8124;
|
|
log = process.argv.length >= 4 ? process.argv[3] : false;
|
|
|
|
// Start the server
|
|
server = new GuzzleServer(port, log);
|
|
server.start();
|