-
Notifications
You must be signed in to change notification settings - Fork 414
/
RestrictUrisProcessorTest.java
95 lines (84 loc) · 4.29 KB
/
RestrictUrisProcessorTest.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
package org.mapfish.print.processor.http;
import com.google.common.base.Predicate;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mapfish.print.TestHttpClientFactory;
import org.mapfish.print.http.MfClientHttpRequestFactory;
import org.mapfish.print.processor.http.matcher.AcceptAllMatcher;
import org.mapfish.print.processor.http.matcher.AddressHostMatcher;
import org.mapfish.print.processor.http.matcher.LocalHostMatcher;
import org.mapfish.print.processor.http.matcher.URIMatcher;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.mock.http.client.MockClientHttpRequest;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class RestrictUrisProcessorTest {
static final TestHttpClientFactory requestFactory = new TestHttpClientFactory();
@BeforeClass
public static void setUp() throws Exception {
requestFactory.registerHandler(new Predicate<URI>() {
@Override
public boolean apply(@Nullable URI input) {
return true;
}
}, new TestHttpClientFactory.Handler() {
@Override
public MockClientHttpRequest handleRequest(URI uri, HttpMethod httpMethod) {
assertEquals("localhost", uri.getHost());
return null;
}
});
}
@Test
public void testCreateFactoryWrapperLegalRequest() throws Exception {
final RestrictUrisProcessor restrictUrisProcessor = createLocalhostOnly();
ClientHttpFactoryProcessorParam params = new ClientHttpFactoryProcessorParam();
final MfClientHttpRequestFactory factoryWrapper = restrictUrisProcessor.createFactoryWrapper(params, requestFactory);
factoryWrapper.createRequest(new URI("http://localhost:8080/geoserver/wms"), HttpMethod.GET);
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFactoryWrapperIllegalRequest() throws Exception {
final RestrictUrisProcessor restrictUrisProcessor = createLocalhostOnly();
ClientHttpFactoryProcessorParam params = new ClientHttpFactoryProcessorParam();
final ClientHttpRequestFactory factoryWrapper = restrictUrisProcessor.createFactoryWrapper(params, requestFactory);
factoryWrapper.createRequest(new URI("http://www.google.com/q"), HttpMethod.GET);
}
@Test
public void testRejectWithLegalRequest() throws Exception {
final RestrictUrisProcessor restrictUrisProcessor = createDenyInternal();
ClientHttpFactoryProcessorParam params = new ClientHttpFactoryProcessorParam();
final ClientHttpRequestFactory factoryWrapper = restrictUrisProcessor.createFactoryWrapper(params, requestFactory);
factoryWrapper.createRequest(new URI("http://localhost/q"), HttpMethod.GET);
}
@Test(expected = IllegalArgumentException.class)
public void testRejectWithIllegalRequest() throws Exception {
final RestrictUrisProcessor restrictUrisProcessor = createDenyInternal();
ClientHttpFactoryProcessorParam params = new ClientHttpFactoryProcessorParam();
final ClientHttpRequestFactory factoryWrapper = restrictUrisProcessor.createFactoryWrapper(params, requestFactory);
factoryWrapper.createRequest(new URI("http://192.168.12.23/q"), HttpMethod.GET);
}
private RestrictUrisProcessor createLocalhostOnly() {
final RestrictUrisProcessor processor = new RestrictUrisProcessor();
processor.setMatchers(Collections.singletonList(new LocalHostMatcher()));
return processor;
}
private RestrictUrisProcessor createDenyInternal() {
final RestrictUrisProcessor processor = new RestrictUrisProcessor();
List<URIMatcher> matchers = new ArrayList<URIMatcher>();
AddressHostMatcher addressHostMatcher = new AddressHostMatcher();
addressHostMatcher.setIp("192.168.12.0");
addressHostMatcher.setMask("255.255.255.0");
addressHostMatcher.setReject(true);
matchers.add(addressHostMatcher);
matchers.add(new AcceptAllMatcher());
processor.setMatchers(matchers);
return processor;
}
}