Merge pull request #717 from aehrc/feature/ifModifiedSince_bugfix_master

fix bugs with inverted test for If-Modified-Since (impl and tests)
This commit is contained in:
James Agnew 2017-10-26 18:31:19 -04:00 committed by GitHub
commit 0fec0485e8
3 changed files with 44 additions and 8 deletions

View File

@ -161,7 +161,7 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding {
ifNoneMatch = ParameterUtil.parseETagValue(ifNoneMatch); ifNoneMatch = ParameterUtil.parseETagValue(ifNoneMatch);
if (responseResource.getIdElement() != null && responseResource.getIdElement().hasVersionIdPart()) { if (responseResource.getIdElement() != null && responseResource.getIdElement().hasVersionIdPart()) {
if (responseResource.getIdElement().getVersionIdPart().equals(ifNoneMatch)) { if (responseResource.getIdElement().getVersionIdPart().equals(ifNoneMatch)) {
ourLog.debug("Returning HTTP 301 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch); ourLog.debug("Returning HTTP 304 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch);
throw new NotModifiedException("Not Modified"); throw new NotModifiedException("Not Modified");
} }
} }
@ -182,8 +182,8 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding {
lastModified = responseResource.getMeta().getLastUpdated(); lastModified = responseResource.getMeta().getLastUpdated();
} }
if (lastModified != null && lastModified.getTime() > ifModifiedSinceDate.getTime()) { if (lastModified != null && lastModified.getTime() <= ifModifiedSinceDate.getTime()) {
ourLog.debug("Returning HTTP 301 because If-Modified-Since does not match"); ourLog.debug("Returning HTTP 304 because If-Modified-Since does not match");
throw new NotModifiedException("Not Modified"); throw new NotModifiedException("Not Modified");
} }
} }

View File

@ -57,20 +57,38 @@ public class ReadDstu2Test {
CloseableHttpResponse status; CloseableHttpResponse status;
HttpGet httpGet; HttpGet httpGet;
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it has changed before the later time of 2012-01-01T13:00:00Z
// so we expect a 304
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T13:00:00Z").getValue())); httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T13:00:00Z").getValue()));
status = ourClient.execute(httpGet); status = ourClient.execute(httpGet);
try { try {
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(304, status.getStatusLine().getStatusCode());
} finally { } finally {
IOUtils.closeQuietly(status); IOUtils.closeQuietly(status);
} }
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it has changed at the same time of 2012-01-01T12:12:12Z
// so we expect a 304
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T12:12:12Z").getValue()));
status = ourClient.execute(httpGet);
try {
assertEquals(304, status.getStatusLine().getStatusCode());
} finally {
IOUtils.closeQuietly(status);
}
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it has changed after the earlier time of 2012-01-01T10:00:00Z
// so we expect a 200
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T10:00:00Z").getValue())); httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T10:00:00Z").getValue()));
status = ourClient.execute(httpGet); status = ourClient.execute(httpGet);
try { try {
assertEquals(304, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
} finally { } finally {
IOUtils.closeQuietly(status); IOUtils.closeQuietly(status);
} }

View File

@ -69,20 +69,38 @@ public class ReadDstu3Test {
CloseableHttpResponse status; CloseableHttpResponse status;
HttpGet httpGet; HttpGet httpGet;
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it hasn't changed after the later time of 2012-01-01T13:00:00Z
// so we expect a 304 (Not Modified)
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T13:00:00Z").getValue())); httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T13:00:00Z").getValue()));
status = ourClient.execute(httpGet); status = ourClient.execute(httpGet);
try { try {
assertEquals(200, status.getStatusLine().getStatusCode()); assertEquals(304, status.getStatusLine().getStatusCode());
} finally { } finally {
IOUtils.closeQuietly(status); IOUtils.closeQuietly(status);
} }
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it hasn't changed after the same time of 2012-01-01T12:12:12Z
// so we expect a 304 (Not Modified)
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T12:12:12Z").getValue()));
status = ourClient.execute(httpGet);
try {
assertEquals(304, status.getStatusLine().getStatusCode());
} finally {
IOUtils.closeQuietly(status);
}
// Fixture was last modified at 2012-01-01T12:12:12Z
// thus it has changed after the earlier time of 2012-01-01T10:00:00Z
// so we expect a 200
httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2");
httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T10:00:00Z").getValue())); httpGet.addHeader(Constants.HEADER_IF_MODIFIED_SINCE, DateUtils.formatDate(new InstantDt("2012-01-01T10:00:00Z").getValue()));
status = ourClient.execute(httpGet); status = ourClient.execute(httpGet);
try { try {
assertEquals(304, status.getStatusLine().getStatusCode()); assertEquals(200, status.getStatusLine().getStatusCode());
} finally { } finally {
IOUtils.closeQuietly(status); IOUtils.closeQuietly(status);
} }