2016-09-13 1 views
3

현재 버전의 spring-restdoc : 1.1.2.RELEASE를 사용합니다.spring-restdoc을 사용하여 쿠키를 문서화하려면 어떻게해야합니까?

헤더에 쿠키가 필요한 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) 

봄 - restdoc 쿠키를 관리하는 방법을? spring-restdoc을 사용하여 신원 쿠키를 문서화하는 방법은 무엇입니까?

spring-restdoc에서 쿠키 용으로 예약 된 단어를 찾을 수 없습니다.

아무도 도와 줄 수 있습니까?

답변

1

현재 Spring REST Docs에 쿠키를 문서화하는 것은 지원되지 않습니다. 당신은 내가 그것을 알고있는 첫 번째 사람입니다. 지원하는 것이 합리적인 것 같아서 몇 가지 개선 사항을 열었습니다. This one이 아마도 귀하의 질문과 가장 관련이 있습니다. 두 가지 접근 방식에 대한 의견이 있거나 다른 접근 방식을 제안하고자하는 경우 문제에 대해 의견을 말하십시오.

+0

신속한 답변을 부탁드립니다. 프로젝트 github에 내 의견을 게시합니다. –

+0

정확히 동일한 기능을 찾고있었습니다. – dvtoever

관련 문제