1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>. 23 */ 24 package org.opengrok.web.api.v1.controller; 25 26 import jakarta.ws.rs.core.Response; 27 import org.glassfish.jersey.servlet.ServletContainer; 28 import org.glassfish.jersey.test.DeploymentContext; 29 import org.glassfish.jersey.test.ServletDeploymentContext; 30 import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; 31 import org.glassfish.jersey.test.spi.TestContainerException; 32 import org.glassfish.jersey.test.spi.TestContainerFactory; 33 import org.junit.jupiter.api.AfterAll; 34 import org.junit.jupiter.api.BeforeAll; 35 import org.junit.jupiter.api.Test; 36 import org.opengrok.indexer.configuration.RuntimeEnvironment; 37 import org.opengrok.indexer.util.TestRepository; 38 import org.opengrok.web.api.v1.RestApp; 39 40 import java.util.Collections; 41 42 import static org.junit.jupiter.api.Assertions.assertEquals; 43 import static org.opengrok.web.api.v1.filter.CorsFilter.ALLOW_CORS_HEADER; 44 import static org.opengrok.web.api.v1.filter.CorsFilter.CORS_REQUEST_HEADER; 45 46 public class SearchControllerTest extends OGKJerseyTest { 47 48 private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance(); 49 50 private static TestRepository repository; 51 52 @Override configureDeployment()53 protected DeploymentContext configureDeployment() { 54 return ServletDeploymentContext.forServlet(new ServletContainer(new RestApp())).build(); 55 } 56 57 @Override getTestContainerFactory()58 protected TestContainerFactory getTestContainerFactory() throws TestContainerException { 59 return new GrizzlyWebTestContainerFactory(); 60 } 61 62 @BeforeAll setUpClass()63 public static void setUpClass() throws Exception { 64 System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); // necessary to test CORS from controllers 65 repository = new TestRepository(); 66 67 repository.create(SearchControllerTest.class.getClassLoader().getResource("sources")); 68 69 env.setHistoryEnabled(false); 70 env.setProjectsEnabled(true); 71 env.setDefaultProjectsFromNames(Collections.singleton("__all__")); 72 73 env.getSuggesterConfig().setRebuildCronConfig(null); 74 } 75 76 @AfterAll tearDownClass()77 public static void tearDownClass() { 78 repository.destroy(); 79 } 80 81 @Test testSearchCors()82 void testSearchCors() { 83 Response response = target(SearchController.PATH) 84 .request() 85 .header(CORS_REQUEST_HEADER, "http://example.com") 86 .get(); 87 assertEquals("*", response.getHeaderString(ALLOW_CORS_HEADER)); 88 } 89 } 90