我使用当前版本的 spring-restdoc:1.1.2.RELEASE。
我创建了一个需要在标头中添加 cookie 的 API。这是我的控制器:
@RequestMapping(value = "/{username}/userInfo", produces = {APPLICATION_JSON_UTF8_VALUE}, method = RequestMethod.GET)
public ResponseEntity<UserInfo> getUserInfo(@CookieValue("identity") String cookie,
@PathVariable(value = "username") String username) {
LOGGER.info("[getUserInfo] user = {}", username);
ResponseEntity<UserInfo> responseEntity = new ResponseEntity<>(userService.findUserInfos(cookie, username), HttpStatus.OK);
LOGGER.debug("[getUserInfo] response = {}", responseEntity.toString());
return responseEntity;
}
在我为 getUserInfos 的文档编写测试之后。这是我的测试:
@Test
public void getUserInfoOK() throws Exception {
when(userService.findUserInfos(cookieCaptor.capture(), userNameCaptor.capture())).thenReturn(
createUserInfo());
Cookie cookie = new Cookie("identity", "cookieForTest");
UriBuilder builder = UriBuilder.fromPath("").path("/v1/users/{username}/userInfo");
URI uri = builder.build("userTest");
MockHttpServletRequestBuilder request = RestDocumentationRequestBuilders.get("http://localhost:8070" + "/api/user" + "/v1/users/{username}/userInfo", "userTest")
.header("Host", "localhost")
.cookie(cookie)
.accept(MediaType.APPLICATION_JSON_UTF8)
.servletPath(uri.toString()).contextPath("/api/user");
mockMvc.perform(request)
.andDo(print())
.andDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()),
pathParameters(
parameterWithName("username").description("The username's user")),
requestHeaders(
headerWithName("Cookie").description(
"The cookie that identifies the user"))
))
.andExpect(MockMvcResultMatchers.jsonPath("$.bouquets", hasSize(3)))
.andExpect(status().isOk());
//verify userService.findUserInfos is called
verify(userService).findUserInfos(anyString(), anyString());
// verify the parameters
assertEquals("userTest", userNameCaptor.getValue());
assertEquals("cookieForTest", cookieCaptor.getValue());
}
当我运行生成文档的测试时,出现以下错误:
org.springframework.restdocs.snippet.SnippetException: Headers with the following names were not found in the request: [Cookie]
at org.springframework.restdocs.headers.AbstractHeadersSnippet.validateHeaderDocumentation(AbstractHeadersSnippet.java:83)
at org.springframework.restdocs.headers.AbstractHeadersSnippet.createModel(AbstractHeadersSnippet.java:65)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.orange.otml.otmluser.controller.UsersControllerTest.getUserInfoOK(UsersControllerTest.java:183)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.restdocs.JUnitRestDocumentation$1.evaluate(JUnitRestDocumentation.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
spring-restdoc 如何管理 cookie ?如何使用 spring-restdoc 记录身份 cookie?
我没有在 spring-restdoc 中找到为 cookie 保留的单词。
谁能帮我 ?