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 package org.apache.hc.client5.testing.sync;
28
29 import static java.util.concurrent.TimeUnit.MILLISECONDS;
30
31 import org.apache.hc.client5.http.auth.AuthSchemeFactory;
32 import org.apache.hc.client5.http.auth.StandardAuthScheme;
33 import org.apache.hc.client5.http.classic.methods.HttpGet;
34 import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
35 import org.apache.hc.client5.http.impl.win.WinHttpClients;
36 import org.apache.hc.client5.http.impl.win.WindowsNegotiateSchemeGetTokenFail;
37 import org.apache.hc.client5.testing.sync.extension.TestClientResources;
38 import org.apache.hc.core5.http.HttpHeaders;
39 import org.apache.hc.core5.http.HttpHost;
40 import org.apache.hc.core5.http.HttpStatus;
41 import org.apache.hc.core5.http.URIScheme;
42 import org.apache.hc.core5.http.config.Registry;
43 import org.apache.hc.core5.http.config.RegistryBuilder;
44 import org.apache.hc.core5.http.io.entity.EntityUtils;
45 import org.apache.hc.core5.testing.classic.ClassicTestServer;
46 import org.apache.hc.core5.util.Timeout;
47 import org.junit.jupiter.api.Assumptions;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.extension.RegisterExtension;
50
51
52
53
54 public class TestWindowsNegotiateScheme {
55
56 public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
57
58 @RegisterExtension
59 private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
60
61 @Test
62 @org.junit.jupiter.api.Timeout(value = 30000, unit = MILLISECONDS)
63 public void testNoInfiniteLoopOnSPNOutsideDomain() throws Exception {
64 final ClassicTestServer server = testResources.startServer(null, null, null);
65 server.registerHandler("/", (request, response, context) -> {
66 response.addHeader(HttpHeaders.WWW_AUTHENTICATE, StandardAuthScheme.SPNEGO);
67 response.setCode(HttpStatus.SC_UNAUTHORIZED);
68 });
69 final HttpHost target = testResources.targetHost();
70 Assumptions.assumeTrue(WinHttpClients.isWinAuthAvailable(), "Test can only be run on Windows");
71
72
73
74
75
76
77
78
79
80 final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
81 .register(StandardAuthScheme.SPNEGO, context -> new WindowsNegotiateSchemeGetTokenFail(StandardAuthScheme.SPNEGO, "HTTP/example.com")).build();
82
83 final CloseableHttpClient client = testResources.startClient(builder -> builder
84 .setDefaultAuthSchemeRegistry(authSchemeRegistry)
85 );
86
87 final HttpGet httpGet = new HttpGet("/");
88 client.execute(target, httpGet, response -> {
89 EntityUtils.consume(response.getEntity());
90 return null;
91 });
92 }
93
94 }