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());
    }
}

@@ -2992,11 +2992,396 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void exceptions() %7B%0A        ServerTest.checkException(() -%3E parse(%22/foo/../../%22), IllegalStateException.class, %22can't parse: /foo/../../%22);%0A        ServerTest.checkException(() -%3E parse(%22/..%22), IllegalStateException.class, %22can't parse: /..%22);%0A        ServerTest.checkException(() -%3E parse(%22foo%22), IllegalStateException.class, %22path must starts with '/'%22);%0A    %7D
 %0A%7D%0A

improve tests