Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringify query params which are objects as JSON #325

Merged
merged 1 commit into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/http-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ HttpInvocation.prototype._processArg = function(req, verb, query, accept) {
// From the query string
if (val !== undefined) {
query = query || {};
query[name] = val;
query[name] = serializeQueryStringValue(val, accept);
}
break;
case 'header':
Expand All @@ -148,7 +148,7 @@ HttpInvocation.prototype._processArg = function(req, verb, query, accept) {
// default to query string for GET
if (val !== undefined) {
query = query || {};
query[name] = val;
query[name] = serializeQueryStringValue(val, accept);
}
} else {
// default to storing args on the body for !GET
Expand All @@ -159,6 +159,15 @@ HttpInvocation.prototype._processArg = function(req, verb, query, accept) {
return query;
};

function serializeQueryStringValue(val, accept) {
if ((accept.type === 'object' || accept.type === 'string') &&
typeof val === 'object') {
return JSON.stringify(val);
} else {
return val;
}
}

/**
* Build args object from the http context's `req` and `res`.
*/
Expand Down
145 changes: 145 additions & 0 deletions test/http-invocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,133 @@ describe('HttpInvocation', function() {
inv.transformResponse(res, body, cb);
}
});

describe('createRequest', function() {
it('creates a simple request', function() {
var inv = givenInvocationForEndpoint(null, []);
var expectedReq = { method: 'GET',
url: 'http://base/testModel/testMethod',
protocol: 'http:',
json: true,
};
expect(inv.createRequest()).to.eql(expectedReq);
});

it('makes primitive type arguments as query params', function() {
var accepts = [
{ arg: 'a', type: 'number' },
{ arg: 'b', type: 'string' },
];
var aValue = 2;
var bValue = 'foo';
var inv = givenInvocationForEndpoint(accepts, [aValue, bValue]);
var expectedReq = { method: 'GET',
url: 'http://base/testModel/testMethod?a=2&b=foo',
protocol: 'http:',
json: true,
};
expect(inv.createRequest()).to.eql(expectedReq);
});

it('makes an array argument as a query param', function() {
var accepts = [
{ arg: 'a', type: 'object' },
];
var aValue = [1, 2, 3];
var inv = givenInvocationForEndpoint(accepts, [aValue]);
var expectedReq = { method: 'GET',
url: 'http://base/testModel/testMethod?a=' + encodeURIComponent('[1,2,3]'),
protocol: 'http:',
json: true,
};
expect(inv.createRequest()).to.eql(expectedReq);
});

it('keeps an empty array as a query param', function() {
var accepts = [
{ arg: 'a', type: 'object' },
];
var aValue = [];
var inv = givenInvocationForEndpoint(accepts, [aValue]);
var expectedReq = { method: 'GET',
url: 'http://base/testModel/testMethod?a=' + encodeURIComponent('[]'),
protocol: 'http:',
json: true,
};
expect(inv.createRequest()).to.eql(expectedReq);
});

it('keeps an empty array as a body param for a POST request', function() {
var accepts = [
{ arg: 'a', type: 'object' },
];
var aValue = [];
var inv = givenInvocationForEndpoint(accepts, [aValue], 'POST');
var expectedReq = { method: 'POST',
url: 'http://base/testModel/testMethod',
protocol: 'http:',
json: true,
body: {
a: [],
},
};
expect(inv.createRequest()).to.eql(expectedReq);
});

it('handles a loopback filter as a query param', function() {
var accepts = [
{ arg: 'filter', type: 'object' },
];
var filter = {
where: {
id: {
inq: [1, 2],
},
typeId: {
inq: [],
},
},
include: ['related'],
};
var inv = givenInvocationForEndpoint(accepts, [filter]);
var expectedFilter =
'{"where":{"id":{"inq":[1,2]},"typeId":{"inq":[]}},"include":["related"]}';
var expectedReq = { method: 'GET',
url: 'http://base/testModel/testMethod?filter=' +
encodeURIComponent(expectedFilter),
protocol: 'http:',
json: true,
};
expect(inv.createRequest()).to.eql(expectedReq);
});
});

it('handles a loopback filter as a body param for a POST request', function() {
var accepts = [
{ arg: 'filter', type: 'object' },
];
var filter = {
where: {
id: {
inq: [1, 2],
},
typeId: {
inq: [],
},
},
include: ['related'],
};
var inv = givenInvocationForEndpoint(accepts, [filter], 'POST');
var expectedReq = { method: 'POST',
url: 'http://base/testModel/testMethod',
protocol: 'http:',
json: true,
body: {
filter: filter,
},
};
expect(inv.createRequest()).to.eql(expectedReq);
});
});

function givenSharedStaticMethod(fn, config) {
Expand All @@ -217,3 +344,21 @@ function givenInvocation(method, params) {
params.auth,
params.typeRegistry || new TypeRegistry());
}

function givenInvocationForEndpoint(accepts, args, verb) {
var method = givenSharedStaticMethod({
accepts: accepts,
});
method.getEndpoints = function() {
return [createEndpoint({ verb: verb || 'GET' })];
};
return givenInvocation(method, { ctorArgs: [], args: args, baseUrl: 'http://base' });
}

function createEndpoint(config) {
config = config || {};
return {
verb: config.verb || 'GET',
fullPath: config.fullPath || '/testModel/testMethod',
};
}