com.intuit.karate.http.HttpRequestBuilder Maven / Gradle / Ivy
The newest version!
/*
* The MIT License
*
* Copyright 2022 Karate Labs Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.http;
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;
import com.intuit.karate.graal.Methods;
import io.netty.handler.codec.http.cookie.ClientCookieEncoder;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.http.client.utils.URIBuilder;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class HttpRequestBuilder implements ProxyObject {
private static final Logger logger = LoggerFactory.getLogger(HttpRequestBuilder.class);
private static final String URL = "url";
private static final String METHOD = "method";
private static final String PATH = "path";
private static final String PARAM = "param";
private static final String PARAMS = "params";
private static final String HEADER = "header";
private static final String HEADERS = "headers";
private static final String BODY = "body";
private static final String INVOKE = "invoke";
private static final String GET = "get";
private static final String POST = "post";
private static final String PUT = "put";
private static final String DELETE = "delete";
private static final String PATCH = "patch";
private static final String HEAD = "head";
private static final String CONNECT = "connect";
private static final String OPTIONS = "options";
private static final String TRACE = "trace";
private static final String MULTI_PART = "multiPart";
private static final String[] KEYS = new String[]{
URL, METHOD, PATH, PARAM, PARAMS, HEADER, HEADERS, BODY, INVOKE,
GET, POST, PUT, DELETE, PATCH, HEAD, CONNECT, OPTIONS, TRACE, MULTI_PART
};
private static final Set KEY_SET = new HashSet<>(Arrays.asList(KEYS));
private static final JsArray KEY_ARRAY = new JsArray(KEYS);
private String url;
private String method;
private String topic;
private String key;
private String schema;
private List paths;
private Map> params;
private Map> headers;
private MultiPartBuilder multiPart;
private Object body;
private Set cookies;
private String retryUntil;
private RuntimeHook hook;
public final HttpClient client;
public HttpRequestBuilder(HttpClient client) {
this.client = client;
}
public HttpRequestBuilder reset() {
// url will be retained
method = null;
paths = null;
params = null;
headers = null;
multiPart = null;
body = null;
cookies = null;
retryUntil = null;
return this;
}
public HttpRequestBuilder copy(HttpClient newClient) {
HttpRequestBuilder hrb = new HttpRequestBuilder(newClient == null ? client : newClient);
hrb.url = url;
hrb.method = method;
hrb.paths = paths;
hrb.params = params;
hrb.headers = headers;
hrb.multiPart = multiPart;
hrb.body = body;
hrb.cookies = cookies;
hrb.retryUntil = retryUntil;
return hrb;
}
public Response invoke(String method) {
this.method = method;
return invoke();
}
public Response invoke(String method, Object body) {
this.method = method;
this.body = body;
return invoke();
}
public HttpRequest build() {
buildInternal();
HttpRequest request = new HttpRequest();
request.setMethod(method);
request.setUrl(getUri());
if (multiPart != null) {
request.setBodyForDisplay(multiPart.getBodyForDisplay());
}
if (body != null) {
request.setBody(JsonUtils.toBytes(body));
}
request.setHeaders(headers);
return request;
}
private void buildInternal() {
if (url == null) {
url = client.getConfig().getUrl();
if (url == null) {
throw new RuntimeException("incomplete http request, 'url' not set");
}
}
if (method == null) {
if (multiPart != null && multiPart.isMultipart()) {
method = "POST";
} else {
method = "GET";
}
}
method = method.toUpperCase();
if ("GET".equals(method) && multiPart != null) {
Map parts = multiPart.getFormFields();
if (parts != null) {
parts.forEach((k, v) -> param(k, (String) v));
}
multiPart = null;
}
if (multiPart != null) {
if (body == null) { // this is not-null only for a re-try, don't rebuild multi-part
body = multiPart.build();
String userContentType = getHeader(HttpConstants.HDR_CONTENT_TYPE);
if (userContentType != null) {
String boundary = multiPart.getBoundary();
if (boundary != null) {
contentType(userContentType + "; boundary=" + boundary);
}
} else {
contentType(multiPart.getContentTypeHeader());
}
}
}
if (cookies != null && !cookies.isEmpty()) {
List cookieValues = new ArrayList<>(cookies.size());
for (Cookie c : cookies) {
String cookieValue = ClientCookieEncoder.LAX.encode(c);
cookieValues.add(cookieValue);
}
header(HttpConstants.HDR_COOKIE, StringUtils.join(cookieValues, "; "));
}
if (body != null) {
if (multiPart == null) {
String contentType = getContentType();
if (contentType == null) {
ResourceType rt = ResourceType.fromObject(body);
if (rt != null) {
contentType = rt.contentType;
}
}
Charset charset = contentType == null ? null : HttpUtils.parseContentTypeCharset(contentType);
if (charset == null) {
// client can be null when not in karate scenario, and mock clients can have nulls
charset = client == null ? null : client.getConfig() == null ? null : client.getConfig().getCharset();
if (charset != null) {
// edge case, support setting content type to an empty string
contentType = StringUtils.trimToNull(contentType);
if (contentType != null) {
contentType = contentType + "; charset=" + charset;
}
}
}
contentType(contentType);
}
}
}
public Response invoke() {
return client.invoke(build());
}
public boolean isRetry() {
return retryUntil != null;
}
public String getRetryUntil() {
return retryUntil;
}
public void setRetryUntil(String retryUntil) {
this.retryUntil = retryUntil;
}
public HttpRequestBuilder url(String value) {
url = value;
return this;
}
public HttpRequestBuilder method(String method) {
this.method = method;
return this;
}
public HttpRequestBuilder paths(String... paths) {
for (String path : paths) {
path(path);
}
return this;
}
public HttpRequestBuilder path(String path) {
if (path == null) {
return this;
}
if (paths == null) {
paths = new ArrayList<>();
}
paths.add(path);
return this;
}
public List getPaths() {
return paths;
}
public void setSchema(String schema) {
this.schema = schema;
}
public String getSchema() {
return schema;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getBody() {
return body;
}
public Map getHeaders() {
if (headers == null) {
return new LinkedHashMap(0);
}
Map map = new LinkedHashMap(headers.size());
headers.forEach((k, v) -> {
if (v != null && !v.isEmpty()) {
Object value = v.get(0);
if (value != null) {
map.put(k, value.toString());
}
}
});
return map;
}
public String getUri() {
try {
URIBuilder builder;
if (url == null) {
builder = new URIBuilder();
} else {
builder = new URIBuilder(url);
}
if (params != null) {
params.forEach((key, values) -> values.forEach(value -> builder.addParameter(key, value)));
}
if (paths != null) {
List segments = new ArrayList();
for (String item : builder.getPathSegments()) {
if (!item.isEmpty()) {
segments.add(item);
}
}
Iterator pathIterator = paths.iterator();
while (pathIterator.hasNext()) {
String item = pathIterator.next();
if (!pathIterator.hasNext() && "/".equals(item)) { // preserve trailing slash
segments.add("");
} else {
for (String s : StringUtils.split(item, '/', true)) {
segments.add(s);
}
}
}
builder.setPathSegments(segments);
}
URI uri = builder.build();
return uri.toASCIIString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public HttpRequestBuilder body(Object body) {
this.body = body;
return this;
}
public HttpRequestBuilder bodyJson(String json) {
this.body = Json.of(json).value();
return this;
}
public List getHeaderValues(String name) {
return StringUtils.getIgnoreKeyCase(headers, name); // TODO optimize
}
public String getHeader(String name) {
List list = getHeaderValues(name);
if (list == null || list.isEmpty()) {
return null;
} else {
return list.get(0);
}
}
public String getContentType() {
return getHeader(HttpConstants.HDR_CONTENT_TYPE);
}
public HttpRequestBuilder removeHeader(String name) {
if (headers != null) {
StringUtils.removeIgnoreKeyCase(headers, name);
}
return this;
}
public HttpRequestBuilder header(String name, String... values) {
return header(name, Arrays.asList(values));
}
public HttpRequestBuilder header(String name, List values) {
if (headers == null) {
headers = new LinkedHashMap<>();
}
for (String key : headers.keySet()) {
if (key.equalsIgnoreCase(name)) {
name = key;
break;
}
}
headers.put(name, values);
return this;
}
public HttpRequestBuilder header(String name, String value) {
return header(name, Collections.singletonList(value));
}
public HttpRequestBuilder headers(Map map) {
map.forEach((k, v) -> {
if (!k.startsWith(":")) { // strip (armeria) special headers
if (v instanceof List) {
header(k, (List) v);
} else if (v != null) {
header(k, v.toString());
}
}
});
return this;
}
public HttpRequestBuilder headers(Value value) {
JsValue jv = new JsValue(value);
if (jv.isObject()) {
headers(jv.getAsMap());
} else {
logger.warn("unexpected headers() argument: {}", value);
}
return this;
}
public HttpRequestBuilder contentType(String contentType) {
if (contentType != null) {
header(HttpConstants.HDR_CONTENT_TYPE, contentType);
}
return this;
}
public List getParam(String name) {
if (params == null || name == null) {
return null;
}
return params.get(name);
}
public HttpRequestBuilder param(String name, String... values) {
return param(name, Arrays.asList(values));
}
public HttpRequestBuilder param(String name, List values) {
if (params == null) {
params = new HashMap<>();
}
List notNullValues = values.stream().filter(v -> v != null).collect(Collectors.toList());
if (!notNullValues.isEmpty()) {
params.put(name, notNullValues);
}
return this;
}
public HttpRequestBuilder params(Map> params) {
this.params = params;
return this;
}
public HttpRequestBuilder cookies(Collection