Add request filter.

This commit is contained in:
Stephan Schnabel 2020-08-20 19:40:48 +02:00
parent c4b1d1d2f6
commit c3d38e2d11
Signed by: stephan.schnabel
GPG key ID: F74FE2422AA07290
12 changed files with 500 additions and 0 deletions

View file

@ -0,0 +1,41 @@
package io.kokuwa.micronaut.logging.request;
import javax.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import ch.qos.logback.classic.Level;
import io.kokuwa.micronaut.logging.AbstractTest;
import io.micronaut.test.annotation.MicronautTest;
/**
* Test for MDC and request filter combined.
*
* @author Stephan Schnabel
*/
@DisplayName("request-composite")
@MicronautTest(environments = "test-composite")
public class CompositeTest extends AbstractTest {
@Inject
TestClient client;
@DisplayName("default level")
@Test
void defaultLogging() {
client.assertLevel(Level.INFO, client.token("somebody"), null);
}
@DisplayName("level set by mdc")
@Test
void headerFromMdc() {
client.assertLevel(Level.DEBUG, client.token("horst"), null);
}
@DisplayName("level set by header (overriding mdc)")
@Test
void headerFromHeader() {
client.assertLevel(Level.TRACE, client.token("horst"), "TRACE");
}
}

View file

@ -0,0 +1,57 @@
package io.kokuwa.micronaut.logging.request;
import javax.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import ch.qos.logback.classic.Level;
import io.kokuwa.micronaut.logging.AbstractTest;
/**
* Test for {@link HeaderLoggingHttpFilter}.
*
* @author Stephan Schnabel
*/
@DisplayName("request-header")
public class RequestHeaderTest extends AbstractTest {
@Inject
TestClient client;
@DisplayName("header missing")
@Test
void headerMissing() {
client.assertLevel(Level.INFO, null, null);
}
@DisplayName("header invalid, use DEBUG as default from logback")
@Test
void headerInvalid() {
client.assertLevel(Level.DEBUG, null, "TRCE");
}
@DisplayName("level trace (below default)")
@Test
void headerLevelTrace() {
client.assertLevel(Level.TRACE, null, "TRACE");
}
@DisplayName("level debug (below default)")
@Test
void headerLevelDebug() {
client.assertLevel(Level.DEBUG, null, "DEBUG");
}
@DisplayName("level info (is default)")
@Test
void headerLevelInfo() {
client.assertLevel(Level.INFO, null, "INFO");
}
@DisplayName("level warn (above default)")
@Test
void headerLevelWarn() {
client.assertLevel(Level.INFO, null, "WARN");
}
}

View file

@ -0,0 +1,44 @@
package io.kokuwa.micronaut.logging.request;
import static org.junit.jupiter.api.Assertions.assertEquals;
import javax.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import io.kokuwa.micronaut.logging.AbstractTest;
/**
* Test for {@link PrincipalHttpFilter}.
*
* @author Stephan Schnabel
*/
@DisplayName("request-principal")
public class RequestPrincipalTest extends AbstractTest {
@Inject
TestClient client;
@DisplayName("token missing")
@Test
void tokenMissing() {
assertPrincipal(null, null);
}
@DisplayName("token invalid")
@Test
void tokenInvalid() {
assertPrincipal(null, "meh");
}
@DisplayName("token valid")
@Test
void tokenValid() {
assertPrincipal("meh", client.token("meh"));
}
private void assertPrincipal(String expectedPrincipal, String actualTokenValue) {
assertEquals(expectedPrincipal, client.get(actualTokenValue, null).getPrincipal());
}
}

View file

@ -0,0 +1,64 @@
package io.kokuwa.micronaut.logging.request;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jwt.JWTClaimsSet;
import ch.qos.logback.classic.Level;
import io.kokuwa.micronaut.logging.request.TestController.TestResponse;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.security.token.jwt.signature.SignatureGeneratorConfiguration;
/**
* Contoller for testing {@link HeaderLoggingHttpFilter} and {@link PrincipalHttpFilter}.
*
* @author Stephan Schnabel
*/
@Singleton
public class TestClient {
@Inject
@Client("/")
HttpClient client;
@Inject
SignatureGeneratorConfiguration signature;
String token(String subject) {
try {
return signature.sign(new JWTClaimsSet.Builder().subject(subject).build()).serialize();
} catch (JOSEException e) {
fail("failed to create token");
return null;
}
}
TestResponse get(String token, String header) {
var request = HttpRequest.GET("/");
if (token != null) {
request.bearerAuth(token);
}
if (header != null) {
request.getHeaders().add(HeaderLoggingHttpFilter.DEFAULT_HEADER, header);
}
var response = client.toBlocking().exchange(request, TestResponse.class);
assertEquals(HttpStatus.OK, response.getStatus(), "status");
assertTrue(response.getBody().isPresent(), "body");
return response.body();
}
void assertLevel(Level expectedLevel, String actualTokenValue, String actualHeaderValue) {
assertEquals(expectedLevel.toString(), get(actualTokenValue, actualHeaderValue).getLevel());
}
}

View file

@ -0,0 +1,52 @@
package io.kokuwa.micronaut.logging.request;
import org.slf4j.MDC;
import ch.qos.logback.classic.Level;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* Controller for testing {@link HeaderLoggingHttpFilter} and {@link PrincipalHttpFilter}.
*
* @author Stephan Schnabel
*/
@Secured({ SecurityRule.IS_ANONYMOUS, SecurityRule.IS_AUTHENTICATED })
@Controller
@Slf4j
public class TestController {
@Get("/")
TestResponse run() {
var principal = MDC.get(PrincipalHttpFilter.DEFAULT_KEY);
var level = Level.OFF;
if (log.isTraceEnabled()) {
level = Level.TRACE;
} else if (log.isDebugEnabled()) {
level = Level.DEBUG;
} else if (log.isInfoEnabled()) {
level = Level.INFO;
} else if (log.isWarnEnabled()) {
level = Level.WARN;
} else if (log.isErrorEnabled()) {
level = Level.ERROR;
}
return new TestResponse(level.toString(), principal);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class TestResponse {
private String level;
private String principal;
}
}