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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.filters.cors;
import com.yahoo.jdisc.http.Cookie;
import com.yahoo.jdisc.http.filter.DiscFilterResponse;
import com.yahoo.jdisc.http.filter.RequestView;
import com.yahoo.jdisc.http.filter.SecurityResponseFilter;
import com.yahoo.jdisc.http.filters.cors.CorsSecurityFilterConfig.Builder;
import com.yahoo.jdisc.http.servlet.ServletOrJdiscHttpResponse;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static com.yahoo.jdisc.http.filters.cors.CorsLogic.ACCESS_CONTROL_HEADERS;
import static com.yahoo.jdisc.http.filters.cors.CorsLogic.ALLOW_ORIGIN_HEADER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author gjoranv
* @author bjorncs
*/
public class CorsSecurityResponseFilterTest {
@Test
public void any_request_yields_access_control_headers_in_response() {
Map<String, String> headers = doFilterRequest(newResponseFilter(), "http://any.origin");
ACCESS_CONTROL_HEADERS.keySet().forEach(
header -> assertFalse("Empty header: " + header, headers.get(header).isEmpty()));
}
@Test
public void allowed_request_origin_yields_allow_origin_header_in_response() {
final String ALLOWED_ORIGIN = "http://allowed.origin";
Map<String, String> headers = doFilterRequest(newResponseFilter(ALLOWED_ORIGIN), ALLOWED_ORIGIN);
assertEquals(ALLOWED_ORIGIN, headers.get(ALLOW_ORIGIN_HEADER));
}
@Test
public void disallowed_request_origin_does_not_yield_allow_origin_header_in_response() {
Map<String, String> headers = doFilterRequest(newResponseFilter("http://allowed.origin"), "http://disallowed.origin");
assertNull(headers.get(ALLOW_ORIGIN_HEADER));
}
@Test
public void any_request_origin_yields_allow_origin_header_in_response_when_wildcard_is_allowed() {
Map<String, String> headers = doFilterRequest(newResponseFilter("*"), "http://any.origin");
assertEquals("*", headers.get(ALLOW_ORIGIN_HEADER));
}
private static Map<String, String> doFilterRequest(SecurityResponseFilter filter, String originUrl) {
TestResponse response = new TestResponse();
filter.filter(response, newRequestView(originUrl));
return Collections.unmodifiableMap(response.headers);
}
private static CorsSecurityResponseFilter newResponseFilter(String... allowedOriginUrls) {
Builder builder = new Builder();
Arrays.asList(allowedOriginUrls).forEach(builder::allowedUrls);
return new CorsSecurityResponseFilter(new CorsSecurityFilterConfig(builder));
}
private static RequestView newRequestView(String originUrl) {
RequestView request = mock(RequestView.class);
when(request.getFirstHeader("Origin")).thenReturn(Optional.of(originUrl));
return request;
}
private static class TestResponse extends DiscFilterResponse {
Map<String, String> headers = new HashMap<>();
TestResponse() {
super(mock(ServletOrJdiscHttpResponse.class));
}
@Override
public void setHeader(String name, String value) {
headers.put(name, value);
}
@Override
public String getHeader(String name) {
return headers.get(name);
}
@Override
public void removeHeaders(String s) { throw new UnsupportedOperationException(); }
@Override
public void setHeaders(String s, String s1) { throw new UnsupportedOperationException(); }
@Override
public void setHeaders(String s, List<String> list) { throw new UnsupportedOperationException(); }
@Override
public void addHeader(String s, String s1) { throw new UnsupportedOperationException(); }
@Override
public void setCookies(List<Cookie> list) { throw new UnsupportedOperationException(); }
@Override
public void setStatus(int i) { throw new UnsupportedOperationException(); }
}
}
|