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

Allows adding literal header values to RequestTemplate #2471

Merged
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
30 changes: 30 additions & 0 deletions core/src/main/java/feign/RequestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,36 @@ public RequestTemplate header(String name, Iterable<String> values) {
return appendHeader(name, values);
}

/**
* @see RequestTemplate#headerLiteral(String, Iterable)
*/
public RequestTemplate headerLiteral(String name, String... values) {
if (values == null) {
return headerLiteral(name, Collections.emptyList());
}

return headerLiteral(name, Arrays.asList(values));
}

/**
* Specify a Header, with the specified values. Values are treated as literals. Template
* expressions are not resolved.
*
* @param name of the header.
* @param values for this header.
* @return a RequestTemplate for chaining.
*/
public RequestTemplate headerLiteral(String name, Iterable<String> values) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("name is required.");
}
if (values == null) {
values = Collections.emptyList();
}

return appendHeader(name, values, true);
}

/**
* Clear on reader from {@link RequestTemplate}
*
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/java/feign/RequestTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ void resolveTemplateWithHeaderEmptyResult() {
assertThat(template).hasNoHeader("Encoded");
}

@Test
void templateWithEmptyJsonObjectLiteralHeader() {
String emptyJsonObject = "{}";
RequestTemplate template =
new RequestTemplate().method(HttpMethod.GET).headerLiteral("A-Header", emptyJsonObject);

template.resolve(new LinkedHashMap<>());
assertThat(template).hasHeaders(entry("A-Header", Collections.singletonList(emptyJsonObject)));
}

@Test
void templateWithTemplateExpressionLiteralHeader() {
String header = "{var}";
RequestTemplate template =
new RequestTemplate().method(HttpMethod.GET).headerLiteral("A-Header", header);

template = template.resolve(mapOf("var", "value"));
assertThat(template).hasHeaders(entry("A-Header", Collections.singletonList(header)));
}

@Test
void resolveTemplateWithMixedRequestLineParams() {
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)//
Expand Down