Skip to content

Commit

Permalink
New test for header field access from spel
Browse files Browse the repository at this point in the history
  • Loading branch information
predic8 committed Aug 18, 2023
1 parent f0f9360 commit c3f43a8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
61 changes: 60 additions & 1 deletion core/src/main/java/com/predic8/membrane/core/http/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* The headers of an HTTP message.
*/
public class Header {
public class Header implements Map<String,String> {

private static final Logger log = LoggerFactory.getLogger(Header.class.getName());

Expand Down Expand Up @@ -431,10 +431,69 @@ public static long parseKeepAliveHeader(String keepAliveHeaderValue, String para
return Long.parseLong(m.group(1));
}

@Override
public int size() {
return fields.size();
}

@Override
public boolean isEmpty() {
return fields.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return false;
}

@Override
public boolean containsValue(Object value) {
return false;
}

@Override
public String get(Object key) {
return null;
}

@org.jetbrains.annotations.Nullable
@Override
public String put(String key, String value) {
return null;
}

@Override
public String remove(Object key) {
return null;
}

@Override
public void putAll(@org.jetbrains.annotations.NotNull Map<? extends String, ? extends String> m) {

}

public void clear() {
fields.clear();
}

@org.jetbrains.annotations.NotNull
@Override
public Set<String> keySet() {
return null;
}

@org.jetbrains.annotations.NotNull
@Override
public Collection<String> values() {
return null;
}

@org.jetbrains.annotations.NotNull
@Override
public Set<Entry<String, String>> entrySet() {
return null;
}

public void setNoCacheResponseHeaders() {
setValue(EXPIRES, "Tue, 03 Jul 2001 06:00:00 GMT");
setValue(CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.predic8.membrane.core.lang.spel.*;
import org.slf4j.*;
import org.springframework.expression.*;
import org.springframework.expression.spel.*;
import org.springframework.expression.spel.standard.*;

import java.time.*;
Expand Down Expand Up @@ -92,12 +93,19 @@ public RateLimitInterceptor(Duration requestLimitDuration, int requestLimit) {

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
if (!strategy.isRequestLimitReached(getKey(exc)))
return CONTINUE;

try {
if (!strategy.isRequestLimitReached(getKey(exc)))
return CONTINUE;
} catch (SpelEvaluationException e) {
log.error("Cannot evaluate keyExpression '{}' cause is {}",keyExpression,e.getMessage());
exc.setResponse(createProblemDetails(500, "/internal-error", "Internal Server Error"));
return RETURN;
}

Map<String,Object> details = new HashMap<>();
details.put("message","The quota of the ratelimiter is exceeded. Try again in %s seconds.".formatted(strategy.getLimitReset(exc.getRemoteAddrIp())));
exc.setResponse(createProblemDetails(429, "/ratelimiter/exceeded", "Rate Limit is Exceeded", details));
exc.setResponse(createProblemDetails(429, "/ratelimiter/exceeded", "Rate limit is exceeded", details));
setHeaderRateLimitFieldsOnResponse(exc);

return RETURN;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.predic8.membrane.core.lang.spel;

import com.predic8.membrane.core.exchange.*;
import com.predic8.membrane.core.http.*;
import org.apache.http.client.methods.*;
import org.junit.jupiter.api.*;
import org.springframework.expression.*;
import org.springframework.expression.spel.standard.*;

import java.net.*;

import static org.junit.jupiter.api.Assertions.*;

class ExchangeEvaluationContextTest {

Exchange exc;

@BeforeEach
void setup() throws URISyntaxException {
exc = new Request.Builder().header("Authentication","foo").buildExchange();


}

@Test
void getMethod() {

Expression expression = new SpelExpressionParser().parseExpression("headers.Authentication");

expression.getValue(new ExchangeEvaluationContext(exc, exc.getRequest()).getStandardEvaluationContext(), String.class);
}
}
4 changes: 2 additions & 2 deletions distribution/examples/rateLimiter/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<router>

<serviceProxy port="2000">
<api port="2000">
<rateLimiter requestLimit="3" requestLimitDuration="PT30S"/>
<target host="www.google.de" port="80" />
</serviceProxy>
</api>

</router>

Expand Down

0 comments on commit c3f43a8

Please sign in to comment.