'use strict';

const fs      = require('fs');
const chalk   = require('chalk');
const uuid    = require('node-uuid');
const parseUA = require('useragent').parse;
const express = require('express');
const hndlb   = require('express-handlebars');

var app       = express();
var tasks     = {};
var testCases = JSON.parse(fs.readFileSync('data/test-cases.json').toString());

class TestTask {
    constructor (userAgent) {
        this.id = uuid.v4();
        this.ua = parseUA(userAgent);

        this.pendingTests = testCases.map(testCase => ({
            name:            testCase['test'],
            expected:        testCase['sent-raw'],
            cookies:         JSON.stringify(testCase['received']),
            forcedResultUrl: testCase['sent-to'],
            resultUrl:       testCase['sent-to'] ?
                             testCase['sent-to'] + `&taskId=${this.id}` :
                             `http://home.example.org:8888/cookie-parser-result?taskId=${this.id}`
        }));

        this.fails = [];
    }

    get testUrl () {
        return `http://home.example.org:8888/test?taskId=${this.id}`;
    }

    get currentTest () {
        return this.pendingTests[0];
    }
}

app.engine('handlebars', hndlb());
app.set('views', 'lib/views');
app.set('view engine', 'handlebars');

app.get('*', (req, res, next) => {
    res.set('cache-control', 'no-cache, no-store, must-revalidate');
    next();
});

app.get('/test', (req, res) => {
    var task = tasks[req.query.taskId];

    res.render('test', {
        cookies:   task.currentTest.cookies,
        resultUrl: task.currentTest.resultUrl
    });
});

function parserResult (req, res) {
    if (req.query.delete) {
        res.render('delete-cookies');
        return;
    }

    var task        = tasks[req.query.taskId];
    var currentTest = task.currentTest;
    var actual      = req.headers['cookie'] || '';

    if (actual === currentTest.expected)
        console.log(`${chalk.bold(currentTest.name)} - ${chalk.green('PASS')}`);
    else {
        console.log(`${chalk.bold(currentTest.name)} - ${chalk.red('FAIL')}`);
        console.log('Test case:');
        console.log(chalk.gray(currentTest.cookies));

        if( currentTest.forcedResultUrl){
            console.log('Results observed on page:');
            console.log(chalk.gray(currentTest.forcedResultUrl));
        }

        console.log('Expected:');
        console.log(chalk.gray(currentTest.expected));
        console.log('Actual:');
        console.log(chalk.gray(actual));
        console.log('----------------');
    }

    task.pendingTests.shift();

    if (task.currentTest)
        res.redirect(task.testUrl);
    else
        res.end('Done!');
}

app.get('/cookie-parser-result/*', parserResult);
app.get('/cookie-parser-result', parserResult);

app.get('/run-tests', (req, res) => {
    var task = new TestTask(req.headers['user-agent']);

    tasks[task.id] = task;

    console.log(`Running tests in ${task.ua.family} (v${task.ua.toVersion()})...`);
    console.log('===============================================================');

    res.redirect(task.testUrl);
});

app.listen(8888);
@@ -69,24 +69,63 @@
 e('chalk');%0A
+const iconv   = require('iconv-lite');%0A
 const uuid  
@@ -146,24 +146,24 @@
 ode-uuid');%0A
-
 const parseU
@@ -278,19 +278,21 @@
 ars');%0A%0A
-var
+const
  app    
@@ -307,19 +307,21 @@
 ress();%0A
-var
+const
  tasks  
@@ -329,19 +329,21 @@
   = %7B%7D;%0A
-var
+const
  testCas
@@ -1493,35 +1493,37 @@
 , res) =%3E %7B%0A    
-var
+const
  task = tasks%5Bre
@@ -1793,27 +1793,29 @@
 %0A    %7D%0A%0A    
-var
+const
  task       
@@ -1846,19 +1846,21 @@
 d%5D;%0A    
-var
+const
  current
@@ -1888,19 +1888,19 @@
 st;%0A    
-var
+let
  actual 
@@ -1900,24 +1900,26 @@
 actual      
+  
 = req.header
@@ -1938,16 +1938,126 @@
 %7C%7C '';%0A%0A
+    // NOTE: node exposes HTTP headers in ASCII%0A    actual = (new Buffer(actual, 'ascii')).toString('utf8');%0A%0A
     if (
@@ -2364,10 +2364,10 @@
   if
-(
  
+(
 curr
@@ -2390,16 +2390,17 @@
 sultUrl)
+ 
 %7B%0A      
@@ -3011,11 +3011,13 @@
     
-var
+const
  tas

Fix encdoing issues