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

https://github.com/alibaba/fastjson · Java · 167 lines · 129 code · 38 blank · 0 comment · 2 complexity · db752141eee84d0f17f7d7922be91a2b 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.FastJsonpResponseBodyAdvice;
  4. import org.junit.Assert;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.http.MediaType;
  10. import org.springframework.test.context.ContextConfiguration;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. import org.springframework.test.context.web.WebAppConfiguration;
  13. import org.springframework.test.web.servlet.MockMvc;
  14. import org.springframework.test.web.servlet.ResultActions;
  15. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  16. import org.springframework.web.context.WebApplicationContext;
  17. import org.springframework.web.filter.CharacterEncodingFilter;
  18. import java.util.List;
  19. import static junit.framework.TestCase.assertTrue;
  20. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  21. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  22. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  23. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  24. @RunWith(SpringJUnit4ClassRunner.class)
  25. @WebAppConfiguration
  26. @ContextConfiguration({ "classpath*:/config/applicationContext-mvc4.xml" })
  27. public class FastJsonpHttpMessageConverter4Case1Test {
  28. private static final MediaType APPLICATION_JAVASCRIPT = new MediaType("application", "javascript");
  29. @Autowired
  30. private WebApplicationContext wac;
  31. private MockMvc mockMvc;
  32. @Before
  33. public void setup() {
  34. this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) //
  35. .addFilter(new CharacterEncodingFilter("UTF-8", true)) // 设置服务器端返回的字符集为:UTF-8
  36. .build();
  37. }
  38. @Test
  39. public void isInjectComponent() {
  40. wac.getBean(FastJsonpResponseBodyAdvice.class);
  41. }
  42. @Test
  43. public void test1() throws Exception {
  44. JSONObject json = new JSONObject();
  45. json.put("id", 123);
  46. json.put("name", "哈哈哈");
  47. mockMvc.perform(
  48. (post("/fastjson/test1").characterEncoding("UTF-8").content(json.toJSONString())
  49. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  50. }
  51. @Test
  52. public void test1_2() throws Exception {
  53. JSONObject json = new JSONObject();
  54. json.put("id", 123);
  55. json.put("name", "哈哈哈");
  56. ResultActions actions = mockMvc.perform((post("/fastjson/test1?callback=fnUpdateSome").characterEncoding(
  57. "UTF-8").content(json.toJSONString()).contentType(MediaType.APPLICATION_JSON)));
  58. actions.andDo(print());
  59. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT));
  60. String content = actions.andReturn().getResponse().getContentAsString();
  61. assertTrue(content.equals("/**/fnUpdateSome({\"name\":\"哈哈哈\",\"id\":123})")
  62. || content.equals("/**/fnUpdateSome({\"id\":123,\"name\":\"哈哈哈\"})"));
  63. }
  64. @Test
  65. public void test2() throws Exception {
  66. String jsonStr = "[{\"name\":\"p1\",\"sonList\":[{\"name\":\"s1\"}]},{\"name\":\"p2\",\"sonList\":[{\"name\":\"s2\"},{\"name\":\"s3\"}]}]";
  67. mockMvc.perform(
  68. (post("/fastjson/test2").characterEncoding("UTF-8").content(jsonStr)
  69. .contentType(MediaType.APPLICATION_JSON))).andExpect(status().isOk()).andDo(print());
  70. }
  71. @Test
  72. public void test2_2() throws Exception {
  73. String jsonStr = "[{\"name\":\"p1\",\"sonList\":[{\"name\":\"s1\"}]},{\"name\":\"p2\",\"sonList\":[{\"name\":\"s2\"},{\"name\":\"s3\"}]}]";
  74. ResultActions actions = mockMvc.perform((post("/fastjson/test2?jsonp=fnUpdateSome").characterEncoding("UTF-8")
  75. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  76. actions.andDo(print());
  77. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT));
  78. String content = actions.andReturn().getResponse().getContentAsString();
  79. assertTrue(content.equals("/**/fnUpdateSome({\"p1\":1,\"p2\":2})")
  80. || content.equals("/**/fnUpdateSome({\"p2\":2,\"p1\":1})"));
  81. }
  82. @Test
  83. public void test3() throws Exception {
  84. List<Object> list = this.mockMvc.perform(post("/fastjson/test3")).andReturn().getResponse()
  85. .getHeaderValues("Content-Length");
  86. Assert.assertNotEquals(list.size(), 0);
  87. }
  88. @Test
  89. public void test3_2() throws Exception {
  90. ResultActions actions = this.mockMvc.perform(post("/fastjson/test3?jsonp=fnUpdateSome"));
  91. actions.andDo(print());
  92. actions.andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  93. .andExpect(content().string("/**/fnUpdateSome({})"));
  94. }
  95. @Test
  96. public void test4() throws Exception {
  97. String jsonStr = "{\"t\":{\"id\":123,\"name\":\"哈哈哈\"}}";
  98. mockMvc.perform(
  99. (post("/fastjson/test4").characterEncoding("UTF-8").content(jsonStr)
  100. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  101. }
  102. @Test
  103. public void test4_2() throws Exception {
  104. String jsonStr = "{\"t\":{\"id\":123,\"name\":\"哈哈哈\"}}";
  105. ResultActions actions = mockMvc.perform((post("/fastjson/test4?callback=myUpdate").characterEncoding("UTF-8")
  106. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  107. actions.andDo(print());
  108. actions.andExpect(status().isOk())
  109. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  110. .andExpect(content().string("/**/myUpdate(\"{\\\"t\\\":{\\\"id\\\":123,\\\"name\\\":\\\"哈哈哈\\\"}}\")"));
  111. }
  112. @Test
  113. public void test5() throws Exception {
  114. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  115. mockMvc.perform(
  116. (post("/fastjson/test5").characterEncoding("UTF-8").content(jsonStr)
  117. .contentType(MediaType.APPLICATION_JSON))).andDo(print());
  118. }
  119. @Test
  120. public void test5_2() throws Exception {
  121. String jsonStr = "{\"packet\":{\"smsType\":\"USER_LOGIN\"}}";
  122. ResultActions actions = mockMvc.perform((post("/fastjson/test5?callback=myUpdate").characterEncoding("UTF-8")
  123. .content(jsonStr).contentType(MediaType.APPLICATION_JSON)));
  124. actions.andDo(print());
  125. actions.andExpect(status().isOk())
  126. .andExpect(content().contentType(APPLICATION_JAVASCRIPT))
  127. .andExpect(content().string("/**/myUpdate(\"{\\\"packet\\\":{\\\"smsType\\\":\\\"USER_LOGIN\\\"}}\")"));
  128. }
  129. }