Merge pull request #653 from flo076
* gh-653: Polish "Fix NPE in HttpResponseSnippet when response has custom status" Fix NPE in HttpResponseSnippet when response has custom status Closes gh-653
This commit is contained in:
@@ -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.
|
||||
@@ -56,12 +56,18 @@ public class HttpResponseSnippet extends TemplatedSnippet {
|
||||
@Override
|
||||
protected Map<String, Object> createModel(Operation operation) {
|
||||
OperationResponse response = operation.getResponse();
|
||||
HttpStatus status = response.getStatus();
|
||||
Map<String, Object> model = new HashMap<>();
|
||||
model.put("responseBody", responseBody(response));
|
||||
model.put("statusCode", status.value());
|
||||
model.put("statusReason", status.getReasonPhrase());
|
||||
model.put("headers", headers(response));
|
||||
HttpStatus status = response.getStatus();
|
||||
if (status != null) {
|
||||
model.put("statusCode", status.value());
|
||||
model.put("statusReason", status.getReasonPhrase());
|
||||
}
|
||||
else {
|
||||
model.put("statusCode", response.getStatusCode());
|
||||
model.put("statusReason", "");
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,10 @@ public abstract class AbstractSnippetTests {
|
||||
return SnippetConditions.httpResponse(this.templateFormat, responseStatus);
|
||||
}
|
||||
|
||||
public HttpResponseCondition httpResponse(int responseStatusCode) {
|
||||
return SnippetConditions.httpResponse(this.templateFormat, responseStatusCode, "");
|
||||
}
|
||||
|
||||
protected FileSystemResource snippetResource(String name) {
|
||||
return new FileSystemResource(
|
||||
"src/test/resources/custom-snippet-templates/" + this.templateFormat.getId() + "/" + name + ".snippet");
|
||||
|
||||
@@ -97,4 +97,10 @@ public class HttpResponseSnippetTests extends AbstractSnippetTests {
|
||||
assertThat(this.generatedSnippets.httpResponse()).contains("Title for the response");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void responseWithCustomStatus() throws IOException {
|
||||
new HttpResponseSnippet().document(this.operationBuilder.response().status(215).build());
|
||||
assertThat(this.generatedSnippets.httpResponse()).is(httpResponse(215));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -71,6 +71,16 @@ public final class SnippetConditions {
|
||||
return new HttpResponseCondition(status, new MarkdownCodeBlockCondition<>("http"), 2);
|
||||
}
|
||||
|
||||
public static HttpResponseCondition httpResponse(TemplateFormat format, Integer responseStatusCode,
|
||||
String responseStatusReason) {
|
||||
if ("adoc".equals(format.getFileExtension())) {
|
||||
return new HttpResponseCondition(responseStatusCode, responseStatusReason,
|
||||
new AsciidoctorCodeBlockCondition<>("http", "nowrap"), 3);
|
||||
}
|
||||
return new HttpResponseCondition(responseStatusCode, responseStatusReason,
|
||||
new MarkdownCodeBlockCondition<>("http"), 2);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public static CodeBlockCondition<?> codeBlock(TemplateFormat format, String language) {
|
||||
if ("adoc".equals(format.getFileExtension())) {
|
||||
@@ -235,6 +245,13 @@ public final class SnippetConditions {
|
||||
this.content("");
|
||||
}
|
||||
|
||||
private HttpResponseCondition(int responseStatusCode, String responseStatusReason,
|
||||
CodeBlockCondition<?> delegate, int headerOffset) {
|
||||
super(delegate, headerOffset);
|
||||
this.content("HTTP/1.1 " + responseStatusCode + " " + responseStatusReason);
|
||||
this.content("");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user