Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
fix: fixes #18 - port info in Python snippets
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Psztyc <[email protected]>
  • Loading branch information
jarrodek committed Dec 1, 2021
1 parent 3560925 commit a9542d8
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 8 deletions.
15 changes: 15 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ <h3>Payload only</h3>
</script>
</template>
</arc-demo-helper>

<h3>Port number request</h3>
<arc-demo-helper>
<template>
<http-code-snippets url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123" method="GET"></http-code-snippets>
<script>
{
let headers = 'accept: application/vnd.github.v3+json\\n';
headers += 'content-type: application/json';
const snippet = document.querySelectorAll('http-code-snippets')[4];
snippet.headers = headers;
}
</script>
</template>
</arc-demo-helper>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions demo/java.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ <h3>Payload only</h3>
</script>
</template>
</arc-demo-helper>

<h3>Port number request</h3>
<arc-demo-helper>
<template>
<java-http-snippets url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123" method="GET"></java-http-snippets>
<script>
{
const headers = [{
name: 'accept', value: 'application/vnd.github.v3+json'
}];
const snippet = document.querySelectorAll('java-http-snippets')[4];
snippet.headers = headers;
}
</script>
</template>
</arc-demo-helper>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions demo/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ <h3>Payload only</h3>
</script>
</template>
</arc-demo-helper>

<h3>Port number request</h3>
<arc-demo-helper>
<template>
<javascript-http-snippets url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123" method="GET"></javascript-http-snippets>
<script>
{
const headers = [{
name: 'accept', value: 'application/vnd.github.v3+json'
}];
const snippet = document.querySelectorAll('javascript-http-snippets')[4];
snippet.headers = headers;
}
</script>
</template>
</arc-demo-helper>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions demo/node.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ <h3>Payload only</h3>
</script>
</template>
</arc-demo-helper>

<h3>Port number request</h3>
<arc-demo-helper>
<template>
<node-http-snippet url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123" method="GET"></node-http-snippet>
<script>
{
const headers = [{
name: 'accept', value: 'application/vnd.github.v3+json'
}];
const snippet = document.querySelectorAll('node-http-snippet')[4];
snippet.headers = headers;
}
</script>
</template>
</arc-demo-helper>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions demo/python.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ <h3>Payload only</h3>
</script>
</template>
</arc-demo-helper>

<h3>Port number request</h3>
<arc-demo-helper>
<template>
<python-http-snippets url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123" method="GET"></python-http-snippets>
<script>
{
const headers = [{
name: 'accept', value: 'application/vnd.github.v3+json'
}];
const snippet = document.querySelectorAll('python-http-snippets')[4];
snippet.headers = headers;
}
</script>
</template>
</arc-demo-helper>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion src/HttpCodeSnippetsElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
10 changes: 7 additions & 3 deletions src/Python/Python27HttpSnippetElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
}
Expand Down
8 changes: 6 additions & 2 deletions src/Python/Python31HttpSnippetElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
30 changes: 29 additions & 1 deletion test/python-27-http-snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('<python-27-http-snippet>', () => {
url="http://domain.com"></python-27-http-snippet>`));
}

/**
* @returns {Promise<Python27HttpSnippetElement>}
*/
async function portNumberFixture() {
return (fixture(html`<python-27-http-snippet method="GET" url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123"></python-27-http-snippet>`));
}

describe('python-27-http-snippet', () => {
it('Renders code block (full)', async () => {
const element = await basicFixture();
Expand All @@ -38,7 +45,7 @@ describe('<python-27-http-snippet>', () => {
'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)',
Expand Down Expand Up @@ -66,6 +73,27 @@ describe('<python-27-http-snippet>', () => {
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()', () => {
Expand Down
31 changes: 30 additions & 1 deletion test/python-31-http-snippet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ describe('<python-31-http-snippet>', () => {
url="http://domain.com"></python-31-http-snippet>`));
}

/**
* @returns {Promise<Python31HttpSnippetElement>}
*/
async function portNumberFixture() {
return (fixture(html`<python-31-http-snippet method="GET" url="http://127.0.0.1:4242/oauth/token?grant_type=password&amp;username=maex&amp;password=abc123_ABC123"></python-31-http-snippet>`));
}

describe('python-31-http-snippet', () => {
it('Renders code block (full)', async () => {
const element = await basicFixture();
Expand All @@ -37,7 +44,7 @@ describe('<python-31-http-snippet>', () => {
'body = """test"""',
'',
'conn = http.client.HTTPConnection(\'domain.com\')',
'conn.request(\'POST\',\'/\', body, headers)',
'conn.request(\'POST\', \'/\', body, headers)',
'res = conn.getresponse()',
'',
'data = res.read()',
Expand Down Expand Up @@ -66,6 +73,28 @@ describe('<python-31-http-snippet>', () => {
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()', () => {
Expand Down

0 comments on commit a9542d8

Please sign in to comment.