|
| 1 | +package graphql.kickstart.spring.web.boot; |
| 2 | + |
| 3 | +import static graphql.Scalars.GraphQLString; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 8 | + |
| 9 | +import graphql.schema.GraphQLFieldDefinition; |
| 10 | +import graphql.schema.GraphQLObjectType; |
| 11 | +import graphql.schema.GraphQLSchema; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 16 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| 17 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.boot.test.context.TestConfiguration; |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | +import org.springframework.http.MediaType; |
| 22 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 23 | +import org.springframework.test.web.servlet.MockMvc; |
| 24 | +import org.springframework.test.web.servlet.ResultActions; |
| 25 | + |
| 26 | +@ExtendWith(SpringExtension.class) |
| 27 | +@AutoConfigureMockMvc |
| 28 | +@ImportAutoConfiguration({JacksonAutoConfiguration.class, GraphQLWebAutoConfiguration.class}) |
| 29 | +@SpringBootTest(properties = {"debug=true", "graphql.servlet.mapping=/graphql", "graphql.servlet.cors.allowed-origins=https://trusted.com", "graphql.servlet.cors.allowed-methods=GET,HEAD,POST"}) |
| 30 | +class CorsTest { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private MockMvc mockMvc; |
| 34 | + |
| 35 | + @Test |
| 36 | + void evilDomain_shouldNotBeAllowed() throws Exception { |
| 37 | + ResultActions resultActions = performCorsPreflight("https://evil.com"); |
| 38 | + resultActions |
| 39 | + .andExpect(status().isForbidden()) |
| 40 | + .andExpect(content().string("Invalid CORS request")); |
| 41 | + } |
| 42 | + |
| 43 | + private ResultActions performCorsPreflight(String origin) throws Exception { |
| 44 | + return mockMvc.perform( |
| 45 | + options("/graphql") |
| 46 | + .contentType(MediaType.APPLICATION_JSON) |
| 47 | + .header("Access-Control-Request-Method", "POST") |
| 48 | + .header("Origin", origin) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void trustedDomain_shouldBeAllowed() throws Exception { |
| 54 | + ResultActions resultActions = performCorsPreflight("https://trusted.com"); |
| 55 | + resultActions |
| 56 | + .andExpect(status().isOk()) |
| 57 | + .andExpect(header().string("Access-Control-Allow-Origin", "https://trusted.com")) |
| 58 | + .andExpect(header().string("Access-Control-Allow-Methods", "GET,HEAD,POST")); |
| 59 | + } |
| 60 | + |
| 61 | + @TestConfiguration |
| 62 | + static class MyTestConfiguration { |
| 63 | + |
| 64 | + @Bean |
| 65 | + public GraphQLSchema graphQLSchema() { |
| 66 | + return GraphQLSchema.newSchema() |
| 67 | + .query(GraphQLObjectType.newObject().name("Query").field( |
| 68 | + GraphQLFieldDefinition.newFieldDefinition() |
| 69 | + .name("echo") |
| 70 | + .type(GraphQLString) |
| 71 | + .build()).build()).build(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments