Allow responses with non-standard status codes to be documented

Fixes gh-639
This commit is contained in:
Andy Wilkinson
2019-09-12 17:03:21 +01:00
parent d586d9f15b
commit 3597ebc35a
17 changed files with 107 additions and 29 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.restdocs.mockmvc;
import javax.servlet.http.Cookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;
@@ -36,8 +35,8 @@ class MockMvcResponseConverter implements ResponseConverter<MockHttpServletRespo
@Override
public OperationResponse convert(MockHttpServletResponse mockResponse) {
return new OperationResponseFactory().create(HttpStatus.valueOf(mockResponse.getStatus()),
extractHeaders(mockResponse), mockResponse.getContentAsByteArray());
return new OperationResponseFactory().create(mockResponse.getStatus(), extractHeaders(mockResponse),
mockResponse.getContentAsByteArray());
}
private HttpHeaders extractHeaders(MockHttpServletResponse response) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -61,4 +61,13 @@ public class MockMvcResponseConverterTests {
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
}
@Test
public void responseWithCustomStatus() {
MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(600);
OperationResponse operationResponse = this.factory.convert(response);
assertThat(operationResponse.getStatus()).isNull();
assertThat(operationResponse.getStatusCode()).isEqualTo(600);
}
}