Skip to content

Commit

Permalink
Merge pull request #11 from camptocamp/fixing-mimetypes-in-links-depe…
Browse files Browse the repository at this point in the history
…nding-on-f-param-accept-header

ogc-api-features - fixing links depending on the requested format
  • Loading branch information
groldan authored Oct 31, 2023
2 parents f52b478 + 226b3a1 commit e3aaf97
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeType;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -167,6 +168,10 @@ private FeatureCollection addLinks(FeatureCollection fc, DataQuery dataQuery) {
Integer offset = extractOffset(request);
int limit = dataQuery.getLimit() == null ? 10 : dataQuery.getLimit();
Integer next = offset == null ? limit : offset + limit;
// f parameter preempts the "Accept" header
final String formatParam = request.getParameter("f");
builder.replaceQueryParam("f",
StringUtils.hasText(formatParam) ? formatParam : requestedFormat.getShortName());
UriComponents nextUri = builder.replaceQueryParam("offset", next.toString())
.replaceQueryParam("limit", limit).build();
fc.getLinks().add(1, link(nextUri.toString(), "next", mime, "Next page"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.camptocamp.opendata.ogc.features.http.codec;

import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class MimeTypesTest {

public @Test void testFindByShortName() {
Optional<MimeTypes> ret = MimeTypes.findByShortName("geojson");

assertTrue(ret.isPresent() && ret.get().equals(MimeTypes.GeoJSON));
}

public @Test void testFindByHeader() {
Optional<MimeTypes> ret = MimeTypes.find("application/json");

assertTrue(ret.isPresent() && ret.get().equals(MimeTypes.JSON));
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
package com.camptocamp.opendata.ogc.features.server.impl;

import com.camptocamp.opendata.ogc.features.app.OgcFeaturesApp;
import com.camptocamp.opendata.ogc.features.model.FeatureCollection;
import com.camptocamp.opendata.ogc.features.model.Link;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ActiveProfiles;

import com.camptocamp.opendata.ogc.features.app.OgcFeaturesApp;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(classes = OgcFeaturesApp.class)
@ActiveProfiles("sample-data")
public class CollectionsApiImplTest extends AbstractCollectionsApiImplIT {

@Test
public void testGetItemsLinks() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.addParameter("f", "geojson");
mockedReq.addHeader("Accept", "application/geo+json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);

List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=geojson"));
}

public @Test void testGetItemsLinksNoFParam() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.removeParameter("f");
mockedReq.addHeader("Accept", "application/json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);

List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=json"));
}

public @Test void testGetItemsLinksFParamAndDifferentHeaderParams() {
MockHttpServletRequest mockedReq = (MockHttpServletRequest) req.getNativeRequest();
mockedReq.addParameter("f", "ooxml");
mockedReq.addHeader("Accept", "application/json");

ResponseEntity<FeatureCollection> response = collectionsApi.getFeatures("locations", //
10, //
null, //
null, //
null);

List<Link> links = response.getBody().getLinks();
assertTrue(links.get(1).getHref().contains("f=ooxml"));
}

}

0 comments on commit e3aaf97

Please sign in to comment.