Skip to content

Commit ee61156

Browse files
committed
HttpGraphQlTransport respects default content-type
Closes gh-359
1 parent ddc3a15 commit ee61156

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/client/HttpGraphQlTransport.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.core.ParameterizedTypeReference;
2525
import org.springframework.graphql.GraphQlRequest;
2626
import org.springframework.graphql.GraphQlResponse;
27+
import org.springframework.http.HttpHeaders;
2728
import org.springframework.http.MediaType;
2829
import org.springframework.util.Assert;
2930
import org.springframework.web.reactive.function.client.WebClient;
@@ -46,17 +47,27 @@ final class HttpGraphQlTransport implements GraphQlTransport {
4647

4748
private final WebClient webClient;
4849

50+
private final MediaType contentType;
51+
4952

5053
HttpGraphQlTransport(WebClient webClient) {
5154
Assert.notNull(webClient, "WebClient is required");
5255
this.webClient = webClient;
56+
this.contentType = initContentType(webClient);
57+
}
58+
59+
private static MediaType initContentType(WebClient webClient) {
60+
HttpHeaders headers = new HttpHeaders();
61+
webClient.mutate().defaultHeaders(headers::putAll);
62+
MediaType contentType = headers.getContentType();
63+
return (contentType != null ? contentType : MediaType.APPLICATION_GRAPHQL);
5364
}
5465

5566

5667
@Override
5768
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
5869
return this.webClient.post()
59-
.contentType(MediaType.APPLICATION_GRAPHQL)
70+
.contentType(this.contentType)
6071
.accept(MediaType.APPLICATION_GRAPHQL, MediaType.APPLICATION_JSON)
6172
.bodyValue(request.toMap())
6273
.retrieve()

spring-graphql/src/test/java/org/springframework/graphql/client/WebGraphQlClientBuilderTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.graphql.server.webflux.GraphQlHttpHandler;
3939
import org.springframework.graphql.server.webflux.GraphQlWebSocketHandler;
4040
import org.springframework.graphql.support.DocumentSource;
41+
import org.springframework.http.HttpHeaders;
42+
import org.springframework.http.MediaType;
4143
import org.springframework.http.codec.ClientCodecConfigurer;
4244
import org.springframework.http.codec.json.Jackson2JsonDecoder;
4345
import org.springframework.http.server.reactive.HttpHandler;
@@ -174,6 +176,28 @@ void urlEncoding(ClientBuilderSetup builderSetup) {
174176
assertThat(builderSetup.getActualRequest().getUri().toString()).isEqualTo("/graphql%20one");
175177
}
176178

179+
@Test
180+
void contentTypeDefault() {
181+
182+
HttpBuilderSetup setup = new HttpBuilderSetup();
183+
setup.initBuilder().build().document(DOCUMENT).execute().block(TIMEOUT);
184+
185+
WebGraphQlRequest request = setup.getActualRequest();
186+
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_GRAPHQL);
187+
}
188+
189+
@Test
190+
void contentTypeOverride() {
191+
192+
HttpBuilderSetup setup = new HttpBuilderSetup();
193+
setup.initBuilder().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build()
194+
.document(DOCUMENT).execute().block(TIMEOUT);
195+
196+
WebGraphQlRequest request = setup.getActualRequest();
197+
assertThat(request.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
198+
199+
}
200+
177201
@ParameterizedTest
178202
@MethodSource("argumentSource")
179203
void codecConfigurerRegistersJsonPathMappingProvider(ClientBuilderSetup builderSetup) {

0 commit comments

Comments
 (0)