~ballot/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/guzzlehttp/guzzle/tests/server.js

  • Committer: Benjamin Allot
  • Date: 2020-07-02 16:31:38 UTC
  • Revision ID: benjamin.allot@canonical.com-20200702163138-qyk6njanak5uw2pg
Revert to revno 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
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.
 
5
 *
 
6
 * - Delete all requests that have been received:
 
7
 *      DELETE /guzzle-server/requests
 
8
 *      Host: 127.0.0.1:8125
 
9
 *
 
10
 *  - Enqueue responses
 
11
 *      PUT /guzzle-server/responses
 
12
 *      Host: 127.0.0.1:8125
 
13
 *
 
14
 *      [{ "statusCode": 200, "reasonPhrase": "OK", "headers": {}, "body": "" }]
 
15
 *
 
16
 *  - Get the received requests
 
17
 *      GET /guzzle-server/requests
 
18
 *      Host: 127.0.0.1:8125
 
19
 *
 
20
 *  - Shutdown the server
 
21
 *      DELETE /guzzle-server
 
22
 *      Host: 127.0.0.1:8125
 
23
 *
 
24
 * @package Guzzle PHP <http://www.guzzlephp.org>
 
25
 * @license See the LICENSE file that was distributed with this source code.
 
26
 */
 
27
 
 
28
var http = require("http");
 
29
 
 
30
/**
 
31
 * Guzzle node.js server
 
32
 * @class
 
33
 */
 
34
var GuzzleServer = function(port, log) {
 
35
 
 
36
    this.port = port;
 
37
    this.log = log;
 
38
    this.responses = [];
 
39
    this.requests = [];
 
40
    var that = this;
 
41
 
 
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
 
49
                that.requests = [];
 
50
                res.writeHead(200, "OK", { "Content-Length": 0 });
 
51
                res.end();
 
52
                if (this.log) {
 
53
                    console.log("Flushing requests");
 
54
                }
 
55
            } else if (req.url == "/guzzle-server") {
 
56
                // Shutdown the server
 
57
                res.writeHead(200, "OK", { "Content-Length": 0, "Connection": "close" });
 
58
                res.end();
 
59
                if (this.log) {
 
60
                    console.log("Shutting down");
 
61
                }
 
62
                that.server.close();
 
63
            }
 
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 });
 
69
                res.end(data);
 
70
                if (that.log) {
 
71
                    console.log("Sending receiving requests");
 
72
                }
 
73
            }
 
74
        } else if (req.method == "PUT") {
 
75
            if (req.url == "/guzzle-server/responses") {
 
76
                if (that.log) {
 
77
                    console.log("Adding responses...");
 
78
                }
 
79
                // Received response to queue
 
80
                var data = request.split("\r\n\r\n")[1];
 
81
                if (!data) {
 
82
                    if (that.log) {
 
83
                        console.log("No response data was provided");
 
84
                    }
 
85
                    res.writeHead(400, "NO RESPONSES IN REQUEST", { "Content-Length": 0 });
 
86
                } else {
 
87
                    that.responses = eval("(" + data + ")");
 
88
                    if (that.log) {
 
89
                        console.log(that.responses);
 
90
                    }
 
91
                    res.writeHead(200, "OK", { "Content-Length": 0 });
 
92
                }
 
93
                res.end();
 
94
            }
 
95
        }
 
96
    };
 
97
 
 
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) {
 
102
            res.writeHead(500);
 
103
            res.end("No responses in queue");
 
104
        } else {
 
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);
 
109
        }
 
110
    };
 
111
 
 
112
    this.start = function() {
 
113
 
 
114
        that.server = http.createServer(function(req, res) {
 
115
 
 
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";
 
119
            }
 
120
            request += "\r\n";
 
121
 
 
122
            // Receive each chunk of the request body
 
123
            req.addListener("data", function(chunk) {
 
124
                request += chunk;
 
125
            });
 
126
 
 
127
            // Called when the request completes
 
128
            req.addListener("end", function() {
 
129
                receivedRequest(request, req, res);
 
130
            });
 
131
        });
 
132
        that.server.listen(port, "127.0.0.1");
 
133
 
 
134
        if (this.log) {
 
135
            console.log("Server running at http://127.0.0.1:8125/");
 
136
        }
 
137
    };
 
138
};
 
139
 
 
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;
 
143
 
 
144
// Start the server
 
145
server = new GuzzleServer(port, log);
 
146
server.start();