Skip to content

Commit

Permalink
refactor: create Pattern used to find URL in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanmaisse committed Jun 27, 2023
1 parent daaa13d commit c86d73b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
Expand Up @@ -58,10 +58,12 @@ public class URLRewritingPolicy {
private static final String GROUP_ATTRIBUTE = "group";
private static final String GROUP_NAME_ATTRIBUTE = "groupName";

private URLRewritingPolicyConfiguration configuration;
private final URLRewritingPolicyConfiguration configuration;
private final Pattern configuredURLPattern;

public URLRewritingPolicy(final URLRewritingPolicyConfiguration configuration) {
this.configuration = configuration;
this.configuredURLPattern = Pattern.compile(configuration.getFromRegex());
}

@OnResponse
Expand Down Expand Up @@ -127,10 +129,9 @@ private String rewrite(String value, ExecutionContext executionContext) {

if (value != null && !value.isEmpty()) {
// Compile pattern
Pattern pattern = Pattern.compile(configuration.getFromRegex());

// Apply regex capture / replacement
Matcher matcher = pattern.matcher(value);
Matcher matcher = this.configuredURLPattern.matcher(value);
int start = 0;

boolean result = matcher.find();
Expand All @@ -147,7 +148,7 @@ private String rewrite(String value, ExecutionContext executionContext) {
executionContext.getTemplateEngine().getTemplateContext().setVariable(GROUP_ATTRIBUTE, groups);

// Extract capture group by name
Set<String> extractedGroupNames = getNamedGroupCandidates(pattern.pattern());
Set<String> extractedGroupNames = getNamedGroupCandidates(this.configuredURLPattern.pattern());
Map<String, String> groupNames = extractedGroupNames
.stream()
.collect(Collectors.toMap(groupName -> groupName, matcher::group));
Expand Down
Expand Up @@ -55,63 +55,53 @@ public class URLRewritingPolicyTest {
@Mock
private ExecutionContext executionContext;

@Mock
private URLRewritingPolicyConfiguration configuration;

@Before
public void init() {
public void initPolicy(String fromRegex, String toReplacement, boolean rewriteResponseBody, boolean rewriteResponseHeaders) {
configuration = new URLRewritingPolicyConfiguration();
configuration.setFromRegex(fromRegex);
configuration.setToReplacement(toReplacement);
configuration.setRewriteResponseBody(rewriteResponseBody);
configuration.setRewriteResponseHeaders(rewriteResponseHeaders);
urlRewritingPolicy = new URLRewritingPolicy(configuration);
}

@Test
public void test_shouldNotRewriteHeaders() {
// Prepare
when(configuration.isRewriteResponseHeaders()).thenReturn(false);
initPolicy("", "", false, false);

// Execute policy
urlRewritingPolicy.onResponse(request, response, executionContext, policyChain);

// Check results
verify(response, never()).headers();
verify(policyChain).doNext(any(Request.class), any(Response.class));
}

@Test
public void test_rewriteHeaders() {
// Prepare
final HttpHeaders headers = HttpHeaders.create().set(HttpHeaderNames.LOCATION, "https://localgateway/mypath");
initPolicy("https?://[^\\/]*\\/((.*|\\/*))", "https://apis.gravitee.io/{#group[1]}", false, true);

final HttpHeaders headers = HttpHeaders.create().set(HttpHeaderNames.LOCATION, "https://localgateway/mypath");
when(response.headers()).thenReturn(headers);

when(configuration.isRewriteResponseHeaders()).thenReturn(true);
when(configuration.getFromRegex()).thenReturn("https?://[^\\/]*\\/((.*|\\/*))");
when(configuration.getToReplacement()).thenReturn("https://apis.gravitee.io/{#group[1]}");

// Prepare context
when(executionContext.getTemplateEngine()).thenReturn(TemplateEngine.templateEngine());

// Execute policy
urlRewritingPolicy.onResponse(request, response, executionContext, policyChain);

// Check results
Assert.assertEquals("https://apis.gravitee.io/mypath", response.headers().get(HttpHeaderNames.LOCATION));
verify(policyChain).doNext(any(Request.class), any(Response.class));
}

@Test
public void test_rewriteHeadersWithMultipleValues() {
// Prepare
initPolicy("Path=/test", "Path=/updated-path", false, true);

final HttpHeaders headers = HttpHeaders
.create()
.add(HttpHeaderNames.SET_COOKIE, "SID=ABAN12398123NJHJZEHDK123012039301U93274923U4KADNZKN; Path=/test")
.add(HttpHeaderNames.SET_COOKIE, "JSESSIONID=123456789; Path=/test");

when(response.headers()).thenReturn(headers);

when(configuration.isRewriteResponseHeaders()).thenReturn(true);
when(configuration.getFromRegex()).thenReturn("Path=/test");
when(configuration.getToReplacement()).thenReturn("Path=/updated-path");

when(executionContext.getTemplateEngine()).thenReturn(TemplateEngine.templateEngine());

urlRewritingPolicy.onResponse(request, response, executionContext, policyChain);
Expand All @@ -128,26 +118,20 @@ public void test_rewriteHeadersWithMultipleValues() {

@Test
public void test_rewriteResponse_disabled() {
// Prepare
when(configuration.isRewriteResponseBody()).thenReturn(false);
initPolicy("", "", false, false);

// Execute policy
ReadWriteStream stream = urlRewritingPolicy.onResponseContent(request, response, executionContext);

// Check results
Assert.assertNull(stream);
}

@Test
public void shouldRewriteEmptyBody() {
// Prepare
when(configuration.isRewriteResponseBody()).thenReturn(true);
initPolicy("", "", true, false);

// Execute policy
final ReadWriteStream stream = urlRewritingPolicy.onResponseContent(request, response, executionContext);
stream.end();

// Check results
Assert.assertNotNull(stream);
}
}

0 comments on commit c86d73b

Please sign in to comment.