|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package sample; |
| 17 | + |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 20 | + |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.boot.test.context.TestConfiguration; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 32 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 33 | +import org.springframework.test.web.servlet.MockMvc; |
| 34 | + |
| 35 | +@SpringBootTest |
| 36 | +@AutoConfigureMockMvc |
| 37 | +public class ResourceControllerTests { |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private MockMvc mockMvc; |
| 41 | + |
| 42 | + @Test |
| 43 | + public void shouldReturnOkWithToken() throws Exception { |
| 44 | + this.mockMvc.perform(get("/").header("Authorization", "Bearer TOKEN")) |
| 45 | + .andExpect(status().isOk()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void shouldReturnUnauthorizedWithoutToken() throws Exception { |
| 50 | + this.mockMvc.perform(get("/")) |
| 51 | + .andExpect(status().isUnauthorized()); |
| 52 | + } |
| 53 | + |
| 54 | + @TestConfiguration |
| 55 | + static class ResourceControllerTestConfiguration { |
| 56 | + @Bean |
| 57 | + public JwtDecoder jwtDecoder() { |
| 58 | + return (token) -> { |
| 59 | + Map<String, Object> headers = new HashMap<>(); |
| 60 | + headers.put("alg", "RS256"); |
| 61 | + headers.put("typ", "JWT"); |
| 62 | + |
| 63 | + Map<String, Object> claims = new HashMap<>(); |
| 64 | + claims.put("sub", "1234567"); |
| 65 | + claims.put("name", "John Doe"); |
| 66 | + return new Jwt(token, Instant.now(), Instant.now().plusMillis(5000), headers, claims); |
| 67 | + }; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments