Skip to content

Commit ca0d4a4

Browse files
committed
Additional tests
HTTP test and unit test
1 parent 3064f2a commit ca0d4a4

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL"
5+
initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL"
6+
purge_cache "$END_USER_VARNISH_SERVICE"
7+
purge_cache "$ADMIN_VARNISH_SERVICE"
8+
purge_cache "$FRONTEND_VARNISH_SERVICE"
9+
10+
# add agent to the readers group to be able to read documents
11+
12+
add-agent-to-group.sh \
13+
-f "$OWNER_CERT_FILE" \
14+
-p "$OWNER_CERT_PWD" \
15+
--agent "$AGENT_URI" \
16+
"${ADMIN_BASE_URL}acl/groups/readers/"
17+
18+
# check for Not Acceptable exception when requesting (X)HTML for a proxied external URL
19+
20+
curl -k -w "%{http_code}\n" -o /dev/null -s \
21+
-G \
22+
-H "Accept: text/html" \
23+
-E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \
24+
--data-urlencode "uri=http://f1d2d4cf-90bb-4f5b-ae4b-921e584b6edd.org" \
25+
"$END_USER_BASE_URL" \
26+
| grep -q "$STATUS_NOT_ACCEPTABLE"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Copyright 2025 Martynas Jusevičius <martynas@atomgraph.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.atomgraph.linkeddatahub.server.filter.request;
18+
19+
import com.atomgraph.client.MediaTypes;
20+
import com.atomgraph.client.vocabulary.AC;
21+
import jakarta.ws.rs.NotAcceptableException;
22+
import jakarta.ws.rs.container.ContainerRequestContext;
23+
import jakarta.ws.rs.core.MediaType;
24+
import jakarta.ws.rs.core.Request;
25+
import jakarta.ws.rs.core.Variant;
26+
import java.io.IOException;
27+
import java.net.URI;
28+
import java.util.Collections;
29+
import java.util.Locale;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.mockito.Mock;
34+
import org.mockito.junit.MockitoJUnitRunner;
35+
import static org.mockito.ArgumentMatchers.anyList;
36+
import static org.mockito.Mockito.*;
37+
38+
/**
39+
* Unit tests for {@link ProxyRequestFilter}.
40+
*
41+
* @author Martynas Jusevičius {@literal <martynas@atomgraph.com>}
42+
*/
43+
@RunWith(MockitoJUnitRunner.class)
44+
public class ProxyRequestFilterTest
45+
{
46+
47+
@Mock private ContainerRequestContext requestContext;
48+
@Mock private Request request;
49+
@Mock private com.atomgraph.linkeddatahub.Application system;
50+
51+
private ProxyRequestFilter filter;
52+
53+
@Before
54+
public void setUp()
55+
{
56+
filter = new ProxyRequestFilter();
57+
filter.mediaTypes = new MediaTypes();
58+
filter.request = request;
59+
filter.system = system;
60+
when(system.getSupportedLanguages()).thenReturn(Collections.emptyList());
61+
}
62+
63+
/** No proxy properties set — filter must be a no-op. */
64+
@Test
65+
public void testNonProxyRequestSkipsFilter() throws IOException
66+
{
67+
// getProperty returns null by default; resolveTargetURI returns empty → filter exits immediately
68+
filter.filter(requestContext);
69+
verify(request, never()).selectVariant(anyList());
70+
verify(requestContext, never()).abortWith(any());
71+
}
72+
73+
/** No acceptable RDF/SPARQL variant — filter must throw 406. */
74+
@Test(expected = NotAcceptableException.class)
75+
public void testNullVariantThrowsNotAcceptable() throws IOException
76+
{
77+
when(requestContext.getProperty(AC.uri.getURI()))
78+
.thenReturn(URI.create("http://example.org/resource"));
79+
when(request.selectVariant(anyList())).thenReturn(null);
80+
filter.filter(requestContext);
81+
}
82+
83+
/** text/html selected as best variant — filter must throw 406. */
84+
@Test(expected = NotAcceptableException.class)
85+
public void testHtmlVariantThrowsNotAcceptable() throws IOException
86+
{
87+
when(requestContext.getProperty(AC.uri.getURI()))
88+
.thenReturn(URI.create("http://example.org/resource"));
89+
when(request.selectVariant(anyList()))
90+
.thenReturn(new Variant(MediaType.TEXT_HTML_TYPE, (Locale) null, null));
91+
filter.filter(requestContext);
92+
}
93+
94+
/** application/xhtml+xml selected as best variant — filter must throw 406. */
95+
@Test(expected = NotAcceptableException.class)
96+
public void testXhtmlVariantThrowsNotAcceptable() throws IOException
97+
{
98+
when(requestContext.getProperty(AC.uri.getURI()))
99+
.thenReturn(URI.create("http://example.org/resource"));
100+
when(request.selectVariant(anyList()))
101+
.thenReturn(new Variant(MediaType.APPLICATION_XHTML_XML_TYPE, (Locale) null, null));
102+
filter.filter(requestContext);
103+
}
104+
105+
}

0 commit comments

Comments
 (0)