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

Commit

Permalink
Remove request.req support #78
Browse files Browse the repository at this point in the history
Fixes #78
  • Loading branch information
tripodsan committed Oct 31, 2018
1 parent dedb654 commit 096e740
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 49 deletions.
27 changes: 10 additions & 17 deletions src/utils/openwhisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,18 @@ const owwrapper = require('@adobe/openwhisk-loggly-wrapper');
*/
function extractClientRequest(action) {
const { request } = action;
try { // the edge encodes the client request parameters into the `params` param ;-)
if (!request || !request.params) {
return {};
}
if (request.params.params) {
return {
params: querystring.parse(request.params.params),
headers: request.headers,
method: request.method,
};
}
// alternatively it can encode the entire request as json
if (request.params.req) {
return JSON.parse(request.params.req);
}
if (!request || !request.params) {
return {};
} catch (e) {
throw Error(`Error while parsing incoming request parameter: ${e.message}`);
}
return {
// the edge encodes the client request parameters into the `params` param ;-)
params: request.params.params ? querystring.parse(request.params.params) : {},
headers: request.headers,
method: request.method,
path: request.params.path || '',
extension: request.params.extension || '',
selector: request.params.selector || '',
};
}

/**
Expand Down
58 changes: 26 additions & 32 deletions test/testOpenwhisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,6 @@ describe('Testing OpenWhisk adapter', () => {
assert.ok(out, 'missing request object');
});

it('extractClientRequest needs to parse req parameter', () => {
const testObject = {
url: 'url',
headers: {
h1: '1',
h2: '2',
},
params: {
p1: '1',
p2: true,
p3: ['a', 'b', 'c'],
p4: {
p41: '1',
},
},
};
const out = extractClientRequest({ request: { params: { req: JSON.stringify(testObject) } } });
assert.ok(out, 'missing request object');
assert.deepStrictEqual(testObject, out, 'request object does not match incoming req');
});

it('extractClientRequest throws error on wrong req parameter', () => {
try {
extractClientRequest({ request: { params: { req: 'this is not json' } } });
assert.fail('should fail.');
} catch (e) {
// ok.
}
});

it('extractClientRequest acts reasonably with no request object', () => {
const out = extractClientRequest({});
assert.ok(out, 'missing request object');
Expand All @@ -122,17 +92,25 @@ describe('Testing OpenWhisk adapter', () => {
__ow_logger: log,
__ow_method: 'get',
path: '/test',
extension: 'html',
selector: 'print.preview',
params: 'a=42&b=green',
SECRET: '1234',
};
let action = {};
let payload = {};
await runPipeline((p, a) => {
action = a;
payload = p;
}, pipe, params);

assert.deepEqual({
assert.deepEqual(action, {
request: {
params: {
path: '/test',
extension: 'html',
selector: 'print.preview',
params: 'a=42&b=green',
},
headers: {
Host: 'example.com',
Expand All @@ -143,6 +121,22 @@ describe('Testing OpenWhisk adapter', () => {
secrets: {
SECRET: '1234',
},
}, action);
});

assert.deepEqual(payload, {
request: {
params: {
a: '42',
b: 'green',
},
headers: {
Host: 'example.com',
},
method: 'get',
path: '/test',
extension: 'html',
selector: 'print.preview',
},
});
});
});

0 comments on commit 096e740

Please sign in to comment.