Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_http_header.js
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17
18 var assert = require('assert');
19 var http = require('http');
20 var checkReqFinish = false;
21
22 // server side code
23 // Server just ends after sending with some headers.
24
25 var server = http.createServer(function (req, res) {
26
27   req.on('data', function (chunk) {
28     body += chunk;
29   });
30
31   var endHandler = function () {
32
33     res.setHeader('h1','h1');
34     res.setHeader('h2','h2');
35     res.setHeader('h3','h3');
36     res.removeHeader('h2');
37     if (res.getHeader('h3') == 'h3') {
38       res.setHeader('h3','h3prime'); // h3 value should be overwrited
39     }
40     // final res.headers = { 'h1' : 'h1', 'h3': 'h3prime' }
41
42     res.end(function(){
43         server.close();
44     });
45   };
46
47   req.on('end', endHandler);
48
49 });
50 server.listen(3045, 3);
51
52
53 // client req code
54
55 var options = {
56   method : 'GET',
57   port : 3045
58 };
59
60
61 var postResponseHandler = function (res) {
62   var res_body = '';
63
64   assert.equal(200, res.statusCode);
65   assert.equal(res.headers['h1'], 'h1');
66   assert.equal(res.headers['h2'], undefined);
67   assert.equal(res.headers['h3'], 'h3prime');
68
69   var endHandler = function(){
70     checkReqFinish = true;
71   };
72   res.on('end', endHandler);
73
74   res.on('data', function(chunk){
75     res_body += chunk.toString();
76   });
77 };
78
79 var req = http.request(options, postResponseHandler);
80 req.end();
81
82 process.on('exit', function() {
83   assert.equal(checkReqFinish, true);
84 });