Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

Fixed the unit tests for the content negotiation. #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private HypermediaControls(String absoluteIRI, Configuration config) {
* (<code>conf:webBase + conf:webResourcePrefix</code>). Otherwise, it
* will be its full absolute IRI with special characters escaped to make
* it safe in constructing paths.
* @return
* @return the path
*/
private String getPubbyPath() {
if (isHosted) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/fuberlin/wiwiss/pubby/IRIRewriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public Map<Property, Integer> rewrite(Map<Property, Integer> original) {

/**
* Rewrites an IRI.
* @param Any absolute IRI
* @param absoluteIRI absolute IRI
* @return The rewritten form of the IRI
*/
public abstract String rewrite(String absoluteIRI);

/**
* Rewrites an IRI. For any valid IRI,
*
* @param Any absolute IRI
* @param absoluteIRI absolute IRI
* @return The rewritten form of the IRI
*/
public abstract String unrewrite(String absoluteIRI);
Expand Down Expand Up @@ -165,7 +165,7 @@ public Map<Property, Integer> rewrite(Map<Property, Integer> original) {
*
* @param originalNamespace The namespace to be replaced
* @param rewrittenNamespace The replacement namespace
* @return
* @return the rewriter
*/
public static IRIRewriter createNamespaceBased(
final String originalNamespace, final String rewrittenNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ public void addUserAgentOverride(Pattern userAgentString,
userAgentString, originalAcceptHeader, newAcceptHeader));
}

/**
* Get best match for accept and null header. Best match is LAST match.
*/
public MediaRangeSpec getBestMatch(String accept) {
return getBestMatch(accept, null);
}

/**
* Get best match for accept or user agent header. Best match is LAST match.
*/
public MediaRangeSpec getBestMatch(String accept, String userAgent) {
if (userAgent == null) {
userAgent = "";
Expand Down Expand Up @@ -166,4 +172,4 @@ String getReplacement() {
return replacement;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
public class PubbyNegotiator {
private final static ContentTypeNegotiator pubbyNegotiator;
private final static ContentTypeNegotiator dataNegotiator;

static {
pubbyNegotiator = new ContentTypeNegotiator();
pubbyNegotiator.setDefaultAccept("text/html");

// MSIE (7.0) sends either */*, or */* with a list of other random types,
// but always without q values, so it doesn't provide any basis for
// actual negotiation. We will simply send HTML to MSIE, no matter what.
pubbyNegotiator.addUserAgentOverride(Pattern.compile("MSIE"), null, "text/html");


// Send Turtle to clients that indicate they accept everything.
// This is specifically so that cURL sees Turtle.
//
Expand All @@ -23,6 +18,15 @@ public class PubbyNegotiator {
// are requested, not for normal web page requests. --RC
pubbyNegotiator.addUserAgentOverride(null, "*/*", "text/turtle");

// MSIE (7.0) sends either */*, or */* with a list of other random types,
// but always without q values, so it doesn't provide any basis for
// actual negotiation. We will simply send HTML to MSIE, no matter what.
pubbyNegotiator.addUserAgentOverride(Pattern.compile("MSIE"), null, "text/html");

// Some versions of Safari send a broken "*/*" Accept header.
// We must override this to send HTML.
pubbyNegotiator.addUserAgentOverride(Pattern.compile("Safari/"), "*/*", "text/html");

pubbyNegotiator.addVariant("text/html;q=0.81")
.addAliasMediaType("application/xhtml+xml;q=0.81");
pubbyNegotiator.addVariant("application/rdf+xml")
Expand All @@ -48,11 +52,11 @@ public class PubbyNegotiator {
.addAliasMediaType("text/turtle;q=0.5");
dataNegotiator.addVariant("text/plain;q=0.2");
}

public static ContentTypeNegotiator getPubbyNegotiator() {
return pubbyNegotiator;
}

public static ContentTypeNegotiator getDataNegotiator() {
return dataNegotiator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

public class PubbyNegotiatorTest extends TestCase {
private ContentTypeNegotiator negotiator;

public void setUp() {
negotiator = PubbyNegotiator.getPubbyNegotiator();
}

public void testAcceptRDFXML() {
assertEquals("application/rdf+xml",
negotiator.getBestMatch("application/rdf+xml").getMediaType());
}

public void testAcceptHTML() {
assertEquals("text/html",
negotiator.getBestMatch("text/html").getMediaType());
}

public void testAcceptXHTMLGetsHTML() {
assertEquals("text/html",
negotiator.getBestMatch("application/xhtml+xml").getMediaType());
}

public void testGetTurtle() {
assertEquals("application/x-turtle",
negotiator.getBestMatch("application/x-turtle").getMediaType());
Expand All @@ -32,7 +32,7 @@ public void testGetTurtle() {
assertEquals("application/x-turtle",
negotiator.getBestMatch("text/turtle").getMediaType());
}

public void testGetN3() {
assertEquals("text/rdf+n3;charset=utf-8",
negotiator.getBestMatch("text/rdf+n3").getMediaType());
Expand All @@ -41,31 +41,31 @@ public void testGetN3() {
assertEquals("text/rdf+n3;charset=utf-8",
negotiator.getBestMatch("application/n3").getMediaType());
}

public void testGetNTriples() {
assertEquals("text/plain",
negotiator.getBestMatch("text/plain").getMediaType());
}

public void testBrowsersGetHTML() {
// Safari and Mozilla have text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5
// We want them to see HTML
assertEquals("text/html",
negotiator.getBestMatch("text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5").getMediaType());
}

public void testAcceptXMLGetsRDFXML() {
assertEquals("application/rdf+xml",
negotiator.getBestMatch("application/xml").getMediaType());
assertEquals("application/rdf+xml",
negotiator.getBestMatch("text/xml").getMediaType());
}

public void testNoAcceptGetsHTML() {
assertEquals("text/html",
negotiator.getBestMatch(null).getMediaType());
}

public void testSafariGetsHTML() {
// Some versions of Safari send a broken "*/*" Accept header.
// We must override this to send HTML.
Expand All @@ -75,11 +75,30 @@ public void testSafariGetsHTML() {
"AppleWebKit/522.11.1 (KHTML, like Gecko) " +
"Version/3.0.3 Safari/522.12.1").getMediaType());
}


public void testPlainMSIE7GetsHTML() {
// MSIE (7.0) sends either */*, or */* with a list of other random types,
// but always without q values, so it doesn't provide any basis for
// actual negotiation. We will simply send HTML to MSIE, no matter what.
assertEquals("text/html",
negotiator.getBestMatch("*/*",
"Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)").getMediaType());
}

public void testCompatibleMSIE7GetsHTML() {
// MSIE (7.0) sends either */*, or */* with a list of other random types,
// but always without q values, so it doesn't provide any basis for
// actual negotiation. We will simply send HTML to MSIE, no matter what.
assertEquals("text/html",
negotiator.getBestMatch("*/*",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; " +
".NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;" +
"Media Center PC 6.0; .NET4.0C; .NET4.0E)").getMediaType());
}
public void testAcceptEverythingGetsHTML() {
assertEquals("text/html", negotiator.getBestMatch("*/*").getMediaType());
assertEquals("application/x-turtle", negotiator.getBestMatch("*/*").getMediaType());
}

public void testFirefox3GetsHTML() {
// Firefox 3.0.1, OS X
assertEquals("text/html",
Expand All @@ -88,7 +107,7 @@ public void testFirefox3GetsHTML() {
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) " +
"Gecko/2008070206 Firefox/3.0.1").getMediaType());
}

public void testTabulatorGetsRDF() {
// Tabulator 0.8.5 on Firefox 3.0.1, OS X
assertEquals("application/rdf+xml",
Expand All @@ -99,14 +118,14 @@ public void testTabulatorGetsRDF() {
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) " +
"Gecko/2008070206 Firefox/3.0.1").getMediaType());
}

public void testDataURIDefaultsToN3ForFirefox() {
assertEquals("text/rdf+n3;charset=utf-8",
PubbyNegotiator.getDataNegotiator().getBestMatch(
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
).getMediaType());
}

public void testDataURIDefaultsToN3ForSafari() {
assertEquals("text/rdf+n3;charset=utf-8",
PubbyNegotiator.getDataNegotiator().getBestMatch(
Expand Down