From 4666cde51b6a8684fed4477d10f1b2231fe4cca3 Mon Sep 17 00:00:00 2001 From: argha-c <007.argha@gmail.com> Date: Tue, 11 May 2021 17:52:52 -0700 Subject: [PATCH] Add a basic builder for request message --- .../zuul/message/util/HttpRequestBuilder.java | 116 ++++++++++++++++++ .../com/netflix/zuul/context/DebugTest.java | 8 +- 2 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 zuul-core/src/main/java/com/netflix/zuul/message/util/HttpRequestBuilder.java diff --git a/zuul-core/src/main/java/com/netflix/zuul/message/util/HttpRequestBuilder.java b/zuul-core/src/main/java/com/netflix/zuul/message/util/HttpRequestBuilder.java new file mode 100644 index 0000000000..94f3a055a9 --- /dev/null +++ b/zuul-core/src/main/java/com/netflix/zuul/message/util/HttpRequestBuilder.java @@ -0,0 +1,116 @@ +/* + * Copyright 2021 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.zuul.message.util; + +import com.netflix.zuul.context.SessionContext; +import com.netflix.zuul.message.Headers; +import com.netflix.zuul.message.http.HttpQueryParams; +import com.netflix.zuul.message.http.HttpRequestMessage; +import com.netflix.zuul.message.http.HttpRequestMessageImpl; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpVersion; +import java.util.Objects; + +/** + * Builder for a zuul http request. *exclusively* for use in unit tests. + * + * For default values initialized in the constructor: + *
+ * {@code new HttpRequestBuilder(context).withDefaults();}
+ *
+ * + * For overrides : + *
+ * {@code new HttpRequestBuilder(context).withHeaders(httpHeaders).withQueryParams(requestParams).build();}
+ * 
+ * @author Argha C + * @since 5/11/21 + */ +public final class HttpRequestBuilder { + private SessionContext sessionContext; + private String protocol; + private String method; + private String path; + private HttpQueryParams queryParams; + private Headers headers; + private String clientIp; + private String scheme; + private int port; + private String serverName; + private boolean isBuilt; + + public HttpRequestBuilder(SessionContext context) { + sessionContext = Objects.requireNonNull(context); + protocol = HttpVersion.HTTP_1_1.text(); + method = "get"; + path = "/"; + queryParams = new HttpQueryParams(); + headers = new Headers(); + clientIp = "::1"; + scheme = "https"; + port = 443; + isBuilt = false; + } + + /** + * Builds a request with basic defaults + * + * @return `HttpRequestMessage` + */ + public HttpRequestMessage withDefaults() { + return build(); + } + + public HttpRequestBuilder withHost(String hostName) { + serverName = Objects.requireNonNull(hostName); + return this; + } + + public HttpRequestBuilder withHeaders(Headers requestHeaders) { + headers = Objects.requireNonNull(requestHeaders); + return this; + } + + public HttpRequestBuilder withQueryParams(HttpQueryParams requestParams) { + this.queryParams = Objects.requireNonNull(requestParams); + return this; + } + + public HttpRequestBuilder withMethod(HttpMethod httpMethod) { + method = Objects.requireNonNull(httpMethod).name(); + return this; + } + + public HttpRequestBuilder withUri(String uri) { + path = Objects.requireNonNull(uri); + return this; + } + + /** + * Used to build a request with overriden values + * + * @return `HttpRequestMessage` + */ + public HttpRequestMessage build() { + if (isBuilt) { + throw new IllegalStateException("Builder must only be invoked once!"); + } + isBuilt = true; + return new HttpRequestMessageImpl(sessionContext, protocol, method, path, queryParams, headers, clientIp, scheme, port, + serverName); + } +} diff --git a/zuul-core/src/test/java/com/netflix/zuul/context/DebugTest.java b/zuul-core/src/test/java/com/netflix/zuul/context/DebugTest.java index b3131fd140..3d2179ccd7 100644 --- a/zuul-core/src/test/java/com/netflix/zuul/context/DebugTest.java +++ b/zuul-core/src/test/java/com/netflix/zuul/context/DebugTest.java @@ -34,6 +34,8 @@ import com.netflix.zuul.message.http.HttpRequestMessageImpl; import com.netflix.zuul.message.http.HttpResponseMessage; import com.netflix.zuul.message.http.HttpResponseMessageImpl; +import com.netflix.zuul.message.util.HttpRequestBuilder; +import io.netty.handler.codec.http.HttpMethod; import java.util.List; import org.junit.Before; import org.junit.Test; @@ -59,8 +61,10 @@ public void setup() { params = new HttpQueryParams(); params.add("k1", "v1"); - request = new HttpRequestMessageImpl(ctx, "HTTP/1.1", "post", "/some/where", - params, headers, "9.9.9.9", "https", 80, "localhost"); + request = new HttpRequestBuilder(ctx).withMethod(HttpMethod.POST) + .withUri("/some/where") + .withHeaders(headers) + .withQueryParams(params).build(); request.setBodyAsText("some text"); request.storeInboundRequest();