99import org .springframework .core .io .Resource ;
1010import org .springframework .core .io .ResourceLoader ;
1111import org .springframework .http .HttpEntity ;
12+ import org .springframework .http .HttpHeaders ;
1213import org .springframework .http .HttpMethod ;
1314import org .springframework .http .ResponseEntity ;
1415import 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 name Name (key) of HTTP header to add.
57+ * @param value 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 newHeaders 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 ) {
0 commit comments