1818
1919import static org .hamcrest .Matchers .containsString ;
2020import static org .hamcrest .Matchers .equalTo ;
21- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
22- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
21+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
22+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
23+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
24+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
25+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
26+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .xpath ;
2327import static org .springframework .test .web .servlet .setup .MockMvcBuilders .standaloneSetup ;
2428
29+ import java .nio .charset .Charset ;
30+ import java .util .Collections ;
31+ import java .util .Map ;
32+
33+ import javax .xml .bind .annotation .XmlRootElement ;
34+
2535import org .junit .Before ;
2636import org .junit .Test ;
37+ import org .springframework .hateoas .Link ;
38+ import org .springframework .hateoas .ResourceSupport ;
2739import org .springframework .http .MediaType ;
2840import org .springframework .stereotype .Controller ;
2941import org .springframework .test .web .servlet .MockMvc ;
4254 */
4355public class ContentAssertionTests {
4456
57+ public static final MediaType TEXT_PLAIN_UTF8 = new MediaType ("text" , "plain" , Charset .forName ("UTF-8" ));
58+
4559 private MockMvc mockMvc ;
4660
4761 @ Before
@@ -51,7 +65,7 @@ public void setup() {
5165
5266 @ Test
5367 public void testContentType () throws Exception {
54- this .mockMvc .perform (get ("/handle" ))
68+ this .mockMvc .perform (get ("/handle" ). accept ( MediaType . TEXT_PLAIN ) )
5569 .andExpect (content ().contentType (MediaType .TEXT_PLAIN ))
5670 .andExpect (content ().contentType ("text/plain" ));
5771
@@ -62,29 +76,58 @@ public void testContentType() throws Exception {
6276
6377 @ Test
6478 public void testContentAsString () throws Exception {
65- this .mockMvc .perform (get ("/handle" )).andExpect (content ().string ("Hello world!" ));
66- this .mockMvc .perform (get ("/handleUtf8" )).andExpect (content ().string ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " ));
79+
80+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .TEXT_PLAIN ))
81+ .andExpect (content ().string ("Hello world!" ));
82+
83+ this .mockMvc .perform (get ("/handleUtf8" ))
84+ .andExpect (content ().string ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " ));
6785
6886 // Hamcrest matchers...
69- this .mockMvc .perform (get ("/handle" )).andExpect (content ().string (equalTo ("Hello world!" )));
87+ this .mockMvc .perform (get ("/handle" ). accept ( MediaType . TEXT_PLAIN ) ).andExpect (content ().string (equalTo ("Hello world!" )));
7088 this .mockMvc .perform (get ("/handleUtf8" )).andExpect (content ().string (equalTo ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " )));
7189 }
7290
7391 @ Test
7492 public void testContentAsBytes () throws Exception {
75- this .mockMvc .perform (get ("/handle" )).andExpect (content ().bytes ("Hello world!" .getBytes ("ISO-8859-1" )));
76- this .mockMvc .perform (get ("/handleUtf8" )).andExpect (content ().bytes ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " .getBytes ("UTF-8" )));
93+
94+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .TEXT_PLAIN ))
95+ .andExpect (content ().bytes ("Hello world!" .getBytes ("ISO-8859-1" )));
96+
97+ this .mockMvc .perform (get ("/handleUtf8" ))
98+ .andExpect (content ().bytes ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " .getBytes ("UTF-8" )));
7799 }
78100
79101 @ Test
80102 public void testContentStringMatcher () throws Exception {
81- this .mockMvc .perform (get ("/handle" )).andExpect (content ().string (containsString ("world" )));
103+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .TEXT_PLAIN ))
104+ .andExpect (content ().string (containsString ("world" )));
82105 }
83106
84107 @ Test
85108 public void testCharacterEncoding () throws Exception {
86- this .mockMvc .perform (get ("/handle" )).andExpect (content ().encoding ("ISO-8859-1" ));
87- this .mockMvc .perform (get ("/handleUtf8" )).andExpect (content ().encoding ("UTF-8" ));
109+
110+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .TEXT_PLAIN ))
111+ .andExpect (content ().encoding ("ISO-8859-1" ))
112+ .andExpect (content ().string (containsString ("world" )));
113+
114+ this .mockMvc .perform (get ("/handleUtf8" ))
115+ .andExpect (content ().encoding ("UTF-8" ))
116+ .andExpect (content ().bytes ("\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " .getBytes ("UTF-8" )));
117+ }
118+
119+ @ Test
120+ public void testSpringHateoasJsonLink () throws Exception {
121+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .APPLICATION_JSON ))
122+ .andExpect (jsonPath ("$.links[?(@.rel == 'self')].href" ).value ("http://myhost/people" ));
123+ }
124+
125+ @ Test
126+ public void testSpringHateoasXmlLink () throws Exception {
127+ Map <String , String > ns = Collections .singletonMap ("ns" , "http://www.w3.org/2005/Atom" );
128+ this .mockMvc .perform (get ("/handle" ).accept (MediaType .APPLICATION_XML ))
129+ .andDo (print ())
130+ .andExpect (xpath ("/person/ns:link[@rel='self']/@href" , ns ).string ("http://myhost/people" ));
88131 }
89132
90133
@@ -102,5 +145,20 @@ public String handle() {
102145 public String handleWithCharset () {
103146 return "\u3053 \u3093 \u306b \u3061 \u306f \u4e16 \u754c \uff01 " ; // "Hello world! (Japanese)
104147 }
148+
149+ @ RequestMapping (value ="/handle" , produces ={"application/json" , "application/xml" })
150+ @ ResponseBody
151+ public PersonResource handleJsonOrXml () {
152+ PersonResource resource = new PersonResource ();
153+ resource .name = "Joe" ;
154+ resource .add (new Link ("http://myhost/people" ));
155+ return resource ;
156+ }
105157 }
158+
159+ @ XmlRootElement (name ="person" )
160+ static class PersonResource extends ResourceSupport {
161+ String name ;
162+ }
163+
106164}
0 commit comments