Skip to content

Commit

Permalink
fix: update ProxyEndpointResolver to resolve endpoint using simple name
Browse files Browse the repository at this point in the history
To resolve endpoint, we needed to provide a string "my-endpoint:". The
colon was mandatory.
This change allows to search an endpoint with a simple name.

This will allow the Traffic Shadowing policy to use a simple endpoint
name in its configuration instead of using an EL:
"{#endpoints['myendpoint'}"
  • Loading branch information
jgiovaresco committed Jan 13, 2025
1 parent aa29b1f commit 5c0125b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,19 @@ private ProxyEndpoint selectUserDefinedEndpoint(String uri) {
} else {
int refSeparatorIdx = uri.indexOf(':');

String endpointName;
String uriPath;

if (refSeparatorIdx > 0) {
endpointName = uri.substring(0, refSeparatorIdx);
uriPath = uri.substring(refSeparatorIdx + 1);
} else {
endpointName = uri;
uriPath = "";
}

// Get the full reference
String sRef = uri.substring(0, refSeparatorIdx);
final Reference reference = referenceRegister.lookup(sRef);
final Reference reference = referenceRegister.lookup(endpointName);

// A null reference has been found (unknown reference ?), returning null to the caller
if (reference == null) {
Expand All @@ -122,7 +132,7 @@ private ProxyEndpoint selectUserDefinedEndpoint(String uri) {
return null;
}

return new UserDefinedProxyEndpoint(endpoint, getMergedTarget(endpoint.target(), uri.substring(refSeparatorIdx + 1)));
return new UserDefinedProxyEndpoint(endpoint, getMergedTarget(endpoint.target(), uriPath));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,40 @@ public void setUp() {
referenceRegister.add(new GroupReference(group));
}

@Test
public void shouldResolveUserDefinedEndpoint() {
var endpointName = "my-endpoint";
var expectedUri = "http://endpoint:8080/test";

Endpoint endpoint = mock(Endpoint.class);
when(endpoint.name()).thenReturn(endpointName);
when(endpoint.target()).thenReturn(expectedUri);
referenceRegister.add(new EndpointReference(endpoint));

ProxyEndpoint proxyEndpoint = resolver.resolve(endpointName);
assertThat(proxyEndpoint).isNotNull();

ProxyRequest proxyRequest = proxyEndpoint.createProxyRequest(mock((Request.class)));
assertThat(proxyRequest.uri()).isEqualTo(expectedUri);
}

@Test
public void shouldResolveUserDefinedEndpoint_withPath() {
var endpointName = "my-endpoint";
var endpointTarget = "http://endpoint:8080/test";

Endpoint endpoint = mock(Endpoint.class);
when(endpoint.name()).thenReturn(endpointName);
when(endpoint.target()).thenReturn(endpointTarget);
referenceRegister.add(new EndpointReference(endpoint));

ProxyEndpoint proxyEndpoint = resolver.resolve(endpointName + ":/echo");
assertThat(proxyEndpoint).isNotNull();

ProxyRequest proxyRequest = proxyEndpoint.createProxyRequest(mock((Request.class)));
assertThat(proxyRequest.uri()).isEqualTo(endpointTarget + "/echo");
}

@Test
// : is forbidden thanks to https://github.com/gravitee-io/issues/issues/1939
public void shouldResolveUserDefinedEndpoint_withPointsInName() {
Expand Down

0 comments on commit 5c0125b

Please sign in to comment.