org.springframework.restdocs.operation.OperationRequest Java Examples

The following examples show how to use org.springframework.restdocs.operation.OperationRequest. 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: RequestHandler.java    From restdocs-raml with MIT License 6 votes vote down vote up
public Map<String, Object> generateModel(Operation operation, RamlResourceSnippetParameters parameters) {
    final OperationRequest request = operation.getRequest();

    if (!StringUtils.isEmpty(request.getContentAsString())) {
        Map<String, Object> model = new HashMap<>();
        model.put("requestBodyFileName", getRequestFileName(operation.getName()));
        model.put("requestBodyPresent", true);
        model.put("contentTypeRequest", getContentTypeOrDefault(request));
        if (!parameters.getRequestFields().isEmpty()) {
            validateRequestFieldsAndInferTypeInformation(operation, parameters);
            model.put("requestFieldsPresent", true);
            if (shouldGenerateRequestSchemaFile(operation, parameters)) {
                model.put("requestSchemaFileName", getRequestSchemaFileName(operation.getName()));
            }
        }
        return model;
    }
    return emptyMap();
}
 
Example #2
Source File: MultipartContentOperationPreprocessorTest.java    From spring-auto-restdocs with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotReplaceWhenNotMultipart() throws URISyntaxException {
    OperationRequest request = createRequestWithContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

    OperationRequest processedRequest = preprocessor.preprocess(request);
    assertArrayEquals(request.getContent(), processedRequest.getContent());
    assertEquals(request.getHeaders(), processedRequest.getHeaders());
    assertEquals(request.getMethod(), processedRequest.getMethod());
    assertEquals(request.getUri(), processedRequest.getUri());
    assertEquals(request.getParameters(), processedRequest.getParameters());

    OperationRequestPart[] expectedParts = request.getParts().toArray(new OperationRequestPart[0]);
    OperationRequestPart[] operationRequestParts = processedRequest.getParts().toArray(new OperationRequestPart[0]);
    for (int i = 0; i < operationRequestParts.length; i++) {
        assertEquals(expectedParts[i], operationRequestParts[i]);
    }
}
 
Example #3
Source File: ContractDslSnippet.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private void insertRequestModel(Operation operation, Map<String, Object> model) {
	OperationRequest request = operation.getRequest();
	model.put("request_method", request.getMethod());
	model.put("request_url", prepareRequestUrl(request.getUri()));
	String rawQuery = request.getUri().getRawQuery();
	boolean urlPathPresent = StringUtils.hasText(rawQuery);
	model.put("request_urlpath_present", urlPathPresent);
	if (urlPathPresent) {
		// TODO: Add support for multiple values
		model.put("request_queryparams",
				request.getParameters().toSingleValueMap().entrySet());
	}
	model.put("request_body_present", request.getContent().length > 0);
	model.put("request_body", request.getContentAsString());
	Map<String, String> headers = new HashMap<>(
			request.getHeaders().toSingleValueMap());
	filterHeaders(headers);
	model.put("request_headers_present", !headers.isEmpty());
	model.put("request_headers", headers.entrySet());
	@SuppressWarnings("unchecked")
	Set<String> jsonPaths = (Set<String>) operation.getAttributes()
			.get("contract.jsonPaths");
	model.put("request_json_paths_present",
			jsonPaths != null && !jsonPaths.isEmpty());
	model.put("request_json_paths", jsonPaths(jsonPaths));
}
 
Example #4
Source File: RequestHandler.java    From restdocs-raml with MIT License 5 votes vote down vote up
private String getContentTypeOrDefault(OperationRequest request) {
    if (request.getHeaders().getContentType() != null) {
        return request.getHeaders().getContentType().getType() + "/" + request.getHeaders().getContentType().getSubtype();
    } else {
        return APPLICATION_JSON_VALUE;
    }
}
 
Example #5
Source File: MultipartContentOperationPreprocessorTest.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
private OperationRequest createRequestWithContentType(String contentType) throws URISyntaxException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_TYPE, contentType);
    Parameters parameters = new Parameters();
    parameters.add("parameter", "value");
    List<OperationRequestPart> requestParts = new ArrayList<>();
    requestParts.add(partFactory.create("first", "file1", BINARY_CONTENT, new HttpHeaders()));
    return requestFactory.create(new URI("http://localhost"), HttpMethod.POST, BINARY_CONTENT, headers, parameters, requestParts);
}
 
Example #6
Source File: MultipartContentOperationPreprocessorTest.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReplaceContent() throws URISyntaxException {
    OperationRequest request = createRequestWithContentType(MediaType.MULTIPART_FORM_DATA_VALUE);

    OperationRequest processedRequest = preprocessor.preprocess(request);
    assertArrayEquals(request.getContent(), processedRequest.getContent());
    assertEquals(request.getHeaders(), processedRequest.getHeaders());
    assertEquals(request.getMethod(), processedRequest.getMethod());
    assertEquals(request.getUri(), processedRequest.getUri());
    assertEquals(request.getParameters(), processedRequest.getParameters());
    for (OperationRequestPart part : processedRequest.getParts()) {
        assertArrayEquals(EXPECTED_CONTENT, part.getContent());
    }
}
 
Example #7
Source File: MultipartContentOperationPreprocessor.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
@Override
public OperationRequest preprocess(OperationRequest request) {
    if (isMultipart(request)) {
        List<OperationRequestPart> parts = request.getParts().stream()
                .map(this::replaceBinary)
                .collect(toList());
        return requestFactory.create(request.getUri(),
                request.getMethod(), request.getContent(), request.getHeaders(), request.getParameters()
                , parts);
    }
    return request;
}
 
Example #8
Source File: OperationBuilder.java    From restdocs-wiremock with Apache License 2.0 5 votes vote down vote up
private OperationRequest buildRequest() {
	List<OperationRequestPart> parts = new ArrayList<>();
	for (OperationRequestPartBuilder builder : this.partBuilders) {
		parts.add(builder.buildPart());
	}
	return new OperationRequestFactory().create(this.requestUri, this.method,
			this.content, this.headers, this.parameters, parts);
}
 
Example #9
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 #10
Source File: OperationBuilder.java    From restdocs-raml with MIT License 5 votes vote down vote up
private OperationRequest buildRequest() {
	List<OperationRequestPart> parts = new ArrayList<>();
	for (OperationRequestPartBuilder builder : this.partBuilders) {
		parts.add(builder.buildPart());
	}
	return new OperationRequestFactory().create(this.requestUri, this.method,
			this.content, this.headers, this.parameters, parts);
}
 
Example #11
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private OperationRequest requestPostWithJsonContentType() {
	return new OperationRequest() {

		@Override
		public byte[] getContent() {
			String content = "{\"name\": \"12\"}";
			return content.getBytes(Charset.forName("UTF-8"));
		}

		@Override
		public String getContentAsString() {
			return "{\"name\": \"12\"}";
		}

		@Override
		public HttpHeaders getHeaders() {
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
			return httpHeaders;
		}

		@Override
		public HttpMethod getMethod() {
			return HttpMethod.POST;
		}

		@Override
		public Parameters getParameters() {
			return null;
		}

		@Override
		public Collection<OperationRequestPart> getParts() {
			return null;
		}

		@Override
		public URI getUri() {
			return URI.create("https://foo/bar");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #12
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private OperationRequest requestPostWithXmlContentType() {
	return new OperationRequest() {

		@Override
		public byte[] getContent() {
			String content = "<name>foo</name>";
			return content.getBytes(Charset.forName("UTF-8"));
		}

		@Override
		public String getContentAsString() {
			return "<name>foo</name>";
		}

		@Override
		public HttpHeaders getHeaders() {
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.add("Content-Type", MediaType.APPLICATION_XML_VALUE);
			return httpHeaders;
		}

		@Override
		public HttpMethod getMethod() {
			return HttpMethod.POST;
		}

		@Override
		public Parameters getParameters() {
			return null;
		}

		@Override
		public Collection<OperationRequestPart> getParts() {
			return null;
		}

		@Override
		public URI getUri() {
			return URI.create("https://foo/bar");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #13
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private OperationRequest requestPostWithEmptyBody() {
	return new OperationRequest() {
		@Override
		public byte[] getContent() {
			return new byte[0];
		}

		@Override
		public String getContentAsString() {
			return "";
		}

		@Override
		public HttpHeaders getHeaders() {
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.add(HttpHeaders.CONTENT_TYPE,
					MediaType.APPLICATION_JSON_VALUE);
			return httpHeaders;
		}

		@Override
		public HttpMethod getMethod() {
			return HttpMethod.POST;
		}

		@Override
		public Parameters getParameters() {
			return null;
		}

		@Override
		public Collection<OperationRequestPart> getParts() {
			return null;
		}

		@Override
		public URI getUri() {
			return URI.create("https://foo/bar");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #14
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private OperationRequest requestPostWithEmptyQueryParamValue() {
	return new OperationRequest() {
		@Override
		public byte[] getContent() {
			return new byte[0];
		}

		@Override
		public String getContentAsString() {
			return "";
		}

		@Override
		public HttpHeaders getHeaders() {
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.add(HttpHeaders.CONTENT_TYPE,
					MediaType.APPLICATION_JSON_VALUE);
			return httpHeaders;
		}

		@Override
		public HttpMethod getMethod() {
			return HttpMethod.POST;
		}

		@Override
		public Parameters getParameters() {
			Parameters parameters = new Parameters();
			parameters.put("myParam", Collections.singletonList(""));
			return parameters;
		}

		@Override
		public Collection<OperationRequestPart> getParts() {
			return null;
		}

		@Override
		public URI getUri() {
			return URI.create("https://foo/bar?myParam=");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #15
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private OperationRequest requestGetWithQueryParam() {
	return new OperationRequest() {
		@Override
		public byte[] getContent() {
			return new byte[0];
		}

		@Override
		public String getContentAsString() {
			return "";
		}

		@Override
		public HttpHeaders getHeaders() {
			HttpHeaders httpHeaders = new HttpHeaders();
			httpHeaders.add(HttpHeaders.CONTENT_TYPE,
					MediaType.APPLICATION_JSON_VALUE);
			return 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("https://foo/bar?myParam=myValue");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #16
Source File: WireMockSnippetTests.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
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("https://foo/bar");
		}

		@Override
		public Collection<RequestCookie> getCookies() {
			return Collections.emptySet();
		}
	};
}
 
Example #17
Source File: MultipartContentOperationPreprocessor.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
private boolean isMultipart(OperationRequest request) {
    List<String> contentTypes = request.getHeaders().get(HttpHeaders.CONTENT_TYPE);
    return contentTypes != null && contentTypes.contains(MediaType.MULTIPART_FORM_DATA_VALUE);
}
 
Example #18
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 #19
Source File: DynamicPortPlaceholderInserterPreprocessor.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Override
public OperationRequest preprocess(OperationRequest request) {
	return request;
}