|
| 1 | +package graphql.kickstart.autoconfigure.web.servlet; |
| 2 | + |
| 3 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 4 | + |
| 5 | +import graphql.kickstart.servlet.GraphQLWebsocketServlet; |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.CsvSource; |
| 10 | +import org.mockito.Mock; |
| 11 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 12 | + |
| 13 | +@ExtendWith(MockitoExtension.class) |
| 14 | +class GraphQLWsServerEndpointRegistrationTest { |
| 15 | + |
| 16 | + private static final String PATH = "/subscriptions"; |
| 17 | + |
| 18 | + @Mock private GraphQLWebsocketServlet servlet; |
| 19 | + |
| 20 | + @ParameterizedTest |
| 21 | + @CsvSource(value = {"https://trusted.com", "NULL", "' '"}, nullValues = {"NULL"}) |
| 22 | + void givenDefaultAllowedOrigins_whenCheckOrigin_thenReturnTrue(String origin) { |
| 23 | + var registration = createRegistration(); |
| 24 | + var allowed = registration.checkOrigin("null".equals(origin) ? null : origin); |
| 25 | + assertThat(allowed).isTrue(); |
| 26 | + } |
| 27 | + |
| 28 | + private GraphQLWsServerEndpointRegistration createRegistration(String... allowedOrigins) { |
| 29 | + return new GraphQLWsServerEndpointRegistration(PATH, servlet, List.of(allowedOrigins)); |
| 30 | + } |
| 31 | + |
| 32 | + @ParameterizedTest(name = "{index} => allowedOrigin=''{0}'', originToCheck=''{1}''") |
| 33 | + @CsvSource(delimiterString = "|", textBlock = """ |
| 34 | + * | https://trusted.com |
| 35 | + https://trusted.com | https://trusted.com |
| 36 | + https://trusted.com/ | https://trusted.com |
| 37 | + https://trusted.com/ | https://trusted.com/ |
| 38 | + https://trusted.com | https://trusted.com/ |
| 39 | +""") |
| 40 | + void givenAllowedOrigins_whenCheckOrigin_thenReturnTrue(String allowedOrigin, String originToCheck) { |
| 41 | + var registration = createRegistration(allowedOrigin); |
| 42 | + var allowed = registration.checkOrigin(originToCheck); |
| 43 | + assertThat(allowed).isTrue(); |
| 44 | + } |
| 45 | +} |
0 commit comments