From e5d420c647b92538713596f2ffb353509d751373 Mon Sep 17 00:00:00 2001 From: Yi Zhao <11612917@mail.sustech.edu.cn> Date: Sat, 25 May 2024 11:23:49 -0700 Subject: [PATCH] fix python.http-client generate code snippet using http --- .../lib/python-httpclient.js | 7 +++++- .../test/unit/converter.test.js | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/codegens/python-http.client/lib/python-httpclient.js b/codegens/python-http.client/lib/python-httpclient.js index f0d80f621..9b8559cc9 100644 --- a/codegens/python-http.client/lib/python-httpclient.js +++ b/codegens/python-http.client/lib/python-httpclient.js @@ -140,7 +140,12 @@ self = module.exports = { snippet += 'from codecs import encode\n'; } snippet += '\n'; - snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + if (request.url.protocol === 'http') { + snippet += `conn = http.client.HTTPConnection("${sanitize(host)}"`; + } + else { + snippet += `conn = http.client.HTTPSConnection("${sanitize(host)}"`; + } snippet += url.port ? `, ${request.url.port}` : ''; snippet += options.requestTimeout !== 0 ? `, timeout = ${options.requestTimeout})\n` : ')\n'; diff --git a/codegens/python-http.client/test/unit/converter.test.js b/codegens/python-http.client/test/unit/converter.test.js index 8d37b9282..d9b686f99 100644 --- a/codegens/python-http.client/test/unit/converter.test.js +++ b/codegens/python-http.client/test/unit/converter.test.js @@ -336,6 +336,29 @@ describe('Python-http.client converter', function () { }); }); + it('should generate valid snippets when url uses http protocol', function () { + var request = new sdk.Request({ + 'method': 'GET', + 'header': [], + 'url': { + 'raw': 'http://localhost:3000', + 'protocol': 'http', + 'host': [ + 'localhost' + ], + 'port': '3000' + }, + 'response': [] + }); + convert(request, {}, function (error, snippet) { + if (error) { + expect.fail(null, null, error); + } + expect(snippet).to.be.a('string'); + expect(snippet).to.include('conn = http.client.HTTPConnection("localhost", 3000)'); + }); + }); + }); describe('parseBody function', function () {