Skip to content
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
Expand Up @@ -65,7 +65,7 @@ public static MessageReader parseStructuredOrBinaryMessage(
* CloudEvent family it indicates it's a structured
* representation that we can't interpret.
*/
if (ct.startsWith("application/cloudevents")) {
if (ct.regionMatches(true, 0, "application/cloudevents", 0, "application/cloudevents".length())) {
throw newUnknownEncodingException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.cloudevents.core.provider;

import java.util.HashMap;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -69,10 +70,18 @@ private EventFormatProvider() {
*/
public void registerFormat(EventFormat format) {
for (String k : format.deserializableContentTypes()) {
this.formats.put(k, format);
this.formats.put(normalizeContentType(k), format);
}
}

private static String normalizeContentType(String contentType) {
int i = contentType.indexOf(';');
if (i != -1) {
return contentType.substring(0, i).toLowerCase(Locale.ROOT) + contentType.substring(i);
}
return contentType.toLowerCase(Locale.ROOT);
}

/**
* Enumerate the supported content types.
*
Expand All @@ -96,7 +105,7 @@ public EventFormat resolveFormat(String contentType) {
if (i != -1) {
contentType = contentType.substring(0, i);
}
return this.formats.get(contentType);
return this.formats.get(contentType.toLowerCase(Locale.ROOT));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ private static Stream<Arguments> testParseStructuredOrBinaryMessage_BinaryMode()
private static Stream<Arguments> testBadContentTypes() {
return Stream.of(
Arguments.of("application/cloudevents"),
Arguments.of("application/cloudevents+morse")
Arguments.of("application/cloudevents+morse"),
Arguments.of("APPLICATION/CLOUDEVENTS"),
Arguments.of("Application/CloudEvents+Morse"),
Arguments.of("APPLICATION/CLOUDEVENTS+MORSE; charset=UTF-8")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

package io.cloudevents.core.provider;

import io.cloudevents.core.format.EventFormat;
import io.cloudevents.core.mock.CSVFormat;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

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

Expand All @@ -36,6 +39,37 @@ void resolveCSVWithParams() {
.isInstanceOf(CSVFormat.class);
}

@ParameterizedTest
@ValueSource(strings = {
"APPLICATION/CLOUDEVENTS+CSV",
"Application/CloudEvents+Csv",
"application/CLOUDEVENTS+csv; charset=UTF-8"
})
void resolveCSVWithCaseInsensitiveContentType(String contentType) {
assertThat(EventFormatProvider.getInstance().resolveFormat(contentType))
.isInstanceOf(CSVFormat.class);
}

@Test
void registerFormatWithMixedCaseContentType() {
EventFormatProvider provider = EventFormatProvider.getInstance();
EventFormat original = provider.resolveFormat(CSVFormat.INSTANCE.serializedContentType());
CSVFormat format = new CSVFormat() {
@Override
public String serializedContentType() {
return "Application/CloudEvents+CSV";
}
};

try {
provider.registerFormat(format);
assertThat(provider.resolveFormat("application/cloudevents+csv")).isSameAs(format);
assertThat(provider.resolveFormat("APPLICATION/CLOUDEVENTS+CSV")).isSameAs(format);
} finally {
provider.registerFormat(original);
}
}

@Test
void listTypes() {
assertThat(EventFormatProvider.getInstance().getContentTypes()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -91,6 +92,24 @@ public void testReadStructured(CloudEvent event) {
.isEqualTo(event);
}

@ParameterizedTest
@ValueSource(strings = {
"application/cloudevents+csv",
"APPLICATION/CLOUDEVENTS+CSV",
"Application/CloudEvents+Csv; charset=UTF-8"
})
void testReadStructuredWithCaseInsensitiveContentType(String contentType) {
Map<String, String> headers = new HashMap<>();
headers.put("content-type", contentType);
headers.put("ce-specversion", "1.0");

MessageReader message = HttpMessageFactory.createReader(
headers, CSVFormat.INSTANCE.serialize(V1_WITH_TEXT_DATA));

assertThat(message.getEncoding()).isEqualTo(Encoding.STRUCTURED);
assertThat(message.toEvent()).isEqualTo(V1_WITH_TEXT_DATA);
}

@ParameterizedTest
@MethodSource("binaryTestArguments")
public void testReadBinary(CloudEvent expectedEvent, Map<String,String> headers, byte[] body) {
Expand Down
Loading