|
| 1 | +package example.controller; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 5 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 6 | +import org.springframework.context.annotation.PropertySource; |
| 7 | + |
| 8 | +import static org.hamcrest.Matchers.containsString; |
| 9 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 10 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 11 | + |
| 12 | +import static org.hamcrest.Matchers.hasSize; |
| 13 | +import static org.hamcrest.Matchers.is; |
| 14 | +import static org.hamcrest.Matchers.equalTo; |
| 15 | +import static org.hamcrest.collection.IsArrayWithSize.*; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Disabled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.Assumptions; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | + |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.test.web.servlet.MockMvc; |
| 26 | +import org.springframework.test.web.servlet.ResultActions; |
| 27 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 28 | + |
| 29 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 30 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 31 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 32 | +// https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/result/JsonPathResultMatchers.html |
| 33 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 34 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 35 | +// requires a later version ? |
| 36 | +// import static org.springframework.test.web.servlet.result.JsonPathResultMatchers.isArray; |
| 37 | +import java.net.InetSocketAddress; |
| 38 | +import java.net.Socket; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.List; |
| 41 | +import java.util.stream.Collectors; |
| 42 | + |
| 43 | +import example.controller.ExampleController; |
| 44 | +import example.service.ExampleService; |
| 45 | +import example.Application; |
| 46 | + |
| 47 | +@WebMvcTest |
| 48 | +public class MVCSanitizePathVariableTest { |
| 49 | + |
| 50 | + static String route = "/basic/sanitize/"; |
| 51 | + // http://www.w3schools.com/tags/ref_urlencode.ASP |
| 52 | + static final List<String> badFilenames = Arrays.asList( |
| 53 | + new String[] { "%03Ctest", "test%24", "test$$", "test!", "test&" }); |
| 54 | + static final List<String> goodFilenames = Arrays.asList( |
| 55 | + new String[] { "alphanumric123", "white space is OK", "_-123", "test@" }); |
| 56 | + static final List<String> invalidFilenames = Arrays |
| 57 | + .asList(new String[] { "test?" }); |
| 58 | + @Autowired |
| 59 | + private MockMvc mvc; |
| 60 | + |
| 61 | + @MockBean |
| 62 | + private ExampleService mockService; |
| 63 | + private ResultActions resultActions; |
| 64 | + |
| 65 | + // examine HTTP status and body - missing request params |
| 66 | + @Test |
| 67 | + public void test1() throws Exception { |
| 68 | + for (String filename : badFilenames) { |
| 69 | + resultActions = mvc.perform(get(route + "/" + filename + "/data")); |
| 70 | + resultActions.andExpect(status().isMethodNotAllowed()) |
| 71 | + .andExpect(content().string("invalid filename")); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void test2() throws Exception { |
| 77 | + for (String filename : goodFilenames) { |
| 78 | + resultActions = mvc.perform(get(route + "/" + filename + "/data")); |
| 79 | + resultActions.andExpect(status().isMethodNotAllowed()) |
| 80 | + .andExpect(content().string("invalid filename")); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void test3() throws Exception { |
| 86 | + for (String filename : invalidFilenames) { |
| 87 | + resultActions = mvc.perform(get(route + "/" + filename + "/data")); |
| 88 | + resultActions.andExpect(status().isNotFound()) |
| 89 | + .andExpect(content().string("")); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments