1
1
/*
2
- * Copyright (C) 2023-2024 Thomas Akehurst
2
+ * Copyright (C) 2023-2025 Thomas Akehurst
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import com .github .tomakehurst .wiremock .client .WireMock ;
25
25
import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
26
26
import io .grpc .*;
27
+ import java .util .Arrays ;
27
28
import org .junit .jupiter .api .AfterEach ;
28
29
import org .junit .jupiter .api .BeforeEach ;
29
30
import org .junit .jupiter .api .Test ;
34
35
public class RequestHeadersAcceptanceTest {
35
36
36
37
public static final String X_MY_HEADER = "x-my-Header" ;
38
+ public static final String X_MY_HEADER_BINARY = "x-my-Header-bin" ;
37
39
WireMockGrpcService mockGreetingService ;
38
40
ManagedChannel managedChannel ;
39
41
Channel channel ;
@@ -58,8 +60,6 @@ void init() {
58
60
59
61
managedChannel =
60
62
ManagedChannelBuilder .forAddress ("localhost" , wm .getPort ()).usePlaintext ().build ();
61
- channel = ClientInterceptors .intercept (managedChannel , new HeaderAdditionInterceptor ());
62
- greetingsClient = new GreetingsClient (channel );
63
63
}
64
64
65
65
@ AfterEach
@@ -69,6 +69,8 @@ void tearDown() {
69
69
70
70
@ Test
71
71
void arbitraryRequestHeaderCanBeUsedWhenMatchingAndTemplating () {
72
+ channel = ClientInterceptors .intercept (managedChannel , new HeaderAdditionInterceptor ());
73
+ greetingsClient = new GreetingsClient (channel );
72
74
wm .stubFor (
73
75
post (urlPathEqualTo ("/com.example.grpc.GreetingService/greeting" ))
74
76
.withHeader (X_MY_HEADER , equalTo ("match me" ))
@@ -84,6 +86,22 @@ void arbitraryRequestHeaderCanBeUsedWhenMatchingAndTemplating() {
84
86
assertThat (greeting , is ("The header value was: match me" ));
85
87
}
86
88
89
+ @ Test
90
+ void binaryRequestHeaderCanBeUsed () {
91
+ channel = ClientInterceptors .intercept (managedChannel , new BinaryHeaderAdditionInterceptor ());
92
+ greetingsClient = new GreetingsClient (channel );
93
+ wm .stubFor (
94
+ post (urlPathEqualTo ("/com.example.grpc.GreetingService/greeting" ))
95
+ .withHeader (X_MY_HEADER_BINARY , equalTo (Arrays .toString ("binary match me" .getBytes ())))
96
+ .willReturn (
97
+ okJson ("{\n " + " \" greeting\" : \" {{request.headers.x-my-Header-bin}}\" \n " + "}" )
98
+ .withTransformers ("response-template" )));
99
+
100
+ String greeting = greetingsClient .greet ("Whatever" );
101
+
102
+ assertThat (greeting , is (Arrays .toString ("binary match me" .getBytes ())));
103
+ }
104
+
87
105
public static class HeaderAdditionInterceptor implements ClientInterceptor {
88
106
89
107
static final Metadata .Key <String > CUSTOM_HEADER_KEY =
@@ -103,4 +121,24 @@ public void start(Listener<RespT> responseListener, Metadata headers) {
103
121
};
104
122
}
105
123
}
124
+
125
+ public static class BinaryHeaderAdditionInterceptor implements ClientInterceptor {
126
+
127
+ static final Metadata .Key <byte []> CUSTOM_HEADER_KEY =
128
+ Metadata .Key .of (X_MY_HEADER_BINARY , Metadata .BINARY_BYTE_MARSHALLER );
129
+
130
+ @ Override
131
+ public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
132
+ MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
133
+ return new ForwardingClientCall .SimpleForwardingClientCall <>(
134
+ next .newCall (method , callOptions )) {
135
+
136
+ @ Override
137
+ public void start (Listener <RespT > responseListener , Metadata headers ) {
138
+ headers .put (CUSTOM_HEADER_KEY , "binary match me" .getBytes ());
139
+ super .start (responseListener , headers );
140
+ }
141
+ };
142
+ }
143
+ }
106
144
}
0 commit comments