File: EmptyAuthenticate.java

package info (click to toggle)
openjdk-26 26~12ea-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 826,896 kB
  • sloc: java: 5,602,163; cpp: 1,336,664; xml: 1,321,153; ansic: 488,421; asm: 404,003; objc: 21,113; sh: 15,220; javascript: 13,281; python: 8,323; makefile: 2,519; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (143 lines) | stat: -rw-r--r-- 5,790 bytes parent folder | download | duplicates (3)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8263899
 * @summary Verifies that empty `WWW-Authenticate` header is correctly parsed
 * @library /test/jdk/java/net/httpclient/lib
 *          /test/lib
 * @build jdk.httpclient.test.lib.common.HttpServerAdapters
 *        jdk.test.lib.net.SimpleSSLContext
 * @run junit EmptyAuthenticate
 */

import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpTestHandler;
import jdk.httpclient.test.lib.common.HttpServerAdapters.HttpTestServer;
import jdk.test.lib.net.SimpleSSLContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static java.net.http.HttpClient.Builder.NO_PROXY;
import static org.junit.jupiter.api.Assertions.assertEquals;

class EmptyAuthenticate {

    private static final SSLContext SSL_CONTEXT = createSslContext();

    private static final String WWW_AUTH_HEADER_NAME = "WWW-Authenticate";

    private static SSLContext createSslContext() {
        try {
            return new SimpleSSLContext().get();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    @ParameterizedTest
    @MethodSource("args")
    void test(Version version, boolean secure) throws Exception {
        String handlerPath = "/%s/%s/".formatted(EmptyAuthenticate.class.getSimpleName(), version);
        String uriPath = handlerPath + (secure ? 's' : 'c');
        HttpTestServer server = createServer(version, secure, handlerPath);
        try (HttpClient client = createClient(version, secure)) {
            HttpRequest request = createRequest(server, secure, uriPath);
            HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
            HttpHeaders responseHeaders = response.headers();
            assertEquals(
                    "",
                    responseHeaders.firstValue(WWW_AUTH_HEADER_NAME).orElse(null),
                    () -> "was expecting empty `%s` header in: %s".formatted(
                            WWW_AUTH_HEADER_NAME, responseHeaders.map()));
        } finally {
            server.stop();
        }
    }

    static Stream<Arguments> args() {
        return Stream
                .of(Version.HTTP_1_1, Version.HTTP_2)
                .flatMap(version -> Stream
                        .of(true, false)
                        .map(secure -> Arguments.of(version, secure)));
    }

    private static HttpTestServer createServer(Version version, boolean secure, String uriPath)
            throws IOException {
        HttpTestServer server = secure
                ? HttpTestServer.create(version, SSL_CONTEXT)
                : HttpTestServer.create(version);
        HttpTestHandler handler = new ServerHandlerRespondingWithEmptyWwwAuthHeader();
        server.addHandler(handler, uriPath);
        server.start();
        return server;
    }

    private static final class ServerHandlerRespondingWithEmptyWwwAuthHeader implements HttpTestHandler {

        private int responseIndex = 0;

        @Override
        public synchronized void handle(HttpServerAdapters.HttpTestExchange exchange) throws IOException {
            try (exchange) {
                exchange.getResponseHeaders().addHeader(WWW_AUTH_HEADER_NAME, "");
                byte[] responseBodyBytes = "test body %d"
                        .formatted(responseIndex)
                        .getBytes(StandardCharsets.US_ASCII);
                exchange.sendResponseHeaders(401, responseBodyBytes.length);
                exchange.getResponseBody().write(responseBodyBytes);
            } finally {
                responseIndex++;
            }
        }

    }

    private static HttpClient createClient(Version version, boolean secure) {
        HttpClient.Builder clientBuilder = HttpClient.newBuilder().version(version).proxy(NO_PROXY);
        if (secure) {
            clientBuilder.sslContext(SSL_CONTEXT);
        }
        return clientBuilder.build();
    }

    private static HttpRequest createRequest(HttpTestServer server, boolean secure, String uriPath) {
        URI uri = URI.create("%s://%s%s".formatted(secure ? "https" : "http", server.serverAuthority(), uriPath));
        return HttpRequest.newBuilder(uri).version(server.getVersion()).GET().build();
    }

}