Merge branch '1.0.x'
This commit is contained in:
@@ -94,7 +94,8 @@ public class WireMockSnippet implements Snippet {
|
||||
|
||||
private ResponseDefinitionBuilder response(Operation operation) {
|
||||
return aResponse().withHeaders(responseHeaders(operation))
|
||||
.withBody(operation.getResponse().getContentAsString());
|
||||
.withBody(operation.getResponse().getContentAsString())
|
||||
.withStatus(operation.getResponse().getStatus().value());
|
||||
}
|
||||
|
||||
private RemoteMappingBuilder<?, ?> request(Operation operation) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -8,6 +9,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.contract.wiremock.WiremockServerRestDocsApplicationTests.TestConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
@@ -26,8 +28,7 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.docu
|
||||
@DirtiesContext
|
||||
public class WiremockServerRestDocsApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
@Autowired private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
@@ -36,6 +37,14 @@ public class WiremockServerRestDocsApplicationTests {
|
||||
.andDo(document("resource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusIsMaintained() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/status"))
|
||||
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
|
||||
.andExpect(MockMvcResultMatchers.status().is(HttpStatus.ACCEPTED_202))
|
||||
.andDo(document("status"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@RestController
|
||||
protected static class TestConfiguration {
|
||||
@@ -46,6 +55,12 @@ public class WiremockServerRestDocsApplicationTests {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/status")
|
||||
public ResponseEntity<String> status() {
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED_202).body("Hello World");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
package org.springframework.cloud.contract.wiremock.restdocs;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
import org.springframework.restdocs.operation.OperationRequest;
|
||||
import org.springframework.restdocs.operation.OperationRequestPart;
|
||||
import org.springframework.restdocs.operation.OperationResponse;
|
||||
import org.springframework.restdocs.operation.Parameters;
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class WireMockSnippetTests {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) Operation operation;
|
||||
@Rule public TemporaryFolder tmp = new TemporaryFolder();
|
||||
File outputFolder;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
this.outputFolder = this.tmp.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_maintain_the_response_status_when_generating_stub() throws Exception {
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
RestDocumentationContext context = new RestDocumentationContext(this.getClass(),
|
||||
"method", this.outputFolder);
|
||||
given(this.operation.getName()).willReturn("foo");
|
||||
given(this.operation.getAttributes().get(anyString())).willReturn(null);
|
||||
given(this.operation.getAttributes().get(RestDocumentationContext.class.getName())).willReturn(context);
|
||||
given(this.operation.getRequest()).willReturn(request());
|
||||
given(this.operation.getResponse()).willReturn(response());
|
||||
|
||||
snippet.document(this.operation);
|
||||
|
||||
File stub = new File(this.outputFolder, "stubs/foo.json");
|
||||
assertThat(stub).exists();
|
||||
StubMapping stubMapping = StubMapping.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
assertThat(stubMapping.getResponse().getStatus()).isEqualTo(HttpStatus.ACCEPTED.value());
|
||||
}
|
||||
|
||||
private OperationResponse response() {
|
||||
return new OperationResponse() {
|
||||
|
||||
@Override public HttpStatus getStatus() {
|
||||
return HttpStatus.ACCEPTED;
|
||||
}
|
||||
|
||||
@Override public HttpHeaders getHeaders() {
|
||||
return new HttpHeaders();
|
||||
}
|
||||
|
||||
@Override public byte[] getContent() {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override public String getContentAsString() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private OperationRequest request() {
|
||||
return new OperationRequest() {
|
||||
|
||||
@Override public byte[] getContent() {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override public String getContentAsString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public HttpHeaders getHeaders() {
|
||||
return new HttpHeaders();
|
||||
}
|
||||
|
||||
@Override public HttpMethod getMethod() {
|
||||
return HttpMethod.GET;
|
||||
}
|
||||
|
||||
@Override public Parameters getParameters() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public Collection<OperationRequestPart> getParts() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public URI getUri() {
|
||||
return URI.create("http://foo/bar");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user