Polish contribution that added support for documenting HTTP headers

The main changes are:

- Update javadoc to align with changes made in 5a01016
- Update documentation with details of the new support for documenting
  HTTP headers
- Add tests to verify that HTTP headers are matched case-insensitively

Closes gh-71
This commit is contained in:
Andy Wilkinson
2015-09-29 12:31:04 +01:00
parent 48f4d1343a
commit ea8f736259
11 changed files with 175 additions and 92 deletions

View File

@@ -91,8 +91,8 @@ public abstract class AbstractHeadersSnippet extends TemplatedSnippet {
}
/**
* Finds the headers that are missing from the operation. A header is missing if
* it is described by one of the {@code headerDescriptors} but is not present in the
* Finds the headers that are missing from the operation. A header is missing if it is
* described by one of the {@code headerDescriptors} but is not present in the
* operation.
*
* @param operation the operation
@@ -100,9 +100,10 @@ public abstract class AbstractHeadersSnippet extends TemplatedSnippet {
*/
protected List<HeaderDescriptor> findMissingHeaders(Operation operation) {
List<HeaderDescriptor> missingHeaders = new ArrayList<HeaderDescriptor>();
Set<String> actualHeaders = extractActualHeaders(operation);
for (HeaderDescriptor headerDescriptor : this.headerDescriptors) {
if (!headerDescriptor.isOptional()
&& !getHeaders(operation).contains(headerDescriptor.getName())) {
&& !actualHeaders.contains(headerDescriptor.getName())) {
missingHeaders.add(headerDescriptor);
}
}
@@ -111,13 +112,13 @@ public abstract class AbstractHeadersSnippet extends TemplatedSnippet {
}
/**
* Returns the headers of the request or response extracted form the given
* Extracts the names of the headers from the request or response of the given
* {@code operation}.
*
* @param operation The operation
* @return The headers
* @param operation the operation
* @return the header names
*/
protected abstract Set<String> getHeaders(Operation operation);
protected abstract Set<String> extractActualHeaders(Operation operation);
/**
* Returns the list of {@link HeaderDescriptor HeaderDescriptors} that will be used to

View File

@@ -56,7 +56,7 @@ public class HeaderDescriptor extends AbstractDescriptor<HeaderDescriptor> {
*
* @return the header name
*/
public String getName() {
public final String getName() {
return this.name;
}

View File

@@ -25,6 +25,7 @@ import org.springframework.restdocs.snippet.Snippet;
* Static factory methods for documenting a RESTful API's request and response headers.
*
* @author Andreas Evers
* @author Andy Wilkinson
*/
public abstract class HeaderDocumentation {
@@ -44,15 +45,14 @@ public abstract class HeaderDocumentation {
}
/**
* Returns a handler that will produce a snippet documenting the headers of the API
* call's request.
* Returns a new {@link Snippet} that will document the headers of the API operation's
* request. The headers will be documented using the given {@code descriptors}.
* <p>
* If a header is documented, is not marked as optional, and is not present in the
* request, a failure will occur. If a header is present in the request, but is not
* documented by one of the descriptors, there will be no failure.
* request, a failure will occur.
*
* @param descriptors The descriptions of the request's headers
* @return the handler
* @param descriptors the descriptions of the request's headers
* @return the snippet that will document the request headers
* @see #headerWithName(String)
*/
public static Snippet requestHeaders(HeaderDescriptor... descriptors) {
@@ -60,17 +60,16 @@ public abstract class HeaderDocumentation {
}
/**
* Returns a handler that will produce a snippet documenting the headers of the API
* call's request. The given {@code attributes} will be available during snippet
* generation.
* Returns a new {@link Snippet} that will document the headers of the API
* operations's request. The given {@code attributes} will be available during snippet
* generation and the headers will be documented using the given {@code descriptors}.
* <p>
* If a header is documented, is not marked as optional, and is not present in the
* request, a failure will occur. If a header is present in the request, but is not
* documented by one of the descriptors, there will be no failure.
* request, a failure will occur.
*
* @param attributes Attributes made available during rendering of the snippet
* @param descriptors The descriptions of the request's headers
* @return the handler
* @param attributes the attributes
* @param descriptors the descriptions of the request's headers
* @return the snippet that will document the request headers
* @see #headerWithName(String)
*/
public static Snippet requestHeaders(Map<String, Object> attributes,
@@ -79,15 +78,14 @@ public abstract class HeaderDocumentation {
}
/**
* Returns a handler that will produce a snippet documenting the headers of the API
* call's response.
* Returns a new {@link Snippet} that will document the headers of the API operation's
* response. The headers will be documented using the given {@code descriptors}.
* <p>
* If a header is documented, is not marked as optional, and is not present in the
* response, a failure will occur. If a header is present in the response, but is not
* documented by one of the descriptors, there will be no failure.
* request, a failure will occur.
*
* @param descriptors The descriptions of the response's headers
* @return the handler
* @param descriptors the descriptions of the response's headers
* @return the snippet that will document the response headers
* @see #headerWithName(String)
*/
public static Snippet responseHeaders(HeaderDescriptor... descriptors) {
@@ -95,17 +93,17 @@ public abstract class HeaderDocumentation {
}
/**
* Returns a handler that will produce a snippet documenting the headers of the API
* call's response. The given {@code attributes} will be available during snippet
* generation.
* Returns a new {@link Snippet} that will document the headers of the API
* operations's response. The given {@code attributes} will be available during
* snippet generation and the headers will be documented using the given
* {@code descriptors}.
* <p>
* If a header is documented, is not marked as optional, and is not present in the
* response, a failure will occur. If a header is present in the response, but is not
* documented by one of the descriptors, there will be no failure.
* response, a failure will occur.
*
* @param attributes Attributes made available during rendering of the snippet
* @param descriptors The descriptions of the response's headers
* @return the handler
* @param attributes the attributes
* @param descriptors the descriptions of the response's headers
* @return the snippet that will document the response headers
* @see #headerWithName(String)
*/
public static Snippet responseHeaders(Map<String, Object> attributes,

View File

@@ -56,7 +56,7 @@ public class RequestHeadersSnippet extends AbstractHeadersSnippet {
}
@Override
protected Set<String> getHeaders(Operation operation) {
protected Set<String> extractActualHeaders(Operation operation) {
return operation.getRequest().getHeaders().keySet();
}

View File

@@ -56,7 +56,7 @@ public class ResponseHeadersSnippet extends AbstractHeadersSnippet {
}
@Override
protected Set<String> getHeaders(Operation operation) {
protected Set<String> extractActualHeaders(Operation operation) {
return operation.getResponse().getHeaders().keySet();
}