diff --git a/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java b/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java index cc834ab8..a8136a10 100644 --- a/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java +++ b/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java @@ -272,6 +272,10 @@ Optional getBestMatchingMediaType(Stream acceptHeaders) { } }); + if (tupleList.isEmpty()) { + return Optional.of(TEXT_PLAIN); + } + WTTuple bestMatchTuple = new WTTuple(-1, null); // Iterate over the list and find the best match diff --git a/implementation/src/test/java/io/smallrye/metrics/MediaHandlerTest.java b/implementation/src/test/java/io/smallrye/metrics/MediaHandlerTest.java new file mode 100644 index 00000000..5b738102 --- /dev/null +++ b/implementation/src/test/java/io/smallrye/metrics/MediaHandlerTest.java @@ -0,0 +1,156 @@ +/* + * Copyright 2018 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * 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 io.smallrye.metrics; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Optional; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author hrupp + */ +public class MediaHandlerTest { + + MetricsRequestHandler requestHandler; + + @Before + public void setUp() { + + requestHandler = new MetricsRequestHandler(); + } + + @Test + public void testNotSamePrio() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.1", + "text/plain;q=0.9")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testNotSamePrio2() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.1,text/plain;q=0.9")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testSamePrio() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.5", + "text/plain;q=0.5")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testSamePrio2() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("text/plain;q=0.5", + "application/json;q=0.5")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testNoMatch() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("image/png", + "image/jpeg ")); + assertThat(res.isPresent()).isFalse(); + } + + @Test + public void testBoth() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.1", + "text/plain;q=0.9")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo(("text/plain")); + } + + @Test + public void testBoth2() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.1,text/plain;q=0.9")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo(("text/plain")); + } + + @Test + public void testBoth3() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.8", + "text/plain;q=0.1")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("application/json"); + } + + @Test + public void testBoth4() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;q=0.8", + "text/plain;q=0.5", + "*/*;q=0.1")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("application/json"); + } + + @Test + public void testStarStarOnly() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("*/*")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testStarMix() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("*/*;q=0.1", + "image/png;q=1")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testStarMix2() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("image/png;q=1", + "*/*;q=0.1")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + + @Test + public void testWithMissingPrioritySingle() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("application/json;charset=UTF-8")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("application/json"); + } + + @Test + public void testWithMissingPriorityMulti() { + // default q=1 so json should be preferred + Optional res = requestHandler.getBestMatchingMediaType(Stream.of("text/plain;q=0.8;charset=UTF-8", + "application/json;charset=UTF-8")); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("application/json"); + } + + @Test + public void testDefaultMediaTypeWhenAcceptHeaderNotSet() { + Optional res = requestHandler.getBestMatchingMediaType(Stream.empty()); + assertThat(res.isPresent()).isTrue(); + assertThat(res.get()).isEqualTo("text/plain"); + } + +}