Skip to content

Commit 39ae9d7

Browse files
author
Chris Wiechmann
committed
Make sure, duplicated Content-Type Headers are removed
1 parent 3234496 commit 39ae9d7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/com/axway/apim/openapi/validator/Utils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.ArrayList;
44
import java.util.Collection;
5+
import java.util.HashMap;
56
import java.util.Iterator;
7+
import java.util.Map;
68

79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
@@ -102,4 +104,21 @@ public static Collection<String> getHeaderValues(HeaderSet headers, String name)
102104
}
103105
return result;
104106
}
107+
108+
/**
109+
* @param headers that might contain a duplicated Content-Type header
110+
*/
111+
public static void removeDuplicateContentTypeHeader(HeaderSet headers) {
112+
HeaderEntry contentTypeHeader = headers.getHeaderEntry("Content-Type");
113+
if(contentTypeHeader != null && contentTypeHeader.size()>1) {
114+
traceMessage("WARN - Header: Multiple Content-Type headers found. Remove all but the first header.", TraceLevel.DEBUG);
115+
// Get the first header
116+
Header first = contentTypeHeader.get(0);
117+
// Clear all others
118+
contentTypeHeader.clear();
119+
// Add only one header
120+
contentTypeHeader.add(first);
121+
}
122+
return;
123+
}
105124
}

src/test/java/com/axway/apim/openapi/validator/UtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.testng.annotations.Test;
77
import org.testng.reporters.Files;
88

9+
import com.vordel.mime.HeaderSet;
10+
911
public class UtilsTest {
1012

1113
private static final String TEST_PACKAGE = "com/axway/apim/openapi/validator/";
@@ -23,4 +25,15 @@ public void testGetContentStartLongContent() throws IOException {
2325
String payloadStart = Utils.getContentStart(payload, 40, true);
2426
Assert.assertEquals(payloadStart, "[ { \"id\": 13973, \"category\": { ...(truncated)");
2527
}
28+
29+
@Test
30+
public void testRemoveDuplicateContentTypeHeader() throws IOException {
31+
HeaderSet headers = new HeaderSet();
32+
headers.addHeader("content-Type", "application/json");
33+
headers.addHeader("Content-type", "application/xml");
34+
Assert.assertEquals(headers.getHeadersSize("Content-Type"), 2);
35+
Utils.removeDuplicateContentTypeHeader(headers);
36+
Assert.assertEquals(headers.getHeadersSize("Content-Type"), 1);
37+
Assert.assertEquals(headers.getHeader("Content-Type"), "application/json");
38+
}
2639
}

0 commit comments

Comments
 (0)