Skip to content
Open
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
14 changes: 13 additions & 1 deletion form/src/main/java/feign/form/multipart/DelegateWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ protected void write(Output output, String key, Object value) throws EncodeExcep
delegate.encode(value, value.getClass(), fake);
val bytes = fake.body();
val string = new String(bytes, output.getCharset()).replaceAll("\n", "");
parameterWriter.write(output, key, string);
parameterWriter.writeWithContentType(output, key, string, contentType(fake));
}

private static String contentType(RequestTemplate template) {
val headers = template.headers().get("Content-Type");
if (headers != null) {
for (val header : headers) {
if (header != null && !header.isEmpty()) {
return header;
}
}
}
return null;
}
}
21 changes: 19 additions & 2 deletions form/src/main/java/feign/form/multipart/SingleParameterWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,31 @@ public boolean isApplicable(Object value) {

@Override
protected void write(Output output, String key, Object value) throws EncodeException {
writeWithContentType(output, key, value, null);
}

/**
* Writes a single parameter using the given content type.
*
* @param output output writer.
* @param key name for piece of data.
* @param value piece of data.
* @param contentType the content type of the part. May be {@code null}, in which case {@code
* text/plain} with the output charset is used.
* @throws EncodeException in case of write errors
*/
protected void writeWithContentType(Output output, String key, Object value, String contentType)
throws EncodeException {
val contentTypeHeader =
contentType != null ? contentType : "text/plain; charset=" + output.getCharset().name();
val string =
new StringBuilder()
.append("Content-Disposition: form-data; name=\"")
.append(key)
.append('"')
.append(CRLF)
.append("Content-Type: text/plain; charset=")
.append(output.getCharset().name())
.append("Content-Type: ")
.append(contentTypeHeader)
.append(CRLF)
.append(CRLF)
.append(value.toString())
Expand Down
57 changes: 57 additions & 0 deletions form/src/test/java/feign/form/multipart/DelegateWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* 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 feign.form.multipart;

import static org.assertj.core.api.Assertions.assertThat;

import feign.codec.Encoder;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;

class DelegateWriterTest {

private static final String BOUNDARY = "boundary";

private static final String KEY = "metadata";

@Test
void usesContentTypeFromDelegate() throws Exception {
Encoder delegate =
(object, bodyType, template) -> {
template.header("Content-Type", "application/json");
template.body("{\"hash\":\"somehash\"}");
};

assertThat(write(delegate))
.contains("Content-Type: application/json")
.doesNotContain("Content-Type: text/plain");
}

@Test
void fallsBackToTextPlainWhenDelegateSetsNoContentType() throws Exception {
Encoder delegate = (object, bodyType, template) -> template.body("plain");

assertThat(write(delegate)).contains("Content-Type: text/plain; charset=UTF-8");
}

private static String write(Encoder delegate) throws Exception {
DelegateWriter writer = new DelegateWriter(delegate);
try (Output output = new Output(StandardCharsets.UTF_8)) {
writer.write(output, BOUNDARY, KEY, new Object());
return new String(output.toByteArray(), StandardCharsets.UTF_8);
}
}
}