StudentController { @ApiOperation(value = "获取Cookie", notes = "跨域设置Cookie") @RequestMapping(value = "", method = RequestMethod.POST) public ResultVO crossDomainSetCookie(HttpServletRequest request, HttpServletResponse response, String name, String value) { response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); response.setHeader("Access-Control-Max-Age", "0"); String allowHeaders = "Origin, No-Cache, X-Requested-With, If-Modified-Since,Pragma, Last-Modified, " + "Cache-Control,Expires, Content-Type,X-E4M-With,userId,token,Access-Control-Allow-Headers"; response.setHeader("Access-Control-Allow-Headers", allowHeaders); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("XDomainRequestAllowed", "1"); response.setHeader("Content-Type", "application/json;charset=UTF-8"); Cookie cookie = new Cookie(name, value); cookie.setMaxAge(0); cookie.setHttpOnly(true); cookie.setPath("/student"); cookie.setDomain("zsm.com"); cookie.setVersion(1); cookie.setComment("测试设置cookie"); cookie.setSecure(true); response.addCookie(cookie); Cookie[] cookies = request.getCookies(); for (Cookie c : cookies) { System.out.println("CookieName: " + c.getName() + " CookieValue: " + c.getValue()); } HttpSession session = request.getSession(); Enumeration<String> attributeNames = session.getAttributeNames(); while (attributeNames.hasMoreElements()) { String element = attributeNames.nextElement(); System.out.println(element); } try { String authType = request.getAuthType(); System.out.println("auth type is:" + authType); } catch (Exception e) { System.out.println("exception message:" + e.getMessage()); } int status = response.getStatus(); System.out.println("the response status is:" + status); return ResultVO.succes(request.getParameter("data")); }  @ApiOperation(value = "根据username查找", notes = "查询数据库中某个的用户信息") @ApiImplicitParam(name = "name", value = "用户名字", paramType = "path", required = true, dataType = "String", example = "李晓明") @RequestMapping("find/{name}") String selectTestInfo(@PathVariable String name); @RequestMapping(value = "userLogin", method = RequestMethod.POST) @ResponseBody String userLogin(Student student, Model model); @ApiIgnore @RequestMapping(value = "post", method = RequestMethod.POST) String postRequest(HttpServletRequest request, HttpServletResponse response); @ApiIgnore @RequestMapping(value = "get", method = RequestMethod.GET) String getRequest(); @RequestMapping(value = "toLogin", method = RequestMethod.POST) @ResponseBody String signingIn(HttpServletRequest request, HttpServletResponse response); @ApiOperation(value = "根据username查找", notes = "查询数据库中某个的用户信息") @ApiImplicitParam(name = "name", value = "李晓明", paramType = "path", required = true, dataType = "String") @RequestMapping(value = "json/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) ResultVO userJSONInfo(@PathVariable String name); @ApiOperation(value = "根据username查找", notes = "查询数据库中某个的用户信息") @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "李晓明", paramType = "path", required = true, dataType = "String")) @RequestMapping(value = "xml/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE) ResultVO userXMLInfo(@PathVariable String name); @ApiOperation(value = "获取Cookie", notes = "跨域设置Cookie") @RequestMapping(value = "", method = RequestMethod.POST) ResultVO crossDomainSetCookie(HttpServletRequest request, HttpServletResponse response,
                                         String name, String value); @ApiOperation("用户信息提交") @RequestMapping(value = "putUserInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParams({@ApiImplicitParam(name = "studentInfo", value = "JSONObject", required = true)}) ResultVO sendKafka(@RequestBody JSONObject studentInfo); @ApiOperation(value = "用户列表查询", notes = "分页查询数据库中用户信息，模糊匹配") @GetMapping("list/{name}/{pageNum}/{pageSize}") ResultVO getUserInfoList(@RequestParam(value = "name") @ApiParam("模糊查询用户名") @PathVariable String name,
                                    @RequestParam(value = "pageNum", defaultValue = "1") @ApiParam("查询页编号")
                                     @PathVariable Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") @ApiParam("查询页数据条数")
                                     @PathVariable Integer pageSize);  }
@Test public void crossDomainSetCookie() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getHeader("Origin")).thenReturn("127.0.0.1"); Mockito.when(request.getParameter(any())).thenReturn("test"); Cookie[] cookies = {new Cookie("Login", "true")}; Mockito.when(request.getCookies()).thenReturn(cookies); HttpSession session = Mockito.mock(HttpSession.class); Enumeration<String> en = Mockito.mock(Enumeration.class); Mockito.when(en.hasMoreElements()).thenReturn(true).thenReturn(true).thenReturn(false); Mockito.when(en.nextElement()).thenReturn("login").thenReturn("loginFlag"); Mockito.when(session.getAttributeNames()).thenReturn(en); Mockito.when(request.getSession()).thenReturn(session); doThrow(new IllegalArgumentException("nothing auth type!")).when(request).getAuthType(); HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(response.getStatus()).thenReturn(1); String value = "e10adc3949ba59abbe56e057f20f883e"; ResultVO msg = studentController.crossDomainSetCookie(request, response, "test", value); Assert.assertEquals("test", msg.getData()); }