Permalink
Browse files

Fixes content negotiation

  • Loading branch information...
Tpt
Tpt committed Feb 6, 2019
1 parent b63eab2 commit 3dc4cf9b83d204df8c8f073c465473603653a59e
@@ -65,13 +65,18 @@ static MediaRange parse(String mediaRange) throws IllegalArgumentException {
}

boolean match(String mime) {
String[] parts = mime.split(",", 1);
String[] parts = mime.split("/", 2);
return type.equals("*") || (type.equals(parts[0])) && (subType.equals("*") || subType.equals(parts[1]));
}

@Override
public int compareTo(MediaRange mediaRange) {
return Float.compare(q, mediaRange.q);
}

@Override
public String toString() {
return type + '/' + subType + "; q=" + q;
}
}
}
@@ -0,0 +1,32 @@
package org.wikidata.history.web;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class ContentNegotiationTest {

@Test
public void testNegotiateAccept() {
testAcceptNegotiation(null, Optional.of("application/ld+json"));
testAcceptNegotiation("application/ld+json", Optional.of("application/ld+json"));
testAcceptNegotiation("application/*", Optional.of("application/ld+json"));
testAcceptNegotiation("*/*", Optional.of("application/ld+json"));
testAcceptNegotiation("application/json", Optional.of("application/json"));
testAcceptNegotiation("application/ld+json; charset=UTF-8", Optional.of("application/ld+json"));
testAcceptNegotiation("application/*; charset=UTF-8", Optional.of("application/ld+json"));
testAcceptNegotiation("*/*; charset=UTF-8", Optional.of("application/ld+json"));
testAcceptNegotiation("application/json; charset=UTF-8", Optional.of("application/json"));
testAcceptNegotiation("application/xml", Optional.empty());

}

private void testAcceptNegotiation(String header, Optional<String> expected) {
List<String> possibles = Arrays.asList("application/ld+json", "application/json");
Assert.assertEquals(expected, ContentNegotiation.negotiateAccept(header, possibles));
}
}

0 comments on commit 3dc4cf9

Please sign in to comment.