/
AssetsContextHandlerTest.java
116 lines (101 loc) · 4.93 KB
/
AssetsContextHandlerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright 2023 Thoughtworks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thoughtworks.go.server;
import com.thoughtworks.go.util.ReflectionUtil;
import com.thoughtworks.go.util.SystemEnvironment;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class AssetsContextHandlerTest {
private AssetsContextHandler handler;
@Mock
private SystemEnvironment systemEnvironment;
@Mock
private WebAppContext webAppContext;
@BeforeEach
public void setUp() throws Exception {
when(systemEnvironment.getWebappContextPath()).thenReturn("/go");
when(webAppContext.getInitParameter("rails.root")).thenReturn("/rails.root");
when(webAppContext.getWebInf()).thenReturn(Resource.newResource("WEB-INF"));
handler = new AssetsContextHandler(systemEnvironment);
handler.init(webAppContext);
}
@Test
public void shouldSetHeadersAndBaseDirectory() {
assertThat(handler.getContextPath(), is("/go/assets"));
assertThat(((HandlerWrapper) handler.getHandler()).getHandler() instanceof AssetsContextHandler.AssetsHandler, is(true));
AssetsContextHandler.AssetsHandler assetsHandler = (AssetsContextHandler.AssetsHandler) ((HandlerWrapper) handler.getHandler()).getHandler();
ResourceHandler resourceHandler = (ResourceHandler) ReflectionUtil.getField(assetsHandler, "resourceHandler");
assertThat(resourceHandler.getCacheControl(), is("max-age=31536000,public"));
assertThat(resourceHandler.getResourceBase(), isSameFileAs(new File("WEB-INF/rails.root/public/assets").toPath().toAbsolutePath().toUri().toString()));
}
@Test
public void shouldPassOverHandlingToResourceHandler() throws Exception {
String target = "/go/assets/junk";
Request request = mock(Request.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Request baseRequest = mock(Request.class);
AssetsContextHandler.AssetsHandler resourceHandler = mock(AssetsContextHandler.AssetsHandler.class);
handler.setHandler(resourceHandler);
handler.getHandler().handle(target, baseRequest, request, response);
verify(resourceHandler).handle(target, baseRequest, request, response);
}
@Test
public void shouldNotHandleForRails4DevelopmentMode() throws IOException, ServletException {
when(systemEnvironment.useCompressedJs()).thenReturn(false);
String target = "/go/assets/junk";
Request request = mock(Request.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Request baseRequest = mock(Request.class);
ResourceHandler resourceHandler = mock(ResourceHandler.class);
ReflectionUtil.setField(((HandlerWrapper) handler.getHandler()).getHandler(), "resourceHandler", resourceHandler);
handler.getHandler().handle(target, baseRequest, request, response);
verify(resourceHandler, never()).handle(any(String.class), any(Request.class), any(HttpServletRequest.class), any(HttpServletResponse.class));
}
private Matcher<? super String> isSameFileAs(final String expected) {
return new BaseMatcher<>() {
@Override
public boolean matches(Object o) {
String actualFile = (String) o;
return OS.WINDOWS.equals(OS.current()) ? expected.equalsIgnoreCase(actualFile) : expected.equals(actualFile);
}
@Override
public void describeTo(Description description) {
description.appendText(" " + expected);
}
};
}
}