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
19 changes: 12 additions & 7 deletions elda-lda/src/main/java/com/epimorphics/lda/core/APIEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.epimorphics.lda.core;

import com.epimorphics.lda.bindings.Bindings;
import com.epimorphics.lda.exceptions.BadRequestException;
import com.epimorphics.lda.query.QueryParameter;
import com.epimorphics.lda.renderers.Renderer;
import com.epimorphics.lda.shortnames.CompleteContext;
Expand All @@ -26,6 +27,7 @@
import com.epimorphics.lda.support.NoteBoard;
import com.epimorphics.util.MediaType;
import com.epimorphics.util.URIUtils;
import jakarta.ws.rs.core.UriBuilderException;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import jakarta.ws.rs.core.UriBuilder;
Expand Down Expand Up @@ -97,13 +99,16 @@ public URI getURIwithFormat() {
* that do not affect the SPARQL select and view queries removed.
*/
public URI getURIplain() {
URI x = UriBuilder.fromUri(URIUtils.changeFormatSuffix(requestURI, formatNames, ""))
.replaceQueryParam(QueryParameter._FORMAT)
.replaceQueryParam(QueryParameter._METADATA)
.replaceQueryParam(QueryParameter._MARK)
.replaceQueryParam(QueryParameter.callback)
.build();
return x;
try {
return UriBuilder.fromUri(URIUtils.changeFormatSuffix(requestURI, formatNames, ""))
.replaceQueryParam(QueryParameter._FORMAT)
.replaceQueryParam(QueryParameter._METADATA)
.replaceQueryParam(QueryParameter._MARK)
.replaceQueryParam(QueryParameter.callback)
.build();
} catch (UriBuilderException ube) {
throw new BadRequestException("The requested query string was invalid: " + requestURI.toString());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.epimorphics.lda.core;

import com.epimorphics.lda.bindings.Bindings;
import com.epimorphics.lda.support.Controls;
import org.junit.Test;

import java.net.URI;

import static org.junit.Assert.*;

public class APIEndpointRequestTest {
private final Controls c = new Controls();
private final Bindings b = new Bindings();

private APIEndpoint.Request request(String uri) {
URI requestUri = URI.create(uri);
return new APIEndpoint.Request(c, requestUri, b);
}

@Test
public void getUriPlain_withUnescapedQuery_returnsUri() {
URI result = request("http://localhost:8080/api/test?_view=all&thing.label=foo+&+bar").getURIplain();
assertEquals("http://localhost:8080/api/test?_view=all&+bar=&thing.label=foo+", result.toString());
}

@Test
public void getUriPlain_withViewParamsOnly_returnsUri() {
URI result = request("http://localhost:8080/api/test?_view=all&label=foo").getURIplain();
assertEquals("http://localhost:8080/api/test?_view=all&label=foo", result.toString());
}

@Test
public void getUriPlain_withNonViewParams_returnsPlainUri() {
URI result = request("http://localhost:8080/api/test?_metadata=true&_format=ttl&callback=hi&_view=all&label=foo&_mark=true").getURIplain();
assertEquals("http://localhost:8080/api/test?_view=all&label=foo", result.toString());
}

@Test
public void getUriPlain_withoutParams_returnsUri() {
URI result = request("http://localhost:8080/api/test").getURIplain();
assertEquals("http://localhost:8080/api/test", result.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -138,7 +139,11 @@ public static MultiMap<String, String> parseQueryString(String queryString) {
for (int i = 0; i < pairs.length; i++) {
if (pairs[i].isEmpty()) break;
String[] pair = pairs[i].split("=");
result.add(pair[0], pair[1]);
if (pair.length > 1) {
result.add(pair[0], pair[1]);
} else {
result.add(pair[0], Collections.emptySet());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this add pair[0] if the query parameter is just a name with no value. I believe that is allowed to indicate a boolean true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to also have this branch covered by a test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is itself a test utility - I suggest it's outside the scope of what we would normally unit test.

}
return result;
}
Expand Down
Loading