summaryrefslogtreecommitdiffstats
path: root/container-core
diff options
context:
space:
mode:
authorBjørn Christian Seime <bjorncs@verizonmedia.com>2021-04-23 11:04:42 +0200
committerBjørn Christian Seime <bjorncs@verizonmedia.com>2021-04-23 11:07:45 +0200
commit8149a140186e19093477007d4079f739716515c0 (patch)
tree5373d063491c332d13c24412265c80a7d364d8be /container-core
parentfa2bd6900dbec8c22e50dd468c625c3dc45b0fa5 (diff)
Add builder for test HttpRequest
Diffstat (limited to 'container-core')
-rw-r--r--container-core/abi-spec.json17
-rw-r--r--container-core/src/main/java/com/yahoo/container/jdisc/HttpRequestBuilder.java71
2 files changed, 88 insertions, 0 deletions
diff --git a/container-core/abi-spec.json b/container-core/abi-spec.json
index 43e0cab967e..2734f366c48 100644
--- a/container-core/abi-spec.json
+++ b/container-core/abi-spec.json
@@ -641,6 +641,23 @@
],
"fields": []
},
+ "com.yahoo.container.jdisc.HttpRequestBuilder": {
+ "superClass": "java.lang.Object",
+ "interfaces": [],
+ "attributes": [
+ "public"
+ ],
+ "methods": [
+ "public static com.yahoo.container.jdisc.HttpRequestBuilder create(com.yahoo.jdisc.http.HttpRequest$Method, java.lang.String)",
+ "public com.yahoo.container.jdisc.HttpRequestBuilder withQueryParameter(java.lang.String, java.lang.String)",
+ "public com.yahoo.container.jdisc.HttpRequestBuilder withHeader(java.lang.String, java.lang.String)",
+ "public com.yahoo.container.jdisc.HttpRequestBuilder withRequestContent(java.io.InputStream)",
+ "public com.yahoo.container.jdisc.HttpRequestBuilder withScheme(java.lang.String)",
+ "public com.yahoo.container.jdisc.HttpRequestBuilder withHostname(java.lang.String)",
+ "public com.yahoo.container.jdisc.HttpRequest build()"
+ ],
+ "fields": []
+ },
"com.yahoo.container.jdisc.HttpRequestHandler": {
"superClass": "java.lang.Object",
"interfaces": [
diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/HttpRequestBuilder.java b/container-core/src/main/java/com/yahoo/container/jdisc/HttpRequestBuilder.java
new file mode 100644
index 00000000000..3f70f4b75bb
--- /dev/null
+++ b/container-core/src/main/java/com/yahoo/container/jdisc/HttpRequestBuilder.java
@@ -0,0 +1,71 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.container.jdisc;
+
+import com.yahoo.jdisc.http.HttpRequest.Method;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * Builder for creating a {@link HttpRequest} to be used in test context
+ *
+ * @author bjorncs
+ */
+public class HttpRequestBuilder {
+ private final Method method;
+ private final String path;
+ private final Map<String, List<String>> queryParameters = new TreeMap<>();
+ private final Map<String, String> headers = new TreeMap<>();
+ private String scheme;
+ private String hostname;
+ private InputStream content;
+
+ private HttpRequestBuilder(Method method, String path) {
+ this.method = method;
+ this.path = path;
+ }
+
+ public static HttpRequestBuilder create(Method method, String path) { return new HttpRequestBuilder(method, path); }
+
+ public HttpRequestBuilder withQueryParameter(String name, String value) {
+ this.queryParameters.computeIfAbsent(name, ignored -> new ArrayList<>()).add(value);
+ return this;
+ }
+
+ public HttpRequestBuilder withHeader(String name, String value) { this.headers.put(name, value); return this; }
+
+ public HttpRequestBuilder withRequestContent(InputStream content) { this.content = content; return this; }
+
+ public HttpRequestBuilder withScheme(String scheme) { this.scheme = scheme; return this; }
+
+ public HttpRequestBuilder withHostname(String hostname) { this.hostname = hostname; return this; }
+
+ public HttpRequest build() {
+ String scheme = this.scheme != null ? this.scheme : "http";
+ String hostname = this.hostname != null ? this.hostname : "localhost";
+ StringBuilder uriBuilder = new StringBuilder(scheme).append("://").append(hostname).append(path);
+ if (queryParameters.size() > 0) {
+ uriBuilder.append('?');
+ queryParameters.forEach((name, values) -> {
+ for (String value : values) {
+ uriBuilder.append(name).append('=').append(value).append('&');
+ }
+ });
+ int lastIndex = uriBuilder.length() - 1;
+ if (uriBuilder.charAt(lastIndex) == '&') {
+ uriBuilder.setLength(lastIndex);
+ }
+ }
+ HttpRequest request;
+ if (content != null) {
+ request = HttpRequest.createTestRequest(uriBuilder.toString(), method, content);
+ } else {
+ request = HttpRequest.createTestRequest(uriBuilder.toString(), method);
+ }
+ headers.forEach((name, value) -> request.getJDiscRequest().headers().put(name, value));
+ return request;
+ }
+}