mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
make http ops verify status 2.. by default
This commit is contained in:
parent
0a84f346b9
commit
837501f8b9
@ -1,13 +1,14 @@
|
||||
package io.nosqlbench.activitytype.cmds;
|
||||
|
||||
import java.net.http.HttpRequest;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class HttpOp {
|
||||
public final String ok_status;
|
||||
public final String ok_body;
|
||||
public final Pattern ok_status;
|
||||
public final Pattern ok_body;
|
||||
public final HttpRequest request;
|
||||
|
||||
public HttpOp(HttpRequest request, String ok_status, String ok_body) {
|
||||
public HttpOp(HttpRequest request, Pattern ok_status, Pattern ok_body) {
|
||||
this.request = request;
|
||||
this.ok_status = ok_status;
|
||||
this.ok_body = ok_body;
|
||||
|
@ -12,21 +12,25 @@ import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.LongFunction;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ReadyHttpOp implements LongFunction<HttpOp> {
|
||||
|
||||
private final CommandTemplate propertyTemplate;
|
||||
public static final String DEFAULT_OK_BODY = ".+?";
|
||||
public static final String DEFAULT_OK_STATUS = "2..";
|
||||
|
||||
// only populated if there is no value which is an actual bindings template
|
||||
private final HttpOp cachedOp;
|
||||
|
||||
public ReadyHttpOp(OpTemplate stmtDef) {
|
||||
propertyTemplate = new CommandTemplate(stmtDef,
|
||||
List.of(
|
||||
HttpFormatParser::parseUrl,
|
||||
HttpFormatParser::parseInline
|
||||
)
|
||||
List.of(
|
||||
HttpFormatParser::parseUrl,
|
||||
HttpFormatParser::parseInline
|
||||
)
|
||||
);
|
||||
|
||||
sanityCheckUri();
|
||||
@ -83,7 +87,7 @@ public class ReadyHttpOp implements LongFunction<HttpOp> {
|
||||
if (cmd.containsKey("version")) {
|
||||
String versionName = cmd.remove("version")
|
||||
.replaceAll("/1.1", "_1_1")
|
||||
.replaceAll("/2.0", "_2");
|
||||
.replaceAll("/2.0", "_2");
|
||||
HttpClient.Version version = HttpClient.Version.valueOf(versionName);
|
||||
builder.version(version);
|
||||
}
|
||||
@ -93,8 +97,8 @@ public class ReadyHttpOp implements LongFunction<HttpOp> {
|
||||
builder.uri(uri);
|
||||
}
|
||||
|
||||
String ok_status = cmd.remove("ok-status");
|
||||
String ok_body = cmd.remove("ok-body");
|
||||
Pattern ok_status = Pattern.compile(Optional.ofNullable(cmd.remove("ok-status")).orElse(DEFAULT_OK_STATUS));
|
||||
Pattern ok_body = Optional.ofNullable(cmd.remove("ok-body")).map(Pattern::compile).orElse(null);
|
||||
|
||||
String timeoutStr = cmd.remove("timeout");
|
||||
if (timeoutStr != null) {
|
||||
|
@ -90,12 +90,12 @@ public class HttpAction implements SyncAction {
|
||||
try {
|
||||
response = responseFuture.get(httpActivity.getTimeoutMillis(), TimeUnit.MILLISECONDS);
|
||||
if (httpOp.ok_status!=null) {
|
||||
if (!String.valueOf(response.statusCode()).matches(httpOp.ok_status)) {
|
||||
throw new InvalidStatusCodeException(cycleValue,httpOp.ok_status,response.statusCode());
|
||||
if (!httpOp.ok_status.matcher(String.valueOf(response.statusCode())).matches()) {
|
||||
throw new InvalidStatusCodeException(cycleValue, httpOp.ok_status, response.statusCode());
|
||||
}
|
||||
}
|
||||
if (httpOp.ok_body!=null) {
|
||||
if (!response.body().matches(httpOp.ok_body)) {
|
||||
if (!httpOp.ok_body.matcher(response.body()).matches()) {
|
||||
throw new InvalidResponseBodyException(cycleValue, httpOp.ok_body, response.body());
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package io.nosqlbench.activitytype.http;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class InvalidResponseBodyException extends RuntimeException {
|
||||
private final long cycleValue;
|
||||
private final String ok_body;
|
||||
private final Pattern ok_body;
|
||||
private final String body;
|
||||
|
||||
public InvalidResponseBodyException(long cycleValue, String ok_body, String body) {
|
||||
public InvalidResponseBodyException(long cycleValue, Pattern ok_body, String body) {
|
||||
this.cycleValue = cycleValue;
|
||||
this.ok_body = ok_body;
|
||||
this.body = body;
|
||||
|
@ -1,11 +1,13 @@
|
||||
package io.nosqlbench.activitytype.http;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class InvalidStatusCodeException extends RuntimeException {
|
||||
private final long cycleValue;
|
||||
private final String ok_status;
|
||||
private final Pattern ok_status;
|
||||
private final int statusCode;
|
||||
|
||||
public InvalidStatusCodeException(long cycleValue, String ok_status, int statusCode) {
|
||||
public InvalidStatusCodeException(long cycleValue, Pattern ok_status, int statusCode) {
|
||||
this.cycleValue = cycleValue;
|
||||
this.ok_status = ok_status;
|
||||
this.statusCode = statusCode;
|
||||
|
Loading…
Reference in New Issue
Block a user