/src/test/java/com/alibaba/json/bvt/support/spring/mock/testcase/FastJsonHttpMessageConverterJSONPCaseTest.java

https://github.com/alibaba/fastjson · Java · 216 lines · 173 code · 43 blank · 0 comment · 0 complexity · 14aa722d501acde598f87c0086ee5128 MD5 · raw file

  1. package com.alibaba.json.bvt.support.spring.mock.testcase;
  2. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  3. import com.alibaba.fastjson.support.spring.FastJsonViewResponseBodyAdvice;
  4. import com.alibaba.fastjson.support.spring.JSONPResponseBodyAdvice;
  5. import org.junit.Assert;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.ComponentScan;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.http.converter.HttpMessageConverter;
  15. import org.springframework.test.context.ContextConfiguration;
  16. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  17. import org.springframework.test.context.web.WebAppConfiguration;
  18. import org.springframework.test.web.servlet.MockMvc;
  19. import org.springframework.test.web.servlet.ResultActions;
  20. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  21. import org.springframework.web.context.WebApplicationContext;
  22. import org.springframework.web.filter.CharacterEncodingFilter;
  23. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  24. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  25. import java.util.List;
  26. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  27. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  28. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  29. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  30. @RunWith(SpringJUnit4ClassRunner.class)
  31. @WebAppConfiguration
  32. @ContextConfiguration
  33. public class FastJsonHttpMessageConverterJSONPCaseTest {
  34. private static final MediaType APPLICATION_JAVASCRIPT = new MediaType("application", "javascript");
  35. @Autowired
  36. private WebApplicationContext wac;
  37. private MockMvc mockMvc;
  38. @ComponentScan(basePackages = "com.alibaba.json.bvt.support.spring.mock.controller")
  39. @EnableWebMvc
  40. @Configuration
  41. protected static class Config extends WebMvcConfigurerAdapter {
  42. @Bean
  43. public JSONPResponseBodyAdvice jsonpResponseBodyAdvice() {
  44. return new JSONPResponseBodyAdvice();
  45. }
  46. @Bean
  47. FastJsonViewResponseBodyAdvice fastJsonViewResponseBodyAdvice() {
  48. return new FastJsonViewResponseBodyAdvice();
  49. }
  50. @Override
  51. public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  52. converters.add(0, new FastJsonHttpMessageConverter());
  53. super.extendMessageConverters(converters);
  54. }
  55. }
  56. @Before
  57. public void setup() {
  58. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) //
  59. .addFilter(new CharacterEncodingFilter("UTF-8", true)) // 设置服务器端返回的字符集为:UTF-8
  60. .build();
  61. }
  62. @Test
  63. public void isInjectComponent() {
  64. wac.getBean(JSONPResponseBodyAdvice.class);
  65. wac.getBean(FastJsonViewResponseBodyAdvice.class);
  66. }
  67. @Test
  68. public void test1() throws Exception {
  69. mockMvc.perform(
  70. (post("/jsonp-fastjsonview/test1").characterEncoding("UTF-8")
  71. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  72. }
  73. @Test
  74. public void test1_2() throws Exception {
  75. ResultActions actions = mockMvc.perform((post("/jsonp-fastjsonview/test1?callback=fnUpdateSome").characterEncoding(
  76. "UTF-8").contentType(MediaType.APPLICATION_JSON)));
  77. actions.andDo(print());
  78. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  79. .andExpect(content().string("/**/fnUpdateSome({\"id\":100,\"name\":\"测试\"})"));
  80. }
  81. @Test
  82. public void test2() throws Exception {
  83. mockMvc.perform(
  84. (post("/jsonp-fastjsonview/test2").characterEncoding("UTF-8")
  85. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  86. }
  87. @Test
  88. public void test2_2() throws Exception {
  89. ResultActions actions = mockMvc.perform((post("/jsonp-fastjsonview/test2?callback=fnUpdateSome").characterEncoding("UTF-8")
  90. .contentType(MediaType.APPLICATION_JSON)));
  91. actions.andDo(print());
  92. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  93. .andExpect(content().string("/**/fnUpdateSome({\"description\":\"fastjsonview注解测试\",\"stock\":\"haha\"})"));
  94. }
  95. @Test
  96. public void test3() throws Exception {
  97. List<Object> list = this.mockMvc.perform(post("/jsonp-fastjsonview/test3")).andReturn().getResponse()
  98. .getHeaderValues("Content-Length");
  99. Assert.assertNotEquals(list.size(), 0);
  100. }
  101. @Test
  102. public void test3_Jsonp_ContentLength() throws Exception{
  103. ResultActions actions1 = this.mockMvc.perform(post("/jsonp-fastjsonview/test3?callback=func")).andDo(print());
  104. Object obj1 = actions1.andReturn().getResponse().getHeaderValue("Content-Length");
  105. Assert.assertNotNull(obj1);
  106. Assert.assertEquals(85,obj1);
  107. ResultActions actions2 = this.mockMvc.perform(post("/jsonp-fastjsonview/test3?callback=fnUpdateSome")).andDo(print());
  108. Object obj2 = actions2.andReturn().getResponse().getHeaderValue("Content-Length");
  109. Assert.assertNotNull(obj2);
  110. Assert.assertEquals(93,obj2);
  111. }
  112. @Test
  113. public void test3_2() throws Exception {
  114. ResultActions actions = this.mockMvc.perform(post("/jsonp-fastjsonview/test3?callback=fnUpdateSome"));
  115. actions.andDo(print());
  116. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  117. .andExpect(content().string("/**/fnUpdateSome({\"id\":100,\"name\":\"测试\",\"rootDepartment\":{\"description\":\"部门1描述\"}})"));
  118. }
  119. @Test
  120. public void test4() throws Exception {
  121. mockMvc.perform(
  122. (post("/jsonp-fastjsonview/test4").characterEncoding("UTF-8")
  123. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  124. }
  125. @Test
  126. public void test4_2() throws Exception {
  127. ResultActions actions = mockMvc.perform((post("/jsonp-fastjsonview/test4?callback=myUpdate").characterEncoding("UTF-8")
  128. .contentType(MediaType.APPLICATION_JSON)));
  129. actions.andDo(print());
  130. actions.andExpect(status().isOk())
  131. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  132. .andExpect(content().string("/**/myUpdate({\"id\":100,\"name\":\"测试\",\"rootDepartment\":{\"id\":1,\"members\":[],\"name\":\"部门1\"}})"));
  133. }
  134. @Test
  135. public void test5() throws Exception {
  136. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  137. mockMvc.perform(
  138. (post("/jsonp-fastjsonview/test5").characterEncoding("UTF-8").content(jsonStr)
  139. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  140. }
  141. @Test
  142. public void test5_2() throws Exception {
  143. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  144. ResultActions actions = mockMvc.perform((post("/jsonp-fastjsonview/test5?callback=myUpdate").characterEncoding("UTF-8")
  145. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  146. actions.andDo(print());
  147. actions.andExpect(status().isOk())
  148. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  149. .andExpect(content().string("/**/myUpdate(\"{\\\"packet\\\":{\\\"smsType\\\":\\\"USER_LOGIN\\\"}}\")"));
  150. }
  151. @Test
  152. public void test7() throws Exception {
  153. ResultActions actions = this.mockMvc.perform(post("/jsonp-fastjsonview/test7?customizedCallbackParamName=fnUpdateSome"));
  154. actions.andDo(print());
  155. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  156. .andExpect(content().string("/**/fnUpdateSome({})"));
  157. }
  158. @Test
  159. public void test8() throws Exception {
  160. String invalidMethodName = "--methodName";
  161. ResultActions actions = this.mockMvc.perform(post("/jsonp-fastjsonview/test7?customizedCallbackParamName=" + invalidMethodName));
  162. actions.andDo(print());
  163. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  164. .andExpect(content().string("/**/null({})"));
  165. }
  166. @Test
  167. public void test9() throws Exception {
  168. ResultActions actions = this.mockMvc.perform(post("/jsonp-fastjsonview/test9?callback=fnUpdateSome"));
  169. actions.andDo(print());
  170. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  171. .andExpect(content().string("/**/fnUpdateSome({\"id\":100})"));
  172. }
  173. }