21
21
import java .nio .charset .StandardCharsets ;
22
22
import java .util .Collections ;
23
23
import java .util .List ;
24
+ import java .util .Map ;
24
25
25
26
import javax .servlet .ServletException ;
26
27
33
34
import org .springframework .http .converter .ResourceHttpMessageConverter ;
34
35
import org .springframework .mock .web .MockHttpServletRequest ;
35
36
import org .springframework .mock .web .MockHttpServletResponse ;
37
+ import org .springframework .web .servlet .function .RouterFunctions ;
36
38
import org .springframework .web .servlet .function .ServerRequest ;
37
39
import org .springframework .web .servlet .function .ServerResponse ;
40
+ import org .springframework .web .util .UriComponentsBuilder ;
38
41
39
42
import static org .assertj .core .api .Assertions .assertThat ;
40
43
@@ -47,8 +50,13 @@ class GraphiQlHandlerTests {
47
50
48
51
private static final List <HttpMessageConverter <?>> MESSAGE_READERS = Collections .emptyList ();
49
52
50
- private GraphiQlHandler handler = new GraphiQlHandler ("/graphql" , null ,
51
- new ByteArrayResource ("GRAPHIQL" .getBytes (StandardCharsets .UTF_8 )));
53
+ private final GraphiQlHandler handler = initHandler ("/graphql" );
54
+
55
+
56
+ private static GraphiQlHandler initHandler (String path ) {
57
+ return new GraphiQlHandler (path , null , new ByteArrayResource ("GRAPHIQL" .getBytes (StandardCharsets .UTF_8 )));
58
+ }
59
+
52
60
53
61
@ Test
54
62
void shouldRedirectWithPathQueryParameter () {
@@ -57,7 +65,8 @@ void shouldRedirectWithPathQueryParameter() {
57
65
ServerResponse response = this .handler .handleRequest (request );
58
66
assertThat (response .statusCode ()).isEqualTo (HttpStatus .TEMPORARY_REDIRECT );
59
67
assertThat (response .headers ().getLocation ()).isNotNull ();
60
- assertThat (response .headers ().getLocation ().toASCIIString ()).isEqualTo ("http://localhost/graphiql?path=/graphql" );
68
+ assertThat (response .headers ().getLocation ().toASCIIString ())
69
+ .isEqualTo ("http://localhost/graphiql?path=/graphql" );
61
70
}
62
71
63
72
@ Test
@@ -69,7 +78,27 @@ void shouldRedirectWithPathAndWsPathQueryParameter() {
69
78
ServerResponse response = wsHandler .handleRequest (request );
70
79
assertThat (response .statusCode ()).isEqualTo (HttpStatus .TEMPORARY_REDIRECT );
71
80
assertThat (response .headers ().getLocation ()).isNotNull ();
72
- assertThat (response .headers ().getLocation ().toASCIIString ()).isEqualTo ("http://localhost/graphiql?path=/graphql&wsPath=/graphql" );
81
+ assertThat (response .headers ().getLocation ().toASCIIString ())
82
+ .isEqualTo ("http://localhost/graphiql?path=/graphql&wsPath=/graphql" );
83
+ }
84
+
85
+ @ Test // gh-478
86
+ void shouldRedirectWithPathVariables () {
87
+ Map <String , Object > pathVariables = Collections .singletonMap ("envId" , "123" );
88
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder .fromUriString ("/env/{envId}/graphiql" );
89
+ String path = uriBuilder .build (pathVariables ).toString ();
90
+
91
+ MockHttpServletRequest servletRequest = new MockHttpServletRequest ("GET" , path );
92
+ ServerRequest request = ServerRequest .create (servletRequest , MESSAGE_READERS );
93
+ servletRequest .setAttribute (RouterFunctions .URI_TEMPLATE_VARIABLES_ATTRIBUTE , pathVariables );
94
+
95
+ GraphiQlHandler graphiQlHandler = initHandler (uriBuilder .build ().toString ());
96
+ ServerResponse response = graphiQlHandler .handleRequest (request );
97
+
98
+ assertThat (response .statusCode ()).isEqualTo (HttpStatus .TEMPORARY_REDIRECT );
99
+ assertThat (response .headers ().getLocation ()).isNotNull ();
100
+ assertThat (response .headers ().getLocation ().toASCIIString ())
101
+ .isEqualTo ("http://localhost" + path + "?path=" + path );
73
102
}
74
103
75
104
@ Test
0 commit comments