Skip to content

Commit

Permalink
Merge pull request #196 from GetFeedback/fix/bug-on-http-path-placeho…
Browse files Browse the repository at this point in the history
…lder

Fix path placeholder overriding itself
  • Loading branch information
Stefano Guerrini authored Sep 2, 2022
2 parents 6607ee0 + 4d11118 commit 159c786
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ public abstract class AbstractHttpCall implements HttpCall, Conditional {

@NotBlank private transient String path;

// This path could change due to the placeholder
private String actualPath;

protected AbstractHttpCall(String name, Map<String, ?> config) {
this.name = name;
if (config.containsKey("path")) {
this.path = config.get("path").toString();
this.actualPath = path;
}

if (config.containsKey("method")) {
Expand Down Expand Up @@ -79,7 +83,7 @@ public String getName() {
public Either<Throwable, RecordAction> call(KaHPPRecord record) {
ResponseHandler responseHandler = this.responseHandler;

return Try.of(() -> apiClient.sendRequest(method, path, record.getValue().toString()))
return Try.of(() -> apiClient.sendRequest(method, actualPath, record.getValue().toString()))
.mapTry(responseHandler::handle)
.toEither();
}
Expand All @@ -94,7 +98,7 @@ public Either<Throwable, RecordAction> call(KaHPPRecord record, JacksonRuntime j
LOGGER.warn("No value found inside the record for this placeholder {}", m.group(0));
return Either.left(new RuntimeException("Placeholder value not found"));
}
path = path.replace(m.group(0), value);
actualPath = path.replace(m.group(0), value);
}
return call(record);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import dev.vox.platform.kahpp.configuration.RecordAction;
import dev.vox.platform.kahpp.configuration.TransformRecord;
import dev.vox.platform.kahpp.configuration.http.HttpCall;
Expand Down Expand Up @@ -35,6 +36,7 @@ class SimpleHttpCallTest {
private ApiClient apiClient;

private final JacksonRuntime jacksonRuntime = new JacksonRuntime();
private static final JsonMapper JSON_MAPPER = new JsonMapper();

@BeforeAll
static void setupMockServer() {
Expand Down Expand Up @@ -170,6 +172,20 @@ void shouldReplacePathWithValue() throws IOException {
assertTrue(afterCall.isRight());
assertThat(afterCall.get()).isExactlyInstanceOf(TransformRecord.class);
assertThat(((TransformRecord) afterCall.get()).getDataSource().toString()).isEqualTo("{}");

JsonNode extraKey = JSON_MAPPER.createObjectNode().put("extra", "record");
JsonNode extraValue =
JSON_MAPPER
.createObjectNode()
.set("payload", JSON_MAPPER.createObjectNode().put("id", "extraCall"));
KaHPPMockServer.mockHttpResponse(
HTTP_CALL_PATH + "/extraCall", extraValue.toString(), 200, "{}");

Either<Throwable, RecordAction> extraCall =
httpCall.call(KaHPPRecord.build(extraKey, extraValue, 1584352842124L), jacksonRuntime);
assertTrue(extraCall.isRight());
assertThat(extraCall.get()).isExactlyInstanceOf(TransformRecord.class);
assertThat(((TransformRecord) extraCall.get()).getDataSource().toString()).isEqualTo("{}");
}

@Test
Expand Down

0 comments on commit 159c786

Please sign in to comment.