package com.hamishrickerby.http_server.auth;

import com.hamishrickerby.http_server.Method;
import com.hamishrickerby.http_server.Request;
import com.hamishrickerby.http_server.helpers.RequestBuilder;
import junit.framework.TestCase;

/**
 * Created by rickerbh on 11/09/2016.
 */
public class RequestAuthenticatorTest extends TestCase {

    public void testCredentialsVerification() {
        RequestAuthenticator authenticator = new RequestAuthenticator();
        authenticator.addCredentials("user", "pass");
        assertTrue(authenticator.authenticate("user", "pass"));
        assertFalse(authenticator.authenticate("user", "fail"));
        assertFalse(authenticator.authenticate("no-user", "pass"));
        assertFalse(authenticator.authenticate("no-user", "fail"));
    }

    public void testRouteProtection() {
        RequestAuthenticator authenticator = new RequestAuthenticator();
        String protectedRoute = "/protected";
        authenticator.addRoute(protectedRoute);
        assertTrue(authenticator.isProtected(protectedRoute));
        assertFalse(authenticator.isProtected("/unprotected"));
    }

    public void testRequestAuthenticationInterface() {
        RequestAuthenticator authenticator = new RequestAuthenticator();
        authenticator.addCredentials("user", "pass");
        authenticator.addRoute("/protected");
        Request request = new RequestBuilder()
                .setMethod(Method.GET)
                .setPath("/protected")
                .addHeader("Authorization: Basic dXNlcjpwYXNz")
                .toRequest();
        assertTrue(authenticator.authenticate(request));

        request = new RequestBuilder()
                .setMethod(Method.GET)
                .setPath("/protected")
                .addHeader("Authorization: Basic dXNlcjpmYWls")
                .toRequest();
        assertFalse(authenticator.authenticate(request));

        request = new RequestBuilder()
                .setMethod(Method.GET)
                .setPath("/unprotected")
                .addHeader("Authorization: Basic dXNlcjpmYWls")
                .toRequest();
        assertTrue(authenticator.authenticate(request));
    }

    public void testEmptyAuthenticationFieldsFail() {
        RequestAuthenticator authenticator = new RequestAuthenticator();
        authenticator.addCredentials("user", "pass");
        authenticator.addRoute("/protected");
        Request request = new RequestBuilder()
                .setMethod(Method.GET)
                .setPath("/protected")
                .toRequest();
        assertFalse(authenticator.authenticate(request));

    }

}

@@ -43,54 +43,8 @@
 h;%0A%0A
-import com.hamishrickerby.http_server.Method;%0A
 impo
@@ -1353,47 +1353,8 @@
 r()%0A
-                .setMethod(Method.GET)%0A
     
@@ -1380,32 +1380,32 @@
 h(%22/protected%22)%0A
+
                 
@@ -1583,47 +1583,8 @@
 r()%0A
-                .setMethod(Method.GET)%0A
     
@@ -1814,47 +1814,8 @@
 r()%0A
-                .setMethod(Method.GET)%0A
     
@@ -2228,32 +2228,32 @@
 (%22/protected%22);%0A
+
         Request 
@@ -2287,47 +2287,8 @@
 r()%0A
-                .setMethod(Method.GET)%0A
     

Remove more setMethod calls