package com.wizzardo.http;

import com.wizzardo.http.mapping.Path;
import org.junit.Assert;
import org.junit.Test;

/**
 * Created by wizzardo on 16.01.15.
 */
public class PathTest {

    private Path parse(String s) {
        return Path.parse(s.getBytes());
    }

    @Test
    public void parsing() {
        Path path;

        path = parse("/");
        Assert.assertEquals("/", path.toString());
        Assert.assertEquals(0, path.length());
        Assert.assertEquals(true, path.isEndsWithSlash());

        path = parse("/foo");
        Assert.assertEquals("/foo", path.toString());
        Assert.assertEquals(1, path.length());
        Assert.assertEquals("foo", path.getPart(0));
        Assert.assertEquals(false, path.isEndsWithSlash());

        path = parse("/foo/");
        Assert.assertEquals("/foo/", path.toString());
        Assert.assertEquals(1, path.length());
        Assert.assertEquals("foo", path.getPart(0));
        Assert.assertEquals(true, path.isEndsWithSlash());

        path = parse("/foo/bar");
        Assert.assertEquals("/foo/bar", path.toString());
        Assert.assertEquals(2, path.length());
        Assert.assertEquals("foo", path.getPart(0));
        Assert.assertEquals("bar", path.getPart(1));
        Assert.assertEquals(false, path.isEndsWithSlash());
    }

    @Test
    public void subPath() {
        Path path;
        Path sub;

        path = parse("/foo/bar");
        sub = path.subPath(1);
        Assert.assertEquals("/bar", sub.toString());
        Assert.assertEquals(1, sub.length());
        Assert.assertEquals("bar", sub.getPart(0));

        path = parse("/foo/bar/key/value");
        sub = path.subPath(1, 3);
        Assert.assertEquals("/bar/key/", sub.toString());
        Assert.assertEquals(2, sub.length());
        Assert.assertEquals("bar", sub.getPart(0));
        Assert.assertEquals("key", sub.getPart(1));

        path = parse("/foo/bar/key/");
        sub = path.subPath(1, 3);
        Assert.assertEquals("/bar/key/", sub.toString());
        Assert.assertEquals(2, sub.length());
        Assert.assertEquals("bar", sub.getPart(0));
        Assert.assertEquals("key", sub.getPart(1));

        path = parse("/foo/bar/key/");
        sub = path.subPath(1, 2);
        Assert.assertEquals("/bar/", sub.toString());
        Assert.assertEquals(1, sub.length());
        Assert.assertEquals("bar", sub.getPart(0));
    }

    @Test
    public void dots() {
        Path path;

        path = parse("/foo/../bar");
        Assert.assertEquals("/foo/../bar", path.toString());
        Assert.assertEquals(1, path.length());
        Assert.assertEquals("bar", path.getPart(0));
        Assert.assertEquals(false, path.isEndsWithSlash());

        path = parse("/foo/foo/../../bar");
        Assert.assertEquals("/foo/foo/../../bar", path.toString());
        Assert.assertEquals(1, path.length());
        Assert.assertEquals("bar", path.getPart(0));
        Assert.assertEquals(false, path.isEndsWithSlash());
    }

    @Test
    public void exceptions() {
        ServerTest.checkException(() -> parse("/foo/../../"), IllegalStateException.class, "can't parse: /foo/../../");
        ServerTest.checkException(() -> parse("/.."), IllegalStateException.class, "can't parse: /..");
        ServerTest.checkException(() -> parse("foo"), IllegalStateException.class, "path must starts with '/'");
    }
}

@@ -60,16 +60,77 @@
 g.Path;%0A
+import com.wizzardo.tools.misc.ExceptionDrivenStringBuilder;%0A
 import o
@@ -3438,11 +3438,761 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void add() %7B%0A        Assert.assertTrue(new Path().add(%22%22).isEndsWithSlash());%0A        Assert.assertEquals(%22/%22, new Path().add(%22%22).toString());%0A%0A        ServerTest.checkException(() -%3E new Path().add(%22..%22), IllegalStateException.class, %22can't parse: ..%22);%0A%0A        Assert.assertEquals(%22/foo/bar%22, new Path().add(%22/foo//bar%22).toString());%0A        Assert.assertFalse(new Path().add(%22/foo//bar%22).isEndsWithSlash());%0A        Assert.assertTrue(new Path().add(%22/foo//bar/%22).isEndsWithSlash());%0A    %7D%0A%0A    @Test%0A    public void test_toString() %7B%0A        ExceptionDrivenStringBuilder sb = new ExceptionDrivenStringBuilder();%0A        new Path().add(%22/foo//bar%22).toString(sb);%0A        Assert.assertEquals(%22/foo/bar%22, sb.toString());%0A    %7D
 %0A%7D%0A

improve tests