From 65b24ba6ac83246db7a88b1d8b79019f220ba94c Mon Sep 17 00:00:00 2001 From: Akhil Kedia Date: Tue, 11 Jul 2017 16:00:26 +0900 Subject: [PATCH] [Bugfix] Correct Handling of http expect: 100-continue Added a Unit test case for this too at ./test/run_pass/test_https_expect.js. This case fails before this commit, passes after. Change-Id: I3f5cc94c31cd890fa028bc9737b6534e2f19f2f4 Signed-off-by: Akhil Kedia --- src/js/https_incoming.js | 16 ++++- test/run_pass/test_https_post_status_codes.js | 60 +++++++++++++++++++ test/testsets.json | 7 ++- 3 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 test/run_pass/test_https_post_status_codes.js diff --git a/src/js/https_incoming.js b/src/js/https_incoming.js index 23b4f90..7fb8143 100644 --- a/src/js/https_incoming.js +++ b/src/js/https_incoming.js @@ -113,6 +113,9 @@ function parserOnHeadersComplete(info) { parser.incoming.started = true; // For client side, if response to 'HEAD' request, we will skip parsing body + if (parser.incoming.statusCode == 100) { + return false; + } return parser.incoming.clientRequest.headersComplete(); } @@ -137,9 +140,16 @@ function parserOnMessageComplete() { var incoming = parser.incoming; if (incoming) { - incoming.completed = true; - // no more data from incoming, stream will emit 'end' event - incoming.push(null); + if (incoming.statusCode == 100) { + incoming.headers = {}; + incoming.statusCode = null; + incoming.statusMessage = null; + incoming.started = false; + } else { + incoming.completed = true; + // no more data from incoming, stream will emit 'end' event + incoming.push(null); + } } } diff --git a/test/run_pass/test_https_post_status_codes.js b/test/run_pass/test_https_post_status_codes.js new file mode 100644 index 0000000..26f4ff7 --- /dev/null +++ b/test/run_pass/test_https_post_status_codes.js @@ -0,0 +1,60 @@ +/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var assert = require('assert'); +var https = require('https'); + +var isRequest1Finished = false; +// 1. POST req +var data = JSON.stringify({ data: { temp: 50, onFire: false }, + sdid: '170e5221612b4bc38dce53fd4395174a', + type: 'message' }); + +var options = { + "method": "POST", + "hostname": "api.artik.cloud", + "path": "/v1.1/messages", + "headers": { + "content-type": "application/json", + "content-length": data.length, + "authorization": "Bearer 1718113118564ad495ad03f04116f379" + } +}; + +var getResponseHandler = function (res) { + var res_body = ''; + + assert.equal(200, res.statusCode); + + var endHandler = function(){ + var response = JSON.parse(res_body); + assert.assert(response['data'], 'Recieved incorrect response from server'); + isRequest1Finished = true; + }; + res.on('end', endHandler); + + res.on('data', function(chunk){ + res_body += chunk.toString(); + }); +}; + +var req = https.request(options, getResponseHandler); +req.write(data); +req.end(); + +process.on('exit', function() { + assert.equal(isRequest1Finished, true); +}); diff --git a/test/testsets.json b/test/testsets.json index 2de56ea..86b221f 100644 --- a/test/testsets.json +++ b/test/testsets.json @@ -41,9 +41,10 @@ { "name": "test_fs_open_read_sync_3.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" }, { "name": "test_gpio_input.js", "skip": ["all"], "reason": "needs hardware" }, { "name": "test_gpio_output.js", "skip": ["all"], "reason": "need user input"}, - { "name": "test_https_get.js", "timeout": 40, "skip": ["all"], "reason": "Only tizen support https, and on tizen we test manually" }, - { "name": "test_https_request_response.js", "timeout": 40, "skip": ["all"], "reason": "Only tizen support https, and on tizen we test manually" }, - { "name": "test_https_timeout.js", "timeout": 40, "skip": ["all"], "reason": "Only tizen support https, and on tizen we test manually" }, + { "name": "test_https_expect.js", "timeout": 40, "skip": ["all"], "reason": "Implemented only for Tizen" }, + { "name": "test_https_get.js", "timeout": 40, "skip": ["all"], "reason": "Implemented only for Tizen" }, + { "name": "test_https_request_response.js", "timeout": 40, "skip": ["all"], "reason": "Implemented only for Tizen" }, + { "name": "test_https_timeout.js", "timeout": 40, "skip": ["all"], "reason": "Implemented only for Tizen" }, { "name": "test_i2c.js", "skip": ["all"], "reason": "need to setup test environment" }, { "name": "test_iotjs_promise.js", "skip": ["all"], "reason": "es2015 is off by default" }, { "name": "test_module_cache.js", "skip": ["nuttx"], "reason": "not implemented for nuttx" }, -- 2.34.1