|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.solr.jersey; |
| 19 | + |
| 20 | +import static jakarta.ws.rs.core.HttpHeaders.ACCEPT; |
| 21 | +import static jakarta.ws.rs.core.HttpHeaders.CONTENT_TYPE; |
| 22 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; |
| 23 | +import static jakarta.ws.rs.core.MediaType.APPLICATION_XML; |
| 24 | +import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito; |
| 25 | +import static org.apache.solr.SolrTestCaseJ4.params; |
| 26 | +import static org.apache.solr.client.solrj.response.JavaBinResponseParser.JAVABIN_CONTENT_TYPE_V2; |
| 27 | +import static org.apache.solr.common.params.CommonParams.WT; |
| 28 | +import static org.apache.solr.jersey.RequestContextKeys.SOLR_PARAMS; |
| 29 | +import static org.apache.solr.jersey.RequestContextKeys.SOLR_QUERY_REQUEST; |
| 30 | +import static org.mockito.Mockito.doReturn; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.when; |
| 33 | + |
| 34 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 35 | +import jakarta.ws.rs.container.ContainerResponseContext; |
| 36 | +import jakarta.ws.rs.container.ResourceInfo; |
| 37 | +import jakarta.ws.rs.core.MultivaluedHashMap; |
| 38 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 39 | +import java.util.Map; |
| 40 | +import org.apache.solr.SolrTestCase; |
| 41 | +import org.apache.solr.handler.admin.ZookeeperRead; |
| 42 | +import org.apache.solr.handler.admin.api.ReloadCollectionAPI; |
| 43 | +import org.apache.solr.request.SolrQueryRequest; |
| 44 | +import org.junit.After; |
| 45 | +import org.junit.Before; |
| 46 | +import org.junit.BeforeClass; |
| 47 | +import org.junit.Test; |
| 48 | +import org.mockito.InjectMocks; |
| 49 | +import org.mockito.Mock; |
| 50 | +import org.mockito.MockitoAnnotations; |
| 51 | + |
| 52 | +/** Checks response negotiation when a Jersey resource has no local SolrQueryRequest. */ |
| 53 | +public class MediaTypeOverridingFilterTest extends SolrTestCase { |
| 54 | + |
| 55 | + @Mock private ResourceInfo resourceInfo; |
| 56 | + @InjectMocks private MediaTypeOverridingFilter filter; |
| 57 | + |
| 58 | + private AutoCloseable mocks; |
| 59 | + private ContainerRequestContext request; |
| 60 | + private ContainerResponseContext response; |
| 61 | + private MultivaluedMap<String, String> requestHeaders; |
| 62 | + private MultivaluedMap<String, Object> responseHeaders; |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void ensureWorkingMockito() { |
| 66 | + assumeWorkingMockito(); |
| 67 | + } |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setUpFilter() { |
| 71 | + mocks = MockitoAnnotations.openMocks(this); |
| 72 | + request = mock(ContainerRequestContext.class); |
| 73 | + response = mock(ContainerResponseContext.class); |
| 74 | + requestHeaders = new MultivaluedHashMap<>(); |
| 75 | + responseHeaders = new MultivaluedHashMap<>(); |
| 76 | + responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML); |
| 77 | + doReturn(ReloadCollectionAPI.class).when(resourceInfo).getResourceClass(); |
| 78 | + when(request.getHeaders()).thenReturn(requestHeaders); |
| 79 | + when(request.getHeaderString(ACCEPT)).thenAnswer(ignored -> requestHeaders.getFirst(ACCEPT)); |
| 80 | + when(response.getHeaders()).thenReturn(responseHeaders); |
| 81 | + when(response.getStatus()).thenReturn(200); |
| 82 | + } |
| 83 | + |
| 84 | + @After |
| 85 | + public void closeMocks() throws Exception { |
| 86 | + mocks.close(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testWtFromQueryParamsWithoutSolrQueryRequest() throws Exception { |
| 91 | + requestHeaders.putSingle(ACCEPT, "text/plain"); |
| 92 | + final Map<String, String> formats = |
| 93 | + Map.of( |
| 94 | + "json", APPLICATION_JSON, "xml", APPLICATION_XML, "javabin", JAVABIN_CONTENT_TYPE_V2); |
| 95 | + for (var format : formats.entrySet()) { |
| 96 | + responseHeaders.putSingle(CONTENT_TYPE, "text/plain"); |
| 97 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, format.getKey())); |
| 98 | + |
| 99 | + filter.filter(request, response); |
| 100 | + |
| 101 | + assertEquals(format.getKey(), format.getValue(), responseHeaders.getFirst(CONTENT_TYPE)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testSolrQueryRequestParamsTakePrecedence() throws Exception { |
| 107 | + responseHeaders.putSingle(CONTENT_TYPE, "text/plain"); |
| 108 | + final SolrQueryRequest solrRequest = mock(SolrQueryRequest.class); |
| 109 | + when(solrRequest.getParams()).thenReturn(params(WT, "xml")); |
| 110 | + when(request.getProperty(SOLR_QUERY_REQUEST)).thenReturn(solrRequest); |
| 111 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin")); |
| 112 | + |
| 113 | + filter.filter(request, response); |
| 114 | + |
| 115 | + assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testAcceptHeaderUsedWithoutWt() throws Exception { |
| 120 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params()); |
| 121 | + requestHeaders.putSingle(ACCEPT, APPLICATION_XML); |
| 122 | + |
| 123 | + filter.filter(request, response); |
| 124 | + |
| 125 | + assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE)); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testDefaultsToJsonWithoutWt() throws Exception { |
| 130 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params()); |
| 131 | + for (String accept : new String[] {null, "*/*"}) { |
| 132 | + requestHeaders.clear(); |
| 133 | + if (accept != null) { |
| 134 | + requestHeaders.putSingle(ACCEPT, accept); |
| 135 | + } |
| 136 | + responseHeaders.putSingle(CONTENT_TYPE, APPLICATION_XML); |
| 137 | + |
| 138 | + filter.filter(request, response); |
| 139 | + |
| 140 | + assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testMissingParamsStillDefaultsToJson() throws Exception { |
| 146 | + filter.filter(request, response); |
| 147 | + |
| 148 | + assertEquals(APPLICATION_JSON, responseHeaders.getFirst(CONTENT_TYPE)); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testErrorsPreserveMediaType() throws Exception { |
| 153 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin")); |
| 154 | + for (int status : new int[] {400, 500}) { |
| 155 | + when(response.getStatus()).thenReturn(status); |
| 156 | + |
| 157 | + filter.filter(request, response); |
| 158 | + |
| 159 | + assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testExemptResourcesPreserveMediaType() throws Exception { |
| 165 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, "javabin")); |
| 166 | + for (Class<?> resourceClass : new Class<?>[] {null, ZookeeperRead.class}) { |
| 167 | + doReturn(resourceClass).when(resourceInfo).getResourceClass(); |
| 168 | + |
| 169 | + filter.filter(request, response); |
| 170 | + |
| 171 | + assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE)); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testBlankAndUnknownWtDoNotOverrideNegotiatedType() throws Exception { |
| 177 | + requestHeaders.putSingle(ACCEPT, APPLICATION_XML); |
| 178 | + for (String wt : new String[] {"", "unknown"}) { |
| 179 | + when(request.getProperty(SOLR_PARAMS)).thenReturn(params(WT, wt)); |
| 180 | + |
| 181 | + filter.filter(request, response); |
| 182 | + |
| 183 | + assertEquals(APPLICATION_XML, responseHeaders.getFirst(CONTENT_TYPE)); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments