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

fixed getMultiple endpoint issues #4

Merged
merged 2 commits into from
May 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/getMultiple.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const is = require("is_js");
const lambda = require("./util/lambda");
const { getItems } = require("./util/persistence");
const { isurl, assert } = require("./util/util");
const { isurl, assert, unique } = require("./util/util");

module.exports.fn = lambda(async (event, success) => {
assert(is.array(event.body), "getMultiple requires an array");
assert(event.body.every(isurl), "getMultiple requires an array of URLs");
const urls = JSON.parse(event.body);

// limit the query to 100 URLs
const urls = event.body.slice(0, 100);
assert(is.array(urls), "getMultiple requires an array");
assert(urls.every(isurl), "getMultiple requires an array of URLs");

const items = await getItems(urls);
// limit the query to 100 URLs
const items = await getItems(unique(urls.slice(0, 100)));
success(items);
});
15 changes: 12 additions & 3 deletions src/getMultiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ jest.setMock("./util/persistence", {
const getMultiple = require("./getMultiple").fn;

test("returns the correct response", done => {
const urls = ["google.com", "microsoft.com"];
const urls = JSON.stringify(["google.com", "microsoft.com"]);
getMultiple({ body: urls }, undefined, (error, response) => {
const body = JSON.parse(response.body);
expect(body.length).toBe(2);
done();
});
});

test("removes duplicate URLs", done => {
const urls = JSON.stringify(["google.com", "google.com", "microsoft.com"]);
getMultiple({ body: urls }, undefined, (error, response) => {
const body = JSON.parse(response.body);
expect(body.length).toBe(2);
Expand All @@ -32,9 +41,9 @@ test("validates that the request is an array", done => {
test("limits the request to 100 URLs", done => {
const urls = [];
for (var i = 0; i < 200; i++) {
urls.push("google.com");
urls.push("google.com/" + i);
}
getMultiple({ body: urls }, undefined, (error, response) => {
getMultiple({ body: JSON.stringify(urls) }, undefined, (error, response) => {
const body = JSON.parse(response.body);
expect(body.length).toBe(100);
done();
Expand Down
5 changes: 4 additions & 1 deletion src/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const isurl = url => url.match(/^http:\/\/localhost:\d*\/?$/) || is.url(url);

const clamp = (value, lower, upper) => Math.max(lower, Math.min(value, upper));

const unique = items => Array.from(new Set(items));

const assert = (truth, message) => {
if (!truth) {
throw new Error(`assertion failure: ${message}`);
Expand Down Expand Up @@ -33,5 +35,6 @@ module.exports = {
clamp,
assert,
getSourceUrl,
normalizeUrl
normalizeUrl,
unique
};
7 changes: 2 additions & 5 deletions test/multiple.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"headers": {
"Referer": "http://www.google.com"
"Referer": "www.google.com"
},
"body": [
"http://www.google.com",
"http://www.google.com"
]
"body": "[\"www.google.com\", \"www.colineberhardt.com\"]"
}