|
| 1 | +package org.fairdatateam.fairdatapoint.acceptance.search.sparql; |
| 2 | + |
| 3 | +import jakarta.servlet.http.HttpServletRequest; |
| 4 | +import org.fairdatateam.fairdatapoint.api.controller.search.SparqlProxyCleaningService; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpRequest; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.http.client.ClientHttpResponse; |
| 12 | +import org.springframework.mock.http.client.MockClientHttpRequest; |
| 13 | +import org.springframework.mock.http.client.MockClientHttpResponse; |
| 14 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 15 | +import org.springframework.test.context.TestPropertySource; |
| 16 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 17 | +import java.lang.reflect.InvocationTargetException; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +@SpringJUnitConfig(SparqlProxyCleaningService.class) |
| 25 | +@TestPropertySource(properties = {"openapi.title=Test API", "openapi.version=1.0"}) |
| 26 | +public class SparqlProxyCleaningServiceTest { |
| 27 | + |
| 28 | + private final SparqlProxyCleaningService cleaningService; |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructor |
| 32 | + */ |
| 33 | + @Autowired |
| 34 | + public SparqlProxyCleaningServiceTest(SparqlProxyCleaningService cleaningService) { |
| 35 | + this.cleaningService = cleaningService; |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void cleansRequestHeaders() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 40 | + final String remoteIP = "1.2.3.4"; |
| 41 | + // headers for the incoming request from a remote client |
| 42 | + HttpHeaders requestHeadersIncoming = new HttpHeaders(); |
| 43 | + requestHeadersIncoming.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 44 | + requestHeadersIncoming.setBearerAuth("dummy-token"); |
| 45 | + MockHttpServletRequest requestIncoming = new MockHttpServletRequest(); |
| 46 | + requestIncoming.setRemoteAddr(remoteIP); |
| 47 | + |
| 48 | + // headers for the outgoing request to the upstream sparql endpoint |
| 49 | + HttpHeaders requestHeadersOutgoing = new HttpHeaders(); |
| 50 | + |
| 51 | + // get access to package-private method for testing |
| 52 | + Method methodUnderTest = SparqlProxyCleaningService.class.getDeclaredMethod( |
| 53 | + "cleanRequestHeadersFactory", HttpServletRequest.class, HttpHeaders.class |
| 54 | + ); |
| 55 | + methodUnderTest.setAccessible(true); |
| 56 | + |
| 57 | + // create callable using factory |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + Consumer<HttpHeaders> cleanRequestHeaders = (Consumer<HttpHeaders>) methodUnderTest.invoke( |
| 60 | + cleaningService, requestIncoming, requestHeadersIncoming); |
| 61 | + |
| 62 | + // perform header clean-up |
| 63 | + cleanRequestHeaders.accept(requestHeadersOutgoing); |
| 64 | + |
| 65 | + // check result |
| 66 | + assertThat(requestHeadersOutgoing.getAccept()).hasSize(1).contains(MediaType.APPLICATION_JSON); |
| 67 | + assertThat(requestHeadersOutgoing.containsKey(HttpHeaders.AUTHORIZATION)).isFalse(); |
| 68 | + assertThat(requestHeadersOutgoing.get(SparqlProxyCleaningService.HEADER_X_FORWARDED_FOR)) |
| 69 | + .hasSize(1).contains(remoteIP); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void cleansResponseHeaders() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 74 | + // mock headers "received" from the upstream sparql endpoint |
| 75 | + final HttpHeaders mockBackendResponseHeaders = new HttpHeaders(); |
| 76 | + |
| 77 | + // add custom header |
| 78 | + final String customHeaderName = "my-custom-header"; |
| 79 | + mockBackendResponseHeaders.add(customHeaderName, "dummy"); |
| 80 | + |
| 81 | + // add standard hop-by-hop headers |
| 82 | + final List<String> hopByHopHeaders = SparqlProxyCleaningService.getHopByHopHeaders(); |
| 83 | + hopByHopHeaders.forEach(header -> mockBackendResponseHeaders.add(header, "dummy")); |
| 84 | + |
| 85 | + // add connection header (list is incomplete on purpose, e.g. it does not contain all hop-by-hop headers) |
| 86 | + mockBackendResponseHeaders.put(HttpHeaders.CONNECTION, List.of(customHeaderName, hopByHopHeaders.get(0))); |
| 87 | + |
| 88 | + // add server header |
| 89 | + final String backendServerHeader = "mock backend sparql server 1.0"; |
| 90 | + mockBackendResponseHeaders.add(HttpHeaders.SERVER, backendServerHeader); |
| 91 | + |
| 92 | + // get access to package-private method for testing |
| 93 | + Method methodUnderTest = SparqlProxyCleaningService.class.getDeclaredMethod( |
| 94 | + "cleanResponse", HttpRequest.class, ClientHttpResponse.class |
| 95 | + ); |
| 96 | + methodUnderTest.setAccessible(true); |
| 97 | + |
| 98 | + try (final MockClientHttpResponse sparqlServerResponse = new MockClientHttpResponse()) { |
| 99 | + // clean the response |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + ResponseEntity<byte[]> cleanedResponse = (ResponseEntity<byte[]>) methodUnderTest.invoke( |
| 102 | + cleaningService, new MockClientHttpRequest(), sparqlServerResponse |
| 103 | + ); |
| 104 | + |
| 105 | + // check result |
| 106 | + final HttpHeaders cleanedResponseHeaders = cleanedResponse.getHeaders(); |
| 107 | + System.out.println(cleanedResponseHeaders); |
| 108 | + assertThat(cleanedResponseHeaders).doesNotContainKey(customHeaderName); |
| 109 | + hopByHopHeaders.forEach(header -> assertThat(cleanedResponseHeaders).doesNotContainKey(header)); |
| 110 | + assertThat(cleanedResponseHeaders).doesNotContainKey(HttpHeaders.CONNECTION); |
| 111 | + assertThat(cleanedResponseHeaders).doesNotContainEntry(HttpHeaders.SERVER, List.of(backendServerHeader)); |
| 112 | + assertThat(cleanedResponseHeaders).containsEntry(HttpHeaders.SERVER, List.of("Test API 1.0")); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments