1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
18
19 import java.nio.charset.Charset;
20
21 import org.junit.Before;
22 import org.junit.Test;
23
24 import org.springframework.http.MediaType;
25 import org.springframework.stereotype.Controller;
26 import org.springframework.test.web.servlet.MockMvc;
27 import org.springframework.web.bind.annotation.RequestMapping;
28 import org.springframework.web.bind.annotation.ResponseBody;
29
30 import static org.hamcrest.Matchers.*;
31 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
33 import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
34
35
36
37
38
39
40
41
42
43
44
45 public class ContentAssertionTests {
46
47 public static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", Charset.forName("UTF-8"));
48
49 private MockMvc mockMvc;
50
51 @Before
52 public void setup() {
53 this.mockMvc = standaloneSetup(new SimpleController()).alwaysExpect(status().isOk()).build();
54 }
55
56 @Test
57 public void testContentType() throws Exception {
58 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
59 .andExpect(content().contentType(MediaType.TEXT_PLAIN))
60 .andExpect(content().contentType("text/plain"));
61
62 this.mockMvc.perform(get("/handleUtf8"))
63 .andExpect(content().contentType(MediaType.valueOf("text/plain;charset=UTF-8")))
64 .andExpect(content().contentType("text/plain;charset=UTF-8"))
65 .andExpect(content().contentTypeCompatibleWith("text/plain"))
66 .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN));
67 }
68
69 @Test
70 public void testContentAsString() throws Exception {
71
72 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
73 .andExpect(content().string("Hello world!"));
74
75 this.mockMvc.perform(get("/handleUtf8"))
76 .andExpect(content().string("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01"));
77
78
79 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN)).andExpect(content().string(equalTo("Hello world!")));
80 this.mockMvc.perform(get("/handleUtf8")).andExpect(content().string(equalTo("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01")));
81 }
82
83 @Test
84 public void testContentAsBytes() throws Exception {
85
86 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
87 .andExpect(content().bytes("Hello world!".getBytes("ISO-8859-1")));
88
89 this.mockMvc.perform(get("/handleUtf8"))
90 .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
91 }
92
93 @Test
94 public void testContentStringMatcher() throws Exception {
95 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
96 .andExpect(content().string(containsString("world")));
97 }
98
99 @Test
100 public void testCharacterEncoding() throws Exception {
101
102 this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
103 .andExpect(content().encoding("ISO-8859-1"))
104 .andExpect(content().string(containsString("world")));
105
106 this.mockMvc.perform(get("/handleUtf8"))
107 .andExpect(content().encoding("UTF-8"))
108 .andExpect(content().bytes("\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01".getBytes("UTF-8")));
109 }
110
111
112 @Controller
113 private static class SimpleController {
114
115 @RequestMapping(value="/handle", produces="text/plain")
116 @ResponseBody
117 public String handle() {
118 return "Hello world!";
119 }
120
121 @RequestMapping(value="/handleUtf8", produces="text/plain;charset=UTF-8")
122 @ResponseBody
123 public String handleWithCharset() {
124 return "\u3053\u3093\u306b\u3061\u306f\u4e16\u754c\uff01";
125 }
126 }
127
128 }