Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 2a1d8ac

Browse files
committed
Add the ability to set HTTP headers in GraphQLTestTemplate.
This is particularly useful for testing GraphQL servers that require some kind of authentication in the headers. See #173
1 parent 73f426f commit 2a1d8ac

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/GraphQLTestTemplate.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.core.io.Resource;
1010
import org.springframework.core.io.ResourceLoader;
1111
import org.springframework.http.HttpEntity;
12+
import org.springframework.http.HttpHeaders;
1213
import org.springframework.http.HttpMethod;
1314
import org.springframework.http.ResponseEntity;
1415
import org.springframework.util.StreamUtils;
@@ -27,6 +28,7 @@ public class GraphQLTestTemplate {
2728
private String graphqlMapping;
2829

2930
private ObjectMapper objectMapper = new ObjectMapper();
31+
private HttpHeaders headers = new HttpHeaders();
3032

3133
private String createJsonQuery(String graphql, ObjectNode variables)
3234
throws JsonProcessingException {
@@ -48,6 +50,32 @@ private String loadResource(Resource resource) throws IOException {
4850
}
4951
}
5052

53+
/**
54+
* Add an HTTP header that will be sent with each request this sends.
55+
*
56+
* @param String Name (key) of HTTP header to add.
57+
* @param String Value of HTTP header to add.
58+
*/
59+
public void addHeader(String name, String value) {
60+
headers.add(name, value);
61+
}
62+
63+
/**
64+
* Replace any associated HTTP headers with the provided headers.
65+
*
66+
* @param HttpHeaders Headers to use.
67+
*/
68+
public void setHeaders(HttpHeaders newHeaders) {
69+
headers = newHeaders
70+
}
71+
72+
/**
73+
* Clear all associated HTTP headers.
74+
*/
75+
public void clearHeaders() {
76+
setHeaders(new HttpHeaders());
77+
}
78+
5179
/**
5280
* @deprecated Use {@link #postForResource(String)} instead
5381
*
@@ -77,11 +105,11 @@ public GraphQLResponse postForResource(String graphqlResource) throws IOExceptio
77105
}
78106

79107
public GraphQLResponse postMultipart(String query, String variables) {
80-
return postRequest(RequestFactory.forMultipart(query, variables));
108+
return postRequest(RequestFactory.forMultipart(query, variables, headers));
81109
}
82110

83111
private GraphQLResponse post(String payload) {
84-
return postRequest(RequestFactory.forJson(payload));
112+
return postRequest(RequestFactory.forJson(payload, headers));
85113
}
86114

87115
private GraphQLResponse postRequest(HttpEntity<Object> request) {

graphql-spring-boot-test/src/main/java/com/graphql/spring/boot/test/RequestFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ private RequestFactory() {
1111
// utility class
1212
}
1313

14-
static HttpEntity<Object> forJson(String json) {
15-
HttpHeaders headers = new HttpHeaders();
14+
static HttpEntity<Object> forJson(String json, HttpHeaders headers) {
1615
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
1716
return new HttpEntity<>(json, headers);
1817
}
1918

20-
static HttpEntity<Object> forMultipart(String query, String variables) {
21-
HttpHeaders headers = new HttpHeaders();
19+
static HttpEntity<Object> forMultipart(String query, String variables, HttpHeaders headers) {
2220
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
2321
LinkedMultiValueMap<String, Object> values = new LinkedMultiValueMap<>();
24-
values.add("query", forJson(query));
25-
values.add("variables", forJson(variables));
22+
values.add("query", forJson(query, headers));
23+
values.add("variables", forJson(variables, headers));
2624
return new HttpEntity<>(values, headers);
2725
}
2826

0 commit comments

Comments
 (0)