Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdan committed Jan 23, 2025
1 parent 5fb3ef0 commit 9f81e22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/test/java/org/primeframework/mvc/GlobalTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001-2024, Inversoft Inc., All Rights Reserved
* Copyright (c) 2001-2025, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -814,6 +814,15 @@ public void get_metricsErrors() {
assertEquals(meters.get("prime-mvc.[*].errors").getCount(), 1);
}

@Test
public void get_modifyRequest() throws Exception {
// The Test HTTP request consumer will have added an HTTP request header.
simulator.test("/foo")
.get()
.assertStatusCode(200)
.custom(result -> assertEquals(result.request.getHeader("X-Test-HTTP-Request-Consumer"), "true"));
}

@Test
public void get_nested_parameters() throws Exception {
test.simulate(() -> simulator.test("/nested")
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/org/primeframework/mvc/PrimeBaseTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2024, Inversoft Inc., All Rights Reserved
* Copyright (c) 2012-2025, Inversoft Inc., All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -86,6 +86,7 @@
import org.primeframework.mvc.security.UserLoginSecurityContext;
import org.primeframework.mvc.security.VerifierProvider;
import org.primeframework.mvc.security.csrf.CSRFProvider;
import org.primeframework.mvc.test.RequestBuilder.HTTPRequestConsumer;
import org.primeframework.mvc.test.RequestSimulator;
import org.primeframework.mvc.util.ThrowingRunnable;
import org.primeframework.mvc.validation.Validation;
Expand Down Expand Up @@ -228,6 +229,7 @@ protected void configure() {
super.configure();
install(new TestMVCConfigurationModule());
bind(CORSConfigurationProvider.class).to(TestCORSConfigurationProvider.class).in(Singleton.class);
bind(HTTPRequestConsumer.class).to(TestHTTPRequestConsumer.class);
bind(MessageObserver.class).toInstance(messageObserver);
bind(MetricRegistry.class).toInstance(metricRegistry);
bind(UserLoginSecurityContext.class).to(MockUserLoginSecurityContext.class);
Expand Down Expand Up @@ -344,6 +346,15 @@ protected void configure() {
}
}

public static class TestHTTPRequestConsumer implements HTTPRequestConsumer {
/**
* @param httpRequest the http request
*/
public void accept(HTTPRequest httpRequest) {
httpRequest.setHeader("X-Test-HTTP-Request-Consumer", "true");
}
}

@SuppressWarnings("unused")
public static class TestListener implements ITestListener {
private final static AtomicInteger TestCounter = new AtomicInteger(0);
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/org/primeframework/mvc/test/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.google.inject.Injector;
import io.fusionauth.http.Cookie;
import io.fusionauth.http.Cookie.SameSite;
Expand Down Expand Up @@ -96,6 +97,9 @@ public class RequestBuilder {

private byte[] body;

@Inject(optional = true)
private HTTPRequestConsumer httpRequestConsumer;

public RequestBuilder(String path, Injector injector, MockUserAgent userAgent, TestMessageObserver messageObserver,
int port) {
this.injector = injector;
Expand All @@ -104,6 +108,8 @@ public RequestBuilder(String path, Injector injector, MockUserAgent userAgent, T
this.request = new HTTPRequest().with(r -> r.addLocales(Locale.US))
.with(r -> r.setPath(path));
this.port = port;
//Injections optionally HTTPRequestConsumer
injector.injectMembers(this);
}

public static HttpClient newHttpClient() {
Expand Down Expand Up @@ -847,7 +853,9 @@ HTTPResponseWrapper run() {
}

// Allow the caller to consume the HTTP request in order to mutate it and what not.
injector.getInstance(HTTPRequestConsumer.class).accept(request);
if (httpRequestConsumer != null) {
httpRequestConsumer.accept(request);
}

List<Locale> locales = request.getLocales();
String contentType = request.getContentType();
Expand Down Expand Up @@ -971,11 +979,10 @@ private void setRequestBodyParameter(String name, Object value) {
requestBodyParameters.put(name, values);
}

public static class HTTPRequestConsumer {
public interface HTTPRequestConsumer {
/**
* @param httpRequest the http request
*/
void accept(HTTPRequest httpRequest) {
}
void accept(HTTPRequest httpRequest);
}
}

0 comments on commit 9f81e22

Please sign in to comment.