|
1 | 1 | package com.codingapi.springboot.security;
|
2 | 2 |
|
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
3 | 5 | import org.junit.jupiter.api.Test;
|
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
4 | 8 | import org.springframework.boot.test.context.SpringBootTest;
|
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.test.web.servlet.MockMvc; |
| 11 | +import org.springframework.test.web.servlet.MvcResult; |
5 | 12 |
|
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | + |
| 15 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 16 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 17 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +@AutoConfigureMockMvc |
6 | 21 | @SpringBootTest
|
| 22 | +@Slf4j |
7 | 23 | public class SecurityJwtApplicationTest {
|
8 | 24 |
|
| 25 | + @Autowired |
| 26 | + private MockMvc mockMvc; |
| 27 | + |
| 28 | + @Test |
| 29 | + void login() throws Exception { |
| 30 | + JSONObject json = new JSONObject(); |
| 31 | + json.put("username","admin"); |
| 32 | + json.put("password","123456"); |
| 33 | + mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()); |
| 34 | + } |
9 | 35 |
|
10 | 36 | @Test
|
11 |
| - void test() { |
| 37 | + void noToken() throws Exception { |
| 38 | + mockMvc.perform(get("/api/hello").contentType(MediaType.APPLICATION_JSON)).andExpect(result -> { |
| 39 | + String body = result.getResponse().getContentAsString(); |
| 40 | + JSONObject jsonObject = JSONObject.parseObject(body); |
| 41 | + log.info("body:{}",jsonObject); |
| 42 | + assertEquals(jsonObject.getString("errCode"),"token.error","token authentication error"); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void haveToken() throws Exception { |
| 48 | + |
| 49 | + JSONObject json = new JSONObject(); |
| 50 | + json.put("username","admin"); |
| 51 | + json.put("password","123456"); |
| 52 | + MvcResult mvcResult = mockMvc.perform(post("/user/login").content(json.toJSONString().getBytes(StandardCharsets.UTF_8)).contentType(MediaType.APPLICATION_JSON)).andReturn(); |
| 53 | + JSONObject loginData = JSONObject.parseObject(mvcResult.getResponse().getContentAsString()); |
12 | 54 |
|
| 55 | + mockMvc.perform(get("/api/hello").header("Authorization",loginData.getJSONObject("data").getString("token")).contentType(MediaType.APPLICATION_JSON)).andExpect(result -> { |
| 56 | + String body = result.getResponse().getContentAsString(); |
| 57 | + assertEquals(body,"hello","token authentication error"); |
| 58 | + }); |
13 | 59 | }
|
14 | 60 |
|
15 | 61 | }
|
0 commit comments