云
8 小时以前 7db504cbc6d08e50666c86a3dbd6c7da5147b630
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cn.iocoder.yudao.framework.web.core.handler;
 
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 
import java.io.IOException;
 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
class GlobalExceptionHandlerClientAbortTest {
 
    @Test
    void doesNotSerializeJsonAfterZipResponseBecomesUnusable() throws Exception {
        MockMvcBuilders.standaloneSetup(new ZipController())
                .setControllerAdvice(new GlobalExceptionHandler("test", null))
                .build()
                .perform(get("/zip"))
                .andExpect(status().isOk())
                .andExpect(content().bytes(new byte[0]));
    }
 
    @RestController
    static class ZipController {
 
        @GetMapping("/zip")
        public void download(HttpServletResponse response) throws IOException {
            response.setContentType("application/zip");
            response.flushBuffer();
            throw new AsyncRequestNotUsableException("client disconnected");
        }
 
    }
 
}