Skip to content

Commit

Permalink
Merge pull request #71 from EvaEngine/feature/pagination-x-forwarded
Browse files Browse the repository at this point in the history
Feature/pagination x forwarded
  • Loading branch information
mr5 authored Jul 2, 2018
2 parents 300f5d4 + 3963406 commit ebdab6d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,5 @@ npm link evaengine

## Import eslint with airbnb code standard for Webstorm

WebStorm > Preferences > Editor > Code Style > JavaScript > Scheme > Import Scheme > Choose `airbnb_code_style.xml` under this project

WebStorm > Preferences > Editor > Code Style > JavaScript > Scheme > Import Scheme > Choose `airbnb_code_style.xml` under this project
34 changes: 31 additions & 3 deletions src/utils/pagination.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import snakeCase from 'lodash/snakeCase';
import transform from 'lodash/transform';
import nodePath from 'path';

const toUrl = (scheme, host, path, query = {}) => {
let queryString = Object.keys(query)
Expand All @@ -9,11 +10,38 @@ const toUrl = (scheme, host, path, query = {}) => {
return `${scheme}://${host}${path}${queryString}`;
};

const toPaginationUrl = (query, req) =>
toUrl(
req.protocol, req.get('host'), req.baseUrl + req.path,
const toPaginationUrl = (query, req) => {
const hostInfo = req.get('host').split(':');
let scheme = req.protocol;
let host = hostInfo[0];
let port = hostInfo[1];
if (!port) {
port = req.protocol === 'https' ? 443 : 80;
}
let requestPath = req.baseUrl;
if (req.get('x-forwarded-port')) {
port = req.get('x-forwarded-port');
}
if (req.get('x-forwarded-host')) {
host = req.get('x-forwarded-host');
}
if (req.get('x-forwarded-proto')) {
scheme = req.get('x-forwarded-proto');
}
if (req.get('x-forwarded-prefix')) {
requestPath = nodePath.join(req.get('x-forwarded-prefix'), requestPath, req.path);
}
if (
(scheme === 'http' && String(port) !== '80')
|| (scheme === 'https' && String(port) !== '443')
) {
host = `${host}:${port}`;
}
return toUrl(
scheme, host, requestPath,
Object.assign(req.query, query)
);
};


const toPositiveInteger = (number) => {
Expand Down
164 changes: 87 additions & 77 deletions test/utils/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ const req = {
method: 'GET',
baseUrl: '/',
path: '',
get: () => 'localhost',
get(name) {
const headers = {
host: 'localhost'
};
return headers[name];
},
query: {}
};

test('Works when no data', (t) => {
const {
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 0,
limit: 10,
offset: 15,
req
});
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 0,
limit: 10,
offset: 15,
req
});
t.is(total, 0);
t.is(offset, 15);
t.is(limit, 10);
Expand All @@ -36,15 +41,15 @@ test('Works when no data', (t) => {

test('Works when less than 1 page', (t) => {
const {
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 3,
limit: 5,
offset: 0,
req
});
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 3,
limit: 5,
offset: 0,
req
});
t.is(total, 3);
t.is(offset, 0);
t.is(limit, 5);
Expand All @@ -60,15 +65,15 @@ test('Works when less than 1 page', (t) => {

test('Works when normal', (t) => {
const {
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 30,
req
});
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 30,
req
});
t.is(total, 100);
t.is(offset, 30);
t.is(limit, 15);
Expand All @@ -84,15 +89,15 @@ test('Works when normal', (t) => {

test('Works when not aligned', (t) => {
const {
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 5,
req
});
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 5,
req
});
t.is(total, 100);
t.is(offset, 5);
t.is(limit, 15);
Expand All @@ -108,15 +113,15 @@ test('Works when not aligned', (t) => {

test('Works on last page', (t) => {
const {
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 95,
req
});
total, offset, limit, prev, next, isFirst, isLast,
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 95,
req
});
t.is(total, 100);
t.is(offset, 95);
t.is(limit, 15);
Expand All @@ -132,29 +137,29 @@ test('Works on last page', (t) => {

test('Works when illegal args', (t) => {
const {
total, offset, limit
} =
pagination({
total: 'abc',
limit: [],
offset: 'foo',
req
});
total, offset, limit
} =
pagination({
total: 'abc',
limit: [],
offset: 'foo',
req
});
t.is(total, 0);
t.is(offset, 0);
t.is(limit, 1);
});

test('Works when negative', (t) => {
const {
total, offset, limit, prev
} =
pagination({
total: 10,
limit: -10,
offset: -20,
req
});
total, offset, limit, prev
} =
pagination({
total: 10,
limit: -10,
offset: -20,
req
});
t.is(total, 10);
t.is(offset, -20);
t.is(limit, 1);
Expand All @@ -163,21 +168,26 @@ test('Works when negative', (t) => {

test('Should keep request query', (t) => {
const {
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 5,
req: {
protocol: 'http',
method: 'GET',
baseUrl: '/',
path: '',
get: () => 'localhost',
query: { foo: 'bar' }
}
});
prevUri, nextUri, firstUri, lastUri
} =
pagination({
total: 100,
limit: 15,
offset: 5,
req: {
protocol: 'http',
method: 'GET',
baseUrl: '/',
path: '',
get(name) {
const headers = {
host: 'localhost'
};
return headers[name];
},
query: { foo: 'bar' }
}
});
t.is(prevUri, 'http://localhost/?foo=bar&offset=-10&limit=15');
t.is(nextUri, 'http://localhost/?foo=bar&offset=20&limit=15');
t.is(firstUri, 'http://localhost/?foo=bar&offset=0&limit=15');
Expand Down

0 comments on commit ebdab6d

Please sign in to comment.