org.springframework.restdocs.operation.OperationResponse Java Examples

The following examples show how to use org.springframework.restdocs.operation.OperationResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResponseHandler.java    From restdocs-raml with MIT License 6 votes vote down vote up
public Map<String, Object> generateModel(Operation operation, RamlResourceSnippetParameters parameters) {
    final OperationResponse response = operation.getResponse();
    if (!StringUtils.isEmpty(response.getContentAsString())) {
        Map<String, Object> model = new HashMap<>();
        model.put("responseBodyFileName", getResponseFileName(operation.getName()));
        model.put("responseBodyPresent", true);
        model.put("contentTypeResponse", getContentTypeOrDefault(response));
        if (!parameters.getResponseFields().isEmpty()) {
            validateResponseFieldsAndInferTypeInformation(operation, parameters);
            model.put("responseFieldsPresent", true);
            if (shouldGenerateResponseSchemaFile(operation, parameters)) {
                model.put("responseSchemaFileName", getResponseSchemaFileName(operation.getName()));
            }
        }
        return model;
    }
    return emptyMap();
}
 
Example #2
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
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;
		}
	};
}
 
Example #3
Source File: ResponseHandler.java    From restdocs-raml with MIT License 5 votes vote down vote up
private String getContentTypeOrDefault(OperationResponse response) {
    if (response.getHeaders().getContentType() != null) {
        return response.getHeaders().getContentType().getType() + "/" + response.getHeaders().getContentType().getSubtype();
    } else {
        return APPLICATION_JSON_VALUE;
    }
}
 
Example #4
Source File: DynamicPortPlaceholderInserterPreprocessor.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Override
public OperationResponse preprocess(OperationResponse response) {
	String content = response.getContentAsString();
	if (content.contains("localhost:8080")) {
		content = content.replace("localhost:8080",
				"localhost:{{request.requestLine.port}}");
		response = this.responseFactory.createFrom(response, content.getBytes());
	}
	return response;
}
 
Example #5
Source File: ContractDslSnippet.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private void insertResponseModel(Operation operation, Map<String, Object> model) {
	OperationResponse response = operation.getResponse();
	model.put("response_status", response.getStatus().value());
	model.put("response_body_present", response.getContent().length > 0);
	model.put("response_body", response.getContentAsString());
	Map<String, String> headers = new HashMap<>(
			response.getHeaders().toSingleValueMap());
	filterHeaders(headers);
	model.put("response_headers_present", !headers.isEmpty());
	model.put("response_headers", headers.entrySet());
}
 
Example #6
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private Operation operation(String name, OperationRequest request,
		OperationResponse response, RestDocumentationContext context) {
	return new Operation() {

		Map<String, Object> map = new HashMap<>();

		@Override
		public Map<String, Object> getAttributes() {
			this.map.put(RestDocumentationContext.class.getName(), context);
			return this.map;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public OperationRequest getRequest() {
			return request;
		}

		@Override
		public OperationResponse getResponse() {
			return response;
		}
	};
}
 
Example #7
Source File: ResponseModifyingPreprocessorsTest.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
private void preprocessorTest(TestData testData) {
    // given
    OperationResponse unprocessedResponse =
            new OperationResponseFake(testData.getContent(), testData.getContentType());
    // when
    OperationResponse processedResponse =
            testData.getPreprocessor().preprocess(unprocessedResponse);
    // then
    assertThat(processedResponse.getContentAsString(), is(testData.getExpectedResult()));
}
 
Example #8
Source File: OperationBuilder.java    From restdocs-raml with MIT License 4 votes vote down vote up
private OperationResponse buildResponse() {
	return new OperationResponseFactory().create(this.status, this.headers,
			this.content);
}
 
Example #9
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private Operation operation(OperationRequest request, OperationResponse response,
		RestDocumentationContext context) {
	return operation("foo", request, response, context);
}
 
Example #10
Source File: OperationBuilder.java    From restdocs-wiremock with Apache License 2.0 4 votes vote down vote up
private OperationResponse buildResponse() {
	return new OperationResponseFactory().create(this.status, this.headers,
			this.content);
}