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

Add a basic builder for request message #1056

Merged
merged 1 commit into from
May 12, 2021
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
Original file line number Diff line number Diff line change
@@ -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:
* <pre>
* {@code new HttpRequestBuilder(context).withDefaults();}
*</pre>
*
* For overrides :
* <pre>
* {@code new HttpRequestBuilder(context).withHeaders(httpHeaders).withQueryParams(requestParams).build();}
* </pre>
* @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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down