package com.wizzardo.http.filter;

import com.wizzardo.http.ServerTest;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

/**
 * @author: wizzardo
 * Date: 29.11.14
 */
public class FilterTest extends ServerTest {

    @Test
    public void testBasicAuth() throws IOException {
        handler = (request, response) -> response.setBody("ok");
        String user = "user";
        String password = "password";
        server.getFiltersMapping().addBefore("/*", new BasicAuthFilter().allow(user, password));

        Assert.assertEquals(401, makeRequest("").get().getResponseCode());
        Assert.assertEquals("ok", makeRequest("").setBasicAuthentication(user, password).get().asString());
    }

    @Test
    public void testChain() throws IOException {
        handler = (request, response) -> response.setBody(request.param("all") + " " + request.param("foo") + " " + request.param("bar"));
        server.getFiltersMapping().addBefore("/*", (request, response) -> {
            request.param("all", "true");
            return true;
        });
        server.getFiltersMapping().addBefore("/foo/*", (request, response) -> {
            request.param("foo", "true");
            return true;
        });
        server.getFiltersMapping().addBefore("/foo/bar/*", (request, response) -> {
            request.param("bar", "true");
            return true;
        });

        Assert.assertEquals("true true true", makeRequest("/foo/bar/123").get().asString());
        Assert.assertEquals("true true true", makeRequest("/foo/bar").get().asString());
        Assert.assertEquals("true true null", makeRequest("/foo/").get().asString());
        Assert.assertEquals("true true null", makeRequest("/foo").get().asString());
        Assert.assertEquals("true null null", makeRequest("/").get().asString());
    }
}

@@ -1837,10 +1837,1136 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void testChainReverse() throws IOException %7B%0A        handler = (request, response) -%3E response.setBody(request.param(%22all%22) + %22 %22 + request.param(%22foo%22) + %22 %22 + request.param(%22bar%22));%0A        server.getFiltersMapping().addBefore(%22/foo/bar/*%22, (request, response) -%3E %7B%0A            request.param(%22bar%22, %22true%22);%0A            return true;%0A        %7D);%0A        server.getFiltersMapping().addBefore(%22/foo/*%22, (request, response) -%3E %7B%0A            request.param(%22foo%22, %22true%22);%0A            return true;%0A        %7D);%0A        server.getFiltersMapping().addBefore(%22/*%22, (request, response) -%3E %7B%0A            request.param(%22all%22, %22true%22);%0A            return true;%0A        %7D);%0A%0A        Assert.assertEquals(%22true true true%22, makeRequest(%22/foo/bar/123%22).get().asString());%0A        Assert.assertEquals(%22true true true%22, makeRequest(%22/foo/bar%22).get().asString());%0A        Assert.assertEquals(%22true true null%22, makeRequest(%22/foo/%22).get().asString());%0A        Assert.assertEquals(%22true true null%22, makeRequest(%22/foo%22).get().asString());%0A        Assert.assertEquals(%22true null null%22, makeRequest(%22/%22).get().asString());%0A    %7D%0A%0A
 %7D%0A

add another test