/* eslint-env mocha */
import { Url, serialize, unserialize } from '../src/url.js';

describe('Unit: Url', () => {
    it('should check url validity', () => {
        assert.doesNotThrow(() => new Url('local.host'), 'local.host');
        assert.doesNotThrow(() => new Url('local.host:80'), 'local.host:80');
        assert.doesNotThrow(() => new Url('//local.host'), '//local.host');
        assert.doesNotThrow(() => new Url('//local.host:80'), '//local.host:80');
        assert.doesNotThrow(() => new Url('http://localhost'), 'http://localhost');
        assert.doesNotThrow(() => new Url('http://localhost:80'), 'http://localhost:80');
        assert.doesNotThrow(() => new Url('/posts'), '/posts');
        assert.doesNotThrow(() => new Url('/posts?size=50'), '/posts?size=50');
        assert.doesNotThrow(() => new Url('/posts?size=50&help'), '/posts?size=50&help');
        assert.doesNotThrow(() => new Url('/posts?size=50/help'), '/posts?size=50/help');
        assert.doesNotThrow(() => new Url('file:///'), 'file:///');
        assert.doesNotThrow(() => new Url('file:///'), 'file:///');
        assert.throws(() => new Url('http://'), SyntaxError, 'http://');
        assert.throws(() => new Url(':80'), SyntaxError, ':80');
        assert.throws(() => new Url('http:///'), SyntaxError, 'http:///');
        assert.throws(() => new Url('http://:80'), SyntaxError, 'http://:80');
        assert.throws(() => new Url('?size=50'), SyntaxError, '?size=50');
        assert.throws(() => new Url('?size=50/help'), SyntaxError, '?size=50/help');
    });

    it('should get information on a base url', () => {
        let url = new Url('http://www.chialab.it:80/posts?size=10');

        assert.equal(url.href, 'http://www.chialab.it:80/posts?size=10');
        assert.equal(url.protocol, 'http:');
        assert.equal(url.hostname, 'www.chialab.it');
        assert.equal(url.port, '80');
        assert.equal(url.host, 'www.chialab.it:80');
        assert.equal(url.search, '?size=10');
        assert.equal(url.origin, 'http://www.chialab.it:80');
    });

    it('should serialize simple string', () => {
        let person = {
            firstName: 'Alan',
            age: 21,
            birthday: new Date('1912/06/12'),
        };
        let serialized = serialize(person);
        let unserialized = unserialize(serialized);

        assert.equal(serialized, `firstName=Alan&age=21&birthday=${encodeURIComponent('1912-06-11T22:00:00.000Z')}`);
        assert.equal(person.firstName, unserialized.firstName);
        assert.equal(person.age, unserialized.age);
        assert.equal(person.birthday.getTime(), new Date(unserialized.birthday).getTime());
    });
});

@@ -626,32 +626,182 @@
 localhost:80');%0A
+        assert.doesNotThrow(() =%3E new Url('127.0.0.1'), '127.0.0.1');%0A        assert.doesNotThrow(() =%3E new Url('127.0.0.1:8080'), '127.0.0.1:8080');%0A
         assert.d
@@ -844,24 +844,24 @@
  '/posts');%0A
-
         asse
@@ -1180,76 +1180,8 @@
 ');%0A
-        assert.doesNotThrow(() =%3E new Url('file:///'), 'file:///');%0A
     
@@ -1624,24 +1624,447 @@
 =50/help');%0A
+        // unhandled yet%0A        // assert.doesNotThrow(() =%3E new Url('%5B::1%5D'), '%5B::1%5D');%0A        // assert.doesNotThrow(() =%3E new Url('%5B::1%5D:8080'), '%5B::1%5D:8080');%0A        // assert.doesNotThrow(() =%3E new Url('https://user:pass@%5B::1%5D:443/path?query#id'), 'https://user:pass@%5B::1%5D:443/path?query#id');%0A        // assert.doesNotThrow(() =%3E new Url('http://%5B2a00:1450:4002:809::200e%5D'), 'http://%5B2a00:1450:4002:809::200e%5D');%0A
     %7D);%0A%0A   
@@ -2730,14 +2730,28 @@
 1912
-/06/12
+-06-11T22:00:00.000Z
 '),%0A

fix serialize test