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

https://github.com/alibaba/fastjson · Java · 190 lines · 150 code · 40 blank · 0 comment · 2 complexity · d310e692bf7f356357ab626c392fc9ff MD5 · raw file

  1. package com.alibaba.json.bvt.support.spring.mock.testcase;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4;
  4. import com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice;
  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 junit.framework.TestCase.assertTrue;
  27. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  28. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  29. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  30. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  31. @RunWith(SpringJUnit4ClassRunner.class)
  32. @WebAppConfiguration
  33. @ContextConfiguration
  34. public class FastJsonpHttpMessageConverter4Case2Test {
  35. private static final MediaType APPLICATION_JAVASCRIPT = new MediaType("application", "javascript");
  36. @Autowired
  37. private WebApplicationContext wac;
  38. private MockMvc mockMvc;
  39. @ComponentScan(basePackages = "com.alibaba.json.bvt.support.spring.mock.controller")
  40. @EnableWebMvc
  41. @Configuration
  42. protected static class Config extends WebMvcConfigurerAdapter {
  43. @Bean
  44. public FastJsonpResponseBodyAdvice fastJsonpResponseBodyAdvice() {
  45. return new FastJsonpResponseBodyAdvice("callback", "jsonp");
  46. }
  47. @Override
  48. public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  49. converters.add(0, new FastJsonpHttpMessageConverter4());
  50. super.extendMessageConverters(converters);
  51. }
  52. }
  53. @Before
  54. public void setup() {
  55. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) //
  56. .addFilter(new CharacterEncodingFilter("UTF-8", true)) // 设置服务器端返回的字符集为:UTF-8
  57. .build();
  58. }
  59. @Test
  60. public void isInjectComponent() {
  61. wac.getBean(FastJsonpResponseBodyAdvice.class);
  62. }
  63. @Test
  64. public void test1() throws Exception {
  65. JSONObject json = new JSONObject();
  66. json.put("id", 123);
  67. json.put("name", "哈哈哈");
  68. mockMvc.perform(
  69. (post("/fastjson/test1").characterEncoding("UTF-8").content(json.toJSONString())
  70. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  71. }
  72. @Test
  73. public void test1_2() throws Exception {
  74. JSONObject json = new JSONObject();
  75. json.put("id", 123);
  76. json.put("name", "哈哈哈");
  77. ResultActions actions = mockMvc.perform((post("/fastjson/test1?callback=fnUpdateSome").characterEncoding(
  78. "UTF-8").content(json.toJSONString()).contentType(MediaType.APPLICATION_JSON)));
  79. actions.andDo(print());
  80. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT));
  81. String content = actions.andReturn().getResponse().getContentAsString();
  82. assertTrue(content.equals("/**/fnUpdateSome({\"name\":\"哈哈哈\",\"id\":123})")
  83. || content.equals("/**/fnUpdateSome({\"id\":123,\"name\":\"哈哈哈\"})"));
  84. }
  85. @Test
  86. public void test2() throws Exception {
  87. String jsonStr = "[{\"name\":\"p1\",\"sonList\":[{\"name\":\"s1\"}]},{\"name\":\"p2\",\"sonList\":[{\"name\":\"s2\"},{\"name\":\"s3\"}]}]";
  88. mockMvc.perform(
  89. (post("/fastjson/test2").characterEncoding("UTF-8").content(jsonStr)
  90. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  91. }
  92. @Test
  93. public void test2_2() throws Exception {
  94. String jsonStr = "[{\"name\":\"p1\",\"sonList\":[{\"name\":\"s1\"}]},{\"name\":\"p2\",\"sonList\":[{\"name\":\"s2\"},{\"name\":\"s3\"}]}]";
  95. ResultActions actions = mockMvc.perform((post("/fastjson/test2?jsonp=fnUpdateSome").characterEncoding("UTF-8")
  96. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  97. actions.andDo(print());
  98. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT));
  99. String content = actions.andReturn().getResponse().getContentAsString();
  100. assertTrue(content.equals("/**/fnUpdateSome({\"p1\":1,\"p2\":2})")
  101. || content.equals("/**/fnUpdateSome({\"p2\":2,\"p1\":1})"));
  102. }
  103. @Test
  104. public void test3() throws Exception {
  105. List<Object> list = this.mockMvc.perform(post("/fastjson/test3")).andReturn().getResponse()
  106. .getHeaderValues("Content-Length");
  107. Assert.assertNotEquals(list.size(), 0);
  108. }
  109. @Test
  110. public void test3_2() throws Exception {
  111. ResultActions actions = this.mockMvc.perform(post("/fastjson/test3?jsonp=fnUpdateSome"));
  112. actions.andDo(print());
  113. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  114. .andExpect(content().string("/**/fnUpdateSome({})"));
  115. }
  116. @Test
  117. public void test4() throws Exception {
  118. String jsonStr = "{\"t\":{\"id\":123,\"name\":\"哈哈哈\"}}";
  119. mockMvc.perform(
  120. (post("/fastjson/test4").characterEncoding("UTF-8").content(jsonStr)
  121. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  122. }
  123. @Test
  124. public void test4_2() throws Exception {
  125. String jsonStr = "{\"t\":{\"id\":123,\"name\":\"哈哈哈\"}}";
  126. ResultActions actions = mockMvc.perform((post("/fastjson/test4?callback=myUpdate").characterEncoding("UTF-8")
  127. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  128. actions.andDo(print());
  129. actions.andExpect(status().isOk())
  130. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  131. .andExpect(content().string("/**/myUpdate(\"{\\\"t\\\":{\\\"id\\\":123,\\\"name\\\":\\\"哈哈哈\\\"}}\")"));
  132. }
  133. @Test
  134. public void test5() throws Exception {
  135. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  136. mockMvc.perform(
  137. (post("/fastjson/test5").characterEncoding("UTF-8").content(jsonStr)
  138. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  139. }
  140. @Test
  141. public void test5_2() throws Exception {
  142. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  143. ResultActions actions = mockMvc.perform((post("/fastjson/test5?callback=myUpdate").characterEncoding("UTF-8")
  144. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  145. actions.andDo(print());
  146. actions.andExpect(status().isOk())
  147. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  148. .andExpect(content().string("/**/myUpdate(\"{\\\"packet\\\":{\\\"smsType\\\":\\\"USER_LOGIN\\\"}}\")"));
  149. }
  150. }