2121import java .nio .charset .StandardCharsets ;
2222import java .util .Collections ;
2323import java .util .List ;
24+ import java .util .Map ;
2425
2526import javax .servlet .ServletException ;
2627
3334import org .springframework .http .converter .ResourceHttpMessageConverter ;
3435import org .springframework .mock .web .MockHttpServletRequest ;
3536import org .springframework .mock .web .MockHttpServletResponse ;
37+ import org .springframework .web .servlet .function .RouterFunctions ;
3638import org .springframework .web .servlet .function .ServerRequest ;
3739import org .springframework .web .servlet .function .ServerResponse ;
40+ import org .springframework .web .util .UriComponentsBuilder ;
3841
3942import static org .assertj .core .api .Assertions .assertThat ;
4043
@@ -47,8 +50,13 @@ class GraphiQlHandlerTests {
4750
4851 private static final List <HttpMessageConverter <?>> MESSAGE_READERS = Collections .emptyList ();
4952
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+
5260
5361 @ Test
5462 void shouldRedirectWithPathQueryParameter () {
@@ -57,7 +65,8 @@ void shouldRedirectWithPathQueryParameter() {
5765 ServerResponse response = this .handler .handleRequest (request );
5866 assertThat (response .statusCode ()).isEqualTo (HttpStatus .TEMPORARY_REDIRECT );
5967 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" );
6170 }
6271
6372 @ Test
@@ -69,7 +78,27 @@ void shouldRedirectWithPathAndWsPathQueryParameter() {
6978 ServerResponse response = wsHandler .handleRequest (request );
7079 assertThat (response .statusCode ()).isEqualTo (HttpStatus .TEMPORARY_REDIRECT );
7180 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 );
73102 }
74103
75104 @ Test
0 commit comments