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

Commit

Permalink
array response is not modified unless keepCookie is used
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Feb 6, 2021
1 parent 0b627db commit fd53a36
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 39 deletions.
7 changes: 5 additions & 2 deletions lib/Adaptor.js
Expand Up @@ -159,8 +159,11 @@ function handleCookies(response) {
});
}

const extendableData = Array.isArray(data) ? {
body: data
} : data;
return { ...response,
data: { ...data,
data: { ...extendableData,
__cookie: (keepCookies === null || keepCookies === void 0 ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies,
__headers: response.headers
}
Expand All @@ -171,7 +174,7 @@ function handleCookies(response) {
}

function handleResponse(state, response) {
console.log('✓', response.config.method.toUpperCase(), 'request succeeded with', response.status);
console.log(response.config.method.toUpperCase(), 'request succeeded with', response.status, '✓');
const compatibleResp = { ...response,
httpStatus: response.status,
message: response.statusText,
Expand Down
6 changes: 1 addition & 5 deletions lib/Utils.js
Expand Up @@ -54,11 +54,7 @@ function assembleError({
}

function tryJson(data) {
if (Array.isArray(data)) {
return {
body: data
};
} else if (typeof data === 'string') {
if (typeof data === 'string') {
try {
return JSON.parse(data);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@openfn/language-http",
"version": "3.1.4",
"version": "3.1.5",
"description": "An HTTP request language package for use with Open Function",
"homepage": "https://docs.openfn.org",
"repository": {
Expand Down
10 changes: 6 additions & 4 deletions src/Adaptor.js
Expand Up @@ -75,10 +75,12 @@ function handleCookies(response) {
});
}

const extendableData = Array.isArray(data) ? { body: data } : data;

return {
...response,
data: {
...data,
...extendableData,
__cookie: keepCookies?.length === 1 ? keepCookies[0] : keepCookies,
__headers: response.headers,
},
Expand All @@ -90,12 +92,12 @@ function handleCookies(response) {

function handleResponse(state, response) {
console.log(
'✓',
response.config.method.toUpperCase(),
'request succeeded with',
response.status
response.status,
'✓'
);

const compatibleResp = {
...response,
httpStatus: response.status,
Expand Down
4 changes: 1 addition & 3 deletions src/Utils.js
Expand Up @@ -39,9 +39,7 @@ export function assembleError({ response, error, params }) {
}

export function tryJson(data) {
if (Array.isArray(data)) {
return { body: data };
} else if (typeof data === 'string') {
if (typeof data === 'string') {
try {
return JSON.parse(data);
} catch (e) {
Expand Down
36 changes: 12 additions & 24 deletions test/index.js
Expand Up @@ -259,19 +259,16 @@ describe('get()', () => {
})
)(state);

expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders');
expect(finalState.data[0]).to.eql('/api/showMeMyHeaders');

expect(finalState.data.body[1]).to.haveOwnProperty('x-openfn', 'testing');
expect(finalState.data[1]).to.haveOwnProperty('x-openfn', 'testing');

expect(finalState.data.body[1]).to.haveOwnProperty(
expect(finalState.data[1]).to.haveOwnProperty(
'authorization',
'Basic aGVsbG86dGhlcmU='
);

expect(finalState.data.body[1]).to.haveOwnProperty(
'host',
'www.example.com'
);
expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com');

expect(finalState.references).to.eql([{ triggering: 'event' }]);
});
Expand All @@ -288,15 +285,12 @@ describe('get()', () => {
const finalState = await execute(
get('https://www.example.com/api/showMeMyHeaders')
)(state);
expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders');
expect(finalState.data.body[1]).to.haveOwnProperty(
expect(finalState.data[0]).to.eql('/api/showMeMyHeaders');
expect(finalState.data[1]).to.haveOwnProperty(
'authorization',
'Basic aGVsbG86dGhlcmU='
);
expect(finalState.data.body[1]).to.haveOwnProperty(
'host',
'www.example.com'
);
expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com');
});

it('can enable gzip', async () => {
Expand All @@ -309,17 +303,14 @@ describe('get()', () => {
get('https://www.example.com/api/showMeMyHeaders', { gzip: true })
)(state);

expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders');
expect(finalState.data[0]).to.eql('/api/showMeMyHeaders');

expect(finalState.data.body[1]).to.haveOwnProperty(
expect(finalState.data[1]).to.haveOwnProperty(
'accept-encoding',
'gzip, deflate'
);

expect(finalState.data.body[1]).to.haveOwnProperty(
'host',
'www.example.com'
);
expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com');
});

it('allows query strings to be set', async () => {
Expand All @@ -332,12 +323,9 @@ describe('get()', () => {
get('https://www.example.com/api/showMeMyHeaders', { query: { id: 1 } })
)(state);

expect(finalState.data.body[0]).to.eql('/api/showMeMyHeaders?id=1');
expect(finalState.data[0]).to.eql('/api/showMeMyHeaders?id=1');

expect(finalState.data.body[1]).to.haveOwnProperty(
'host',
'www.example.com'
);
expect(finalState.data[1]).to.haveOwnProperty('host', 'www.example.com');
});

it('can follow redirects', async () => {
Expand Down

0 comments on commit fd53a36

Please sign in to comment.