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

Http using RuntimeHook #2392

Merged
merged 1 commit into from
Sep 6, 2023
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
5 changes: 5 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public Http header(String name, String value) {
return this;
}

public Http hook(RuntimeHook hook) {
builder.hook(hook);
return this;
}

public Response method(String method, Object body) {
if (body != null) {
builder.body(body instanceof Json ? ((Json) body).value() : body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -593,9 +596,8 @@ private void httpInvokeOnce() {
}
long startTime = System.currentTimeMillis();
httpRequest.setStartTime(startTime); // this may be fine-adjusted by actual http client
if (hooks != null) {
hooks.forEach(h -> h.beforeHttpCall(httpRequest, runtime));
}
Collection<RuntimeHook> allHooks = getRuntimeHooks();
allHooks.forEach(h -> h.beforeHttpCall(httpRequest, runtime));
try {
response = requestBuilder.client.invoke(httpRequest);
} catch (Exception e) {
Expand All @@ -619,9 +621,7 @@ private void httpInvokeOnce() {
final long endTime = httpRequest.getEndTime();
final long responseTime = endTime - startTime;
response.setResponseTime(responseTime);
if (hooks != null) {
hooks.forEach(h -> h.afterHttpCall(httpRequest, response, runtime));
}
allHooks.forEach(h -> h.afterHttpCall(httpRequest, response, runtime));
byte[] bytes = response.getBody();
Object body;
String responseType;
Expand Down Expand Up @@ -664,6 +664,12 @@ private void httpInvokeOnce() {
}
}

private List<RuntimeHook> getRuntimeHooks() {
return Stream.concat(hooks.stream(), Stream.of(requestBuilder.hook()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private void httpInvokeWithRetries() {
int maxRetries = config.getRetryCount();
int sleep = config.getRetryInterval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.intuit.karate.Json;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.RuntimeHook;
import com.intuit.karate.StringUtils;
import com.intuit.karate.graal.JsArray;
import com.intuit.karate.graal.JsValue;
Expand Down Expand Up @@ -103,7 +104,7 @@ public class HttpRequestBuilder implements ProxyObject {
private Object body;
private Set<Cookie> cookies;
private String retryUntil;

private RuntimeHook hook;
public final HttpClient client;

public HttpRequestBuilder(HttpClient client) {
Expand Down Expand Up @@ -519,6 +520,15 @@ public HttpRequestBuilder multiPart(Map<String, Object> map) {
return this;
}

public HttpRequestBuilder hook(RuntimeHook hook) {
this.hook = hook;
return this;
}

public RuntimeHook hook() {
return hook;
}

//==========================================================================
//
private final Methods.FunVar PATH_FUNCTION = args -> {
Expand Down
39 changes: 39 additions & 0 deletions karate-core/src/test/java/com/intuit/karate/HttpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.intuit.karate;

import com.intuit.karate.core.ScenarioRuntime;
import com.intuit.karate.http.HttpRequest;
import com.intuit.karate.http.Response;
import org.junit.jupiter.api.Test;

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

class HttpTest {

static class TestRuntimeHook implements RuntimeHook {
@Override
public void beforeHttpCall(HttpRequest request, ScenarioRuntime sr) {
String url = request.getUrl();
request.setUrl(url.substring(0, url.length() - 1) + "2");
}

@Override
public void afterHttpCall(HttpRequest request, Response response, ScenarioRuntime sr) {
response.setBody(response.json().set("username", "Karate").toString());
}
}

@Test
void testInvokeWithoutHook() {
Response response = Http.to("https://jsonplaceholder.typicode.com/users/1").header("Accept", "application/json").get();
assertEquals("1", response.json().get("id").toString());
assertEquals("Bret", response.json().get("username").toString());
}

@Test
void testInvokeWithHook() {
Response response = Http.to("https://jsonplaceholder.typicode.com/users/1").header("Accept", "application/xml").hook(new TestRuntimeHook()).get();
assertEquals("2", response.json().get("id").toString());
assertEquals("Karate", response.json().get("username").toString());
}

}