Skip to content

Commit 712eb48

Browse files
committed
add test case for the not-modified case
1 parent af87427 commit 712eb48

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,18 +1828,7 @@ private SortedSet<AcceptedMessage> getProjectMessages() {
18281828
* @see <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">HTTP Caching</a>
18291829
*/
18301830
public boolean isNotModified(HttpServletRequest request, HttpServletResponse response) {
1831-
String currentEtag = String.format("W/\"%s\"",
1832-
Objects.hash(
1833-
// last modified time as UTC timestamp in millis
1834-
getLastModified(),
1835-
// all project related messages which changes the view
1836-
getProjectMessages(),
1837-
// last timestamp value
1838-
getEnv().getDateForLastIndexRun() != null ? getEnv().getDateForLastIndexRun().getTime() : 0,
1839-
// OpenGrok version has changed since the last time
1840-
Info.getVersion()
1841-
)
1842-
);
1831+
String currentEtag = getEtag();
18431832

18441833
String headerEtag = request.getHeader(HttpHeaders.IF_NONE_MATCH);
18451834
if (headerEtag != null && headerEtag.equals(currentEtag)) {
@@ -1853,6 +1842,21 @@ public boolean isNotModified(HttpServletRequest request, HttpServletResponse res
18531842
return false;
18541843
}
18551844

1845+
@VisibleForTesting @NotNull String getEtag() {
1846+
return String.format("W/\"%s\"",
1847+
Objects.hash(
1848+
// last modified time as UTC timestamp in millis
1849+
getLastModified(),
1850+
// all project related messages which changes the view
1851+
getProjectMessages(),
1852+
// last timestamp value
1853+
getEnv().getDateForLastIndexRun() != null ? getEnv().getDateForLastIndexRun().getTime() : 0,
1854+
// OpenGrok version has changed since the last time
1855+
Info.getVersion()
1856+
)
1857+
);
1858+
}
1859+
18561860
/**
18571861
* @param root root path
18581862
* @param path path

opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.junit.jupiter.api.condition.OS;
5050
import org.junit.jupiter.params.ParameterizedTest;
5151
import org.junit.jupiter.params.provider.MethodSource;
52-
import org.mockito.Mockito;
5352
import org.opengrok.indexer.authorization.AuthControlFlag;
5453
import org.opengrok.indexer.authorization.AuthorizationFramework;
5554
import org.opengrok.indexer.authorization.AuthorizationPlugin;
@@ -73,6 +72,9 @@
7372
import static org.junit.jupiter.api.Assumptions.assumeTrue;
7473
import static org.mockito.ArgumentMatchers.eq;
7574
import static org.mockito.ArgumentMatchers.startsWith;
75+
import static org.mockito.Mockito.mock;
76+
import static org.mockito.Mockito.verify;
77+
import static org.mockito.Mockito.when;
7678
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL;
7779
import static org.opengrok.indexer.history.LatestRevisionUtil.getLatestRevision;
7880

@@ -654,7 +656,7 @@ public String getPathInfo() {
654656
}
655657

656658
@Test
657-
void testIsNotModified() {
659+
void testIsNotModifiedEtag() {
658660
HttpServletRequest req = new DummyHttpServletRequest() {
659661
@Override
660662
public String getHeader(String name) {
@@ -671,8 +673,20 @@ public String getPathInfo() {
671673
};
672674

673675
PageConfig cfg = PageConfig.get(req);
674-
HttpServletResponse resp = Mockito.mock(HttpServletResponse.class);
676+
HttpServletResponse resp = mock(HttpServletResponse.class);
675677
assertFalse(cfg.isNotModified(req, resp));
676-
Mockito.verify(resp).setHeader(eq(HttpHeaders.ETAG), startsWith("W/"));
678+
verify(resp).setHeader(eq(HttpHeaders.ETAG), startsWith("W/"));
679+
}
680+
681+
@Test
682+
void testIsNotModifiedNotModified() {
683+
DummyHttpServletRequest req = mock(DummyHttpServletRequest.class);
684+
when(req.getPathInfo()).thenReturn("/");
685+
PageConfig cfg = PageConfig.get(req);
686+
String etag = cfg.getEtag();
687+
when(req.getHeader(HttpHeaders.IF_NONE_MATCH)).thenReturn(etag);
688+
HttpServletResponse resp = mock(HttpServletResponse.class);
689+
assertTrue(cfg.isNotModified(req, resp));
690+
verify(resp).setStatus(eq(HttpServletResponse.SC_NOT_MODIFIED));
677691
}
678692
}

0 commit comments

Comments
 (0)