aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc-security-filters/src/test/java/com/yahoo/jdisc/http/filter/security/cors/CorsPreflightRequestFilterTest.java
blob: 576b04e23b6a281f20e348543b1fbff2f6859296 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.filter.security.cors;

import com.yahoo.jdisc.HeaderFields;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.filter.DiscFilterRequest;
import com.yahoo.jdisc.http.filter.SecurityRequestFilter;
import com.yahoo.jdisc.http.filter.security.cors.CorsFilterConfig.Builder;
import com.yahoo.jdisc.http.filter.util.FilterTestUtils;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static com.yahoo.jdisc.http.HttpRequest.Method.OPTIONS;
import static com.yahoo.jdisc.http.filter.security.cors.CorsLogic.ACCESS_CONTROL_HEADERS;
import static com.yahoo.jdisc.http.filter.security.cors.CorsLogic.ALLOW_ORIGIN_HEADER;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;


/**
 * @author gjoranv
 * @author bjorncs
 */
public class CorsPreflightRequestFilterTest {

    @Test
    void any_options_request_yields_access_control_headers_in_response() {
        HeaderFields headers = doFilterRequest(newRequestFilter(), "http://any.origin");
        ACCESS_CONTROL_HEADERS.keySet().forEach(
                header -> assertFalse(headers.getFirst(header).isEmpty(), "Empty header: " + header));
    }

    @Test
    void allowed_request_origin_yields_allow_origin_header_in_response() {
        final String ALLOWED_ORIGIN = "http://allowed.origin";
        HeaderFields headers = doFilterRequest(newRequestFilter(ALLOWED_ORIGIN), ALLOWED_ORIGIN);
        assertEquals(ALLOWED_ORIGIN, headers.getFirst(ALLOW_ORIGIN_HEADER));
    }

    @Test
    void extended_request_origin_does_not_yield_allow_origin_header_in_response() {
        final String ALLOWED_ORIGIN = "https://allowed.origin";
        final String EXTENDED_ORIGIN = "https://allowed.origin.as.subdomain.com";
        HeaderFields headers = doFilterRequest(newRequestFilter(ALLOWED_ORIGIN), EXTENDED_ORIGIN);
        assertNull(headers.getFirst(ALLOW_ORIGIN_HEADER));
    }

    @Test
    void allowed_wildcard_origin_yields_origin_header_in_response() {
        final String ALLOWED_ORIGIN = "http://allowed.origin";
        HeaderFields headers = doFilterRequest(newRequestFilter("*"), ALLOWED_ORIGIN);
        assertEquals(ALLOWED_ORIGIN, headers.getFirst(ALLOW_ORIGIN_HEADER));
    }

    @Test
    void disallowed_request_origin_does_not_yield_allow_origin_header_in_response() {
        HeaderFields headers = doFilterRequest(newRequestFilter("http://allowed.origin"), "http://disallowed.origin");
        assertNull(headers.getFirst(ALLOW_ORIGIN_HEADER));
    }

    private static HeaderFields doFilterRequest(SecurityRequestFilter filter, String originUrl) {
        AccessControlResponseHandler responseHandler = new AccessControlResponseHandler();
        filter.filter(newOptionsRequest(originUrl), responseHandler);
        return responseHandler.response.headers();
    }

    private static DiscFilterRequest newOptionsRequest(String origin) {
        return FilterTestUtils.newRequestBuilder().withHeader("Origin", origin).withMethod(OPTIONS).build();
    }

    private static CorsPreflightRequestFilter newRequestFilter(String... allowedOriginUrls) {
        Builder builder = new Builder();
        Arrays.asList(allowedOriginUrls).forEach(builder::allowedUrls);
        return new CorsPreflightRequestFilter(new CorsFilterConfig(builder));
    }

    private static class AccessControlResponseHandler implements ResponseHandler {
        Response response;

        @Override
        public ContentChannel handleResponse(Response response) {
            this.response = response;
            return mock(ContentChannel.class);
        }
    }

}