Add e2e test cases for [[/]]

This commit is contained in:
Shin'ya Ueoka 2018-05-13 14:41:03 +09:00
parent d844440a30
commit 28da164e5c
3 changed files with 120 additions and 8 deletions

View file

@ -1,3 +1,5 @@
'use strict';
var serverUrl = require('./url');
var http = require('http');
var url = require('url');
@ -7,15 +9,66 @@ const handleScroll = (req, res) => {
res.end('<!DOCTYPEhtml><html lang="en"><body style="width:10000px; height:10000px"></body></html">');
};
const handleAPagenation = (req, res) => {
let u = url.parse(req.url);
let params = new url.URLSearchParams(u.search);
let page = params.get('page') === null ? null : Number(params.get('page'));
if (page === null || isNaN(page)) {
return handle404(req, res);
}
let body = '';
let nextLink = u.pathname + '?page=' + (page + 1);
let prevLink = u.pathname + '?page=' + (page - 1);
if (page > 1) {
body += '<a href="' + prevLink + '">prev</a> | ';
}
body += '<a href="' + nextLink + '">next</a>';
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<!DOCTYPEhtml><html lang="en"><body">' + body + '</body></html">');
};
const handleLinkPagenation = (req, res) => {
let u = url.parse(req.url);
let params = new url.URLSearchParams(u.search);
let page = params.get('page') === null ? null : Number(params.get('page'));
if (page === null || isNaN(page)) {
return handle404(req, res);
}
let head = '';
let nextLink = u.pathname + '?page=' + (page + 1);
let prevLink = u.pathname + '?page=' + (page - 1);
if (page > 1) {
head += '<link rel="prev" href="' + prevLink + '"></link>';
}
head += '<link rel="next" href="' + nextLink + '"></link>';
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<!DOCTYPEhtml><html lang="en"><head>' + head + '</head><body"></body></html">');
};
const handle404 = (req, res) => {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('not found')
};
http.createServer(function (req, res) {
if (req.method !== 'GET') {
handle404(req, res);
}
let u = url.parse(req.url);
if (req.method === 'GET' && u.pathname === '/scroll') {
if (u.pathname === '/scroll') {
handleScroll(req, res);
} else if (u.pathname === '/a-pagenation') {
handleAPagenation(req, res);
} else if (u.pathname === '/link-pagenation') {
handleLinkPagenation(req, res);
} else {
handle404(req, res);
}