Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eq/hc for HttpVersionSelection #9999

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.micronaut.http.client.annotation.Client;

import java.util.Arrays;
import java.util.Objects;

/**
* This class collects information about HTTP client protocol version settings, such as the
Expand Down Expand Up @@ -196,6 +197,25 @@ public boolean isHttp3() {
return http3;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HttpVersionSelection that = (HttpVersionSelection) o;
return alpn == that.alpn && http2CipherSuites == that.http2CipherSuites && http3 == that.http3 && plaintextMode == that.plaintextMode && Arrays.equals(alpnSupportedProtocols, that.alpnSupportedProtocols);
}

@Override
public int hashCode() {
int result = Objects.hash(plaintextMode, alpn, http2CipherSuites, http3);
result = 31 * result + Arrays.hashCode(alpnSupportedProtocols);
return result;
}

/**
* The connection mode to use for plaintext (non-TLS) connections.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.micronaut.http.client


import io.micronaut.core.annotation.Introspected
import io.micronaut.core.beans.BeanIntrospector
import io.micronaut.http.client.annotation.Client
import spock.lang.Specification

class HttpVersionSelectionSpec extends Specification {
def 'annotation equals'() {
given:
def introspection = BeanIntrospector.SHARED.findIntrospection(TestClass).get()
def s1 = HttpVersionSelection.forClientAnnotation(introspection)
def s2 = HttpVersionSelection.forClientAnnotation(introspection)

expect:
s1 == s2
}

@Client(alpnModes = ["h2"], plaintextMode = HttpVersionSelection.PlaintextMode.HTTP_1)
@Introspected
static interface TestClass {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public boolean equals(Object o) {
return false;
}
ClientKey clientKey = (ClientKey) o;
return httpVersion == clientKey.httpVersion &&
return Objects.equals(httpVersion, clientKey.httpVersion) &&
Objects.equals(clientId, clientKey.clientId) &&
Objects.equals(filterAnnotations, clientKey.filterAnnotations) &&
Objects.equals(path, clientKey.path) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.context.event.BeanCreatedEvent
import io.micronaut.context.event.BeanCreatedEventListener
import io.micronaut.core.annotation.Introspected
import io.micronaut.core.beans.BeanIntrospector
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.HttpClientRegistry
import io.micronaut.http.client.HttpVersionSelection
import io.micronaut.http.client.annotation.Client
import io.netty.channel.Channel
import io.netty.util.AttributeKey
Expand Down Expand Up @@ -75,6 +79,26 @@ class DefaultNettyHttpClientRegistrySpec extends Specification {
!customizer.duplicate
}

@Issue("https://github.com/micronaut-projects/micronaut-core/pull/9999")
def 'same client with protocol customizations'() {
given:
def ctx = ApplicationContext.run([
'spec.name': 'DefaultNettyHttpClientRegistrySpec',
'micronaut.http.services.test-client2.url': 'https://micronaut.io'
])
def registry = ctx.getBean(HttpClientRegistry)
def introspection = BeanIntrospector.SHARED.findIntrospection(DeclarativeClient2).get()

when:
def client1 = registry.getClient(introspection)
def client2 = registry.getClient(introspection)
then:
client1 == client2

cleanup:
ctx.close()
}

@Requires(property = 'spec.name', value = 'DefaultNettyHttpClientRegistrySpec')
@Client('test-client')
static interface DeclarativeClient {
Expand Down Expand Up @@ -124,4 +148,10 @@ class DefaultNettyHttpClientRegistrySpec extends Specification {
@Client('test-client')
HttpClient client;
}

@Requires(property = 'spec.name', value = 'DefaultNettyHttpClientRegistrySpec')
@Introspected
@Client(value = 'test-client2', alpnModes = ["h2"], plaintextMode = HttpVersionSelection.PlaintextMode.HTTP_1)
static interface DeclarativeClient2 {
}
}