Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / test / core / simple / test-http-extra-response.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var common = require('../common');
23 var assert = require('assert');
24 var http = require('http');
25 var net = require('net');
26
27 // If an HTTP server is broken and sends data after the end of the response,
28 // node should ignore it and drop the connection.
29 // Demos this bug: https://github.com/ry/node/issues/680
30
31 var body = 'hello world\r\n';
32 var fullResponse =
33     'HTTP/1.1 500 Internal Server Error\r\n' +
34     'Content-Length: ' + body.length + '\r\n' +
35     'Content-Type: text/plain\r\n' +
36     'Date: Fri + 18 Feb 2011 06:22:45 GMT\r\n' +
37     'Host: 10.20.149.2\r\n' +
38     'Access-Control-Allow-Credentials: true\r\n' +
39     'Server: badly broken/0.1 (OS NAME)\r\n' +
40     '\r\n' +
41     body;
42
43 var gotResponse = false;
44
45
46 var server = net.createServer(function (socket) {
47   var postBody = '';
48
49   socket.setEncoding('utf8');
50
51   socket.on('data', function (chunk) {
52     postBody += chunk;
53
54     if (postBody.indexOf('\r\n') > -1) {
55       socket.write(fullResponse);
56       // omg, I wrote the response twice, what a terrible HTTP server I am.
57       socket.end(fullResponse);
58     }
59   });
60 });
61
62
63 server.listen(common.PORT, function () {
64   http.get({ port: common.PROXY_PORT }, function (res) {
65     var buffer = '';
66     console.log('Got res code: ' + res.statusCode);
67
68     res.setEncoding('utf8');
69     res.on('data', function (chunk) {
70       buffer += chunk;
71     });
72
73     res.on('end', function () {
74       console.log('Response ended, read ' + buffer.length + ' bytes');
75       assert.equal(body, buffer);
76       server.close();
77       gotResponse = true;
78     });
79   });
80 });
81
82
83 process.on('exit', function () {
84   assert.ok(gotResponse);
85 });
86