jdaugherty commented on code in PR #15478: URL: https://github.com/apache/grails-core/pull/15478#discussion_r2895813333
########## grails-testing-support-httpclient/src/main/groovy/org/apache/grails/testing/httpclient/HttpClientSupport.groovy: ########## @@ -0,0 +1,418 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.grails.testing.httpclient + +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.time.Duration + +import groovy.json.JsonOutput +import groovy.transform.CompileStatic +import groovy.xml.MarkupBuilder + +import org.springframework.beans.factory.annotation.Value + +/** + * Test support trait that provides a lazily-created JDK {@link HttpClient} for HTTP-based tests. + * <p> + * The client is stored as a static singleton so tests can reuse pooled connections efficiently. + * <p> + * Typical usage in a Grails Spock integration specification (where {@code local.server.port} is injected): + * <pre> + * import grails.testing.mixin.integration.Integration + * import spock.lang.Specification + * + * @Integration + * class MySpec extends Specification implements HttpClientSupport { + * + * void 'health endpoint responds OK'() { + * when: + * def response = http('/health') + * + * then: + * response.expectStatus(200) + * } + * } + * </pre> + * <p> + * Implementing tests must provide {@code local.server.port} (in Grails typically via {@code @Integration}), + * or otherwise override {@code getHttpClientRootUri()}. + */ +@CompileStatic +trait HttpClientSupport { + + @Value('${local.server.port}') + int localServerPort = -1 + + /** + * Shared singleton client reused across tests. + */ + private static volatile HttpClient sharedClient + + /** + * @return the shared singleton client instance, creating it on first access. + */ + HttpClient getHttpClient() { + def client = sharedClient Review Comment: Makes sense, the only change I would suggest then is the spock extension. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
