Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace tokens in URL b/c curly braces are illegal #171

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void intercept(HttpContentRequestImpl proxyRequest,
}
}

// curly braces are illegal URL characters
url = removeRemainingUrlTokens(url);

// update the URL in the content request
proxyRequest.setProxiedLocation(url);

Expand All @@ -94,6 +97,11 @@ public void intercept(HttpContentRequestImpl proxyRequest,

}

private String removeRemainingUrlTokens(String url) {
String s = url.replaceAll("/\\{[^\\}]*\\}/", "/");
return s.replaceAll("\\{[^\\}]*\\}", "");
}

@Override
public boolean validate(HttpContentRequestImpl proxyRequest,
PortletRequest portletRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void setUp() {
proxyRequest = new HttpContentRequestImpl();
proxyRequest.setParameters(parameters);
proxyRequest.setProxiedLocation("http://somewhere.com/rest/test/id");

}

@Test
Expand All @@ -74,19 +73,52 @@ public void testNoChange() {
public void testReplacePathElement() {
proxyRequest.setProxiedLocation("http://somewhere.com/rest/{test}/id");
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("http://somewhere.com/rest/somevalue/id", proxyRequest.getProxiedLocation());
}

@Test
public void testNonReplacePathElement() {
proxyRequest.setProxiedLocation("http://somewhere.com/rest/{unknown_attr}/id");
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("http://somewhere.com/rest/id", proxyRequest.getProxiedLocation());
}

@Test
public void testReplaceParam() {
proxyRequest.setProxiedLocation("http://somewhere.com/rest/id?param={test}");
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("http://somewhere.com/rest/id?param=somevalue", proxyRequest.getProxiedLocation());
}

@Test
public void testReplaceParamter() {
public void testNonReplaceParam() {
proxyRequest.setProxiedLocation("http://somewhere.com/rest/id?param={missing_attr}");
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("http://somewhere.com/rest/id?param=", proxyRequest.getProxiedLocation());
}

@Test
public void testReplaceParameter() {
IFormField formField = new FormFieldImpl("param", new String[]{"val1", "{test}"});
parameters.put("param", formField);
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("val1", proxyRequest.getParameters().get("param").getValues()[0]);
assertEquals("somevalue", proxyRequest.getParameters().get("param").getValues()[1]);
}

@Test
public void testNonReplaceParameter() {
IFormField formField = new FormFieldImpl("param", new String[]{"val1", "{missing_attr}"});
parameters.put("param", formField);
preprocessor.intercept(proxyRequest, portletRequest);

assertEquals("val1", proxyRequest.getParameters().get("param").getValues()[0]);
assertEquals("{missing_attr}", proxyRequest.getParameters().get("param").getValues()[1]);
}

}