diff --git a/demo/index.html b/demo/index.html index 426a3c9..f2939ec 100644 --- a/demo/index.html +++ b/demo/index.html @@ -63,6 +63,21 @@

Payload only

+ +

Port number request

+ + + diff --git a/demo/java.html b/demo/java.html index 593ebc6..4c8f487 100644 --- a/demo/java.html +++ b/demo/java.html @@ -68,6 +68,22 @@

Payload only

+ +

Port number request

+ + + diff --git a/demo/javascript.html b/demo/javascript.html index 3b3cd94..1a0e2a4 100644 --- a/demo/javascript.html +++ b/demo/javascript.html @@ -69,6 +69,22 @@

Payload only

+ +

Port number request

+ + + diff --git a/demo/node.html b/demo/node.html index 457c988..7960a04 100644 --- a/demo/node.html +++ b/demo/node.html @@ -69,6 +69,22 @@

Payload only

+ +

Port number request

+ + + diff --git a/demo/python.html b/demo/python.html index 995884c..c7b889c 100644 --- a/demo/python.html +++ b/demo/python.html @@ -69,6 +69,22 @@

Payload only

+ +

Port number request

+ + + diff --git a/src/HttpCodeSnippetsElement.js b/src/HttpCodeSnippetsElement.js index 72b5f8b..65e2892 100644 --- a/src/HttpCodeSnippetsElement.js +++ b/src/HttpCodeSnippetsElement.js @@ -108,7 +108,7 @@ export class HttpCodeSnippetsElement extends LitElement { */ headersToList(headersValue) { let headers = headersValue || this.headers; - if (!headers || !headers.trim() || typeof headers !== 'string') { + if (typeof headers !== 'string' || !headers || !headers.trim()) { return []; } const result = []; diff --git a/src/Python/Python27HttpSnippetElement.js b/src/Python/Python27HttpSnippetElement.js index 52ac829..ce01a84 100644 --- a/src/Python/Python27HttpSnippetElement.js +++ b/src/Python/Python27HttpSnippetElement.js @@ -40,7 +40,7 @@ export class Python27HttpSnippetElement extends BaseCodeSnippet { return ''; } let result = 'import httplib\n\n'; - const hasHeaders = !!(headers && headers instanceof Array && headers.length); + const hasHeaders = !!(Array.isArray(headers) && headers.length); const hasPayload = !!payload; if (hasHeaders) { @@ -99,8 +99,12 @@ export class Python27HttpSnippetElement extends BaseCodeSnippet { if (data.port === '443') { clazz += 'S'; } - let result = `conn = httplib.${clazz}Connection('${data.hostValue}')\n`; - result += `conn.request('${method}','${data.path}'`; + let result = `conn = httplib.${clazz}Connection('${data.hostValue}'`; + if (data.port && !['80', '443'].includes(data.port)) { + result += `, ${data.port}`; + } + result += ')\n'; + result += `conn.request('${method}', '${data.path}'`; if (hasPayload) { result += ', body'; } diff --git a/src/Python/Python31HttpSnippetElement.js b/src/Python/Python31HttpSnippetElement.js index e9d38fb..81b64e2 100644 --- a/src/Python/Python31HttpSnippetElement.js +++ b/src/Python/Python31HttpSnippetElement.js @@ -99,8 +99,12 @@ export class Python31HttpSnippetElement extends BaseCodeSnippet { if (data.port === '443') { clazz += 'S'; } - let result = `conn = http.client.${clazz}Connection('${data.hostValue}')\n`; - result += `conn.request('${method}','${data.path}'`; + let result = `conn = http.client.${clazz}Connection('${data.hostValue}'`; + if (data.port && !['80', '443'].includes(data.port)) { + result += `, ${data.port}`; + } + result += ')\n'; + result += `conn.request('${method}', '${data.path}'`; if (hasPayload) { result += ', body'; } diff --git a/test/python-27-http-snippet.test.js b/test/python-27-http-snippet.test.js index ce829cd..1333978 100644 --- a/test/python-27-http-snippet.test.js +++ b/test/python-27-http-snippet.test.js @@ -20,6 +20,13 @@ describe('', () => { url="http://domain.com">`)); } + /** + * @returns {Promise} + */ + async function portNumberFixture() { + return (fixture(html``)); + } + describe('python-27-http-snippet', () => { it('Renders code block (full)', async () => { const element = await basicFixture(); @@ -38,7 +45,7 @@ describe('', () => { 'body = """test"""', '', 'conn = httplib.HTTPConnection(\'domain.com\')', - 'conn.request(\'POST\',\'/\', body, headers)', + 'conn.request(\'POST\', \'/\', body, headers)', 'res = conn.getresponse()', '', 'print(res.status, res.reason)', @@ -66,6 +73,27 @@ describe('', () => { const code = element._code.innerText; assert.equal(code.indexOf('req.write(body)'), -1); }); + + it('adds the port number', async () => { + const element = await portNumberFixture(); + const lines = [ + 'import httplib', + '', + 'conn = httplib.HTTPConnection(\'127.0.0.1\', 4242)', + 'conn.request(\'GET\', \'/oauth/token?grant_type=password&username=maex&password=abc123_ABC123\')', + 'res = conn.getresponse()', + '', + 'print(res.status, res.reason)', + 'print(res.read())', + 'print(res.getheaders())' + ]; + await oneEvent(element, 'highlighted'); + const code = element._code.innerText; + const result = code.split('\n'); + for (let i = 0; i < result.length; i++) { + assert.equal(result[i], lines[i], `Line ${i} matches`); + } + }); }); describe('_getHeaders()', () => { diff --git a/test/python-31-http-snippet.test.js b/test/python-31-http-snippet.test.js index 554bf7f..7908e94 100644 --- a/test/python-31-http-snippet.test.js +++ b/test/python-31-http-snippet.test.js @@ -19,6 +19,13 @@ describe('', () => { url="http://domain.com">`)); } + /** + * @returns {Promise} + */ + async function portNumberFixture() { + return (fixture(html``)); + } + describe('python-31-http-snippet', () => { it('Renders code block (full)', async () => { const element = await basicFixture(); @@ -37,7 +44,7 @@ describe('', () => { 'body = """test"""', '', 'conn = http.client.HTTPConnection(\'domain.com\')', - 'conn.request(\'POST\',\'/\', body, headers)', + 'conn.request(\'POST\', \'/\', body, headers)', 'res = conn.getresponse()', '', 'data = res.read()', @@ -66,6 +73,28 @@ describe('', () => { const code = element._code.innerText; assert.equal(code.indexOf('req.write(body)'), -1); }); + + it('adds the port number', async () => { + const element = await portNumberFixture(); + const lines = [ + 'import http.client', + '', + 'conn = http.client.HTTPConnection(\'127.0.0.1\', 4242)', + 'conn.request(\'GET\', \'/oauth/token?grant_type=password&username=maex&password=abc123_ABC123\')', + 'res = conn.getresponse()', + '', + 'data = res.read()', + 'print(res.status, res.reason)', + 'print(data.decode(\'utf-8\'))', + 'print(res.getheaders())' + ]; + await oneEvent(element, 'highlighted'); + const code = element._code.innerText; + const result = code.split('\n'); + for (let i = 0; i < result.length; i++) { + assert.equal(result[i], lines[i], `Line ${i} matches`); + } + }); }); describe('_getHeaders()', () => {