Skip to content

Commit

Permalink
Support for web test client (#1078)
Browse files Browse the repository at this point in the history
* Set up module. Start adding factory classes and methods for creating WebTestClient instances.

* Add test setup. Add specification interfaces.
Add `standaloneSetup()`, `given()`, `when()`, `and()` methods implementation.
Start moving code that can be reused to a commons module.
Start implementing `WebTesClientRequestSpecificationImpl`.

* Implement handling body, cookies, multipart, config and merging specs.
Fix serializing.

* Finish implementing `WebTestClientRequestSenderImpl`. Start implementing
logging for WebTestClient.

* Start implementing WebtTestClientSenderImpl. Add tests and fixes.

* Test fixes and javadocs for attributes and cookies.

* Fix passing response headers. Implement `ValidatableWebTestClientResponseImpl`.

* Handle null response content type. Implement content type tests. Switch
to standard test formatting.

* Implement logging.Start working on `WebTestClientRequestSpecBuilder`.

* Fix assigning config to WebTestClientRequestSpecification. Adjust logging
tests to WebTestClient returned headers. Implement setup for logging if
validation fails.

* Handle form params. Add non-static request and response spec test.

* Implement param update methods in WebtTestClientParamConfig. Add param
config tests.

* Add methods in `WebTestClientRequestLogSpecification` and implement them
in `WebTestClientRequestLogSpecificationImpl`, add fixes to
`WebTestClientRequestSpecification` and `WebTestClientRequestSenderImpl`.
Add more tests for params, requests and request specification.

* Switch `WebTestClientRequestSenderImpl` to deserialize response to String
in order to enable custom content types. Add more tests.

* Fix outputting status line for error responses. Add setting blank content
type for URL-encoded. Add `PutTest`.

* Implement methods in `WebTestClientRequestLogSpecificationImpl`. Add
`RequestLoggingTest`. Add `QueryParamTest`.

* Add `ResponseLoggingTest`. Refactor `RequestLoggingTest`.

* Fix passing params for restdocs. Fix consumeWith. Removed the possibility
of returning responseSpec as trying to do body assertions on it would cause
issues, cause the stream has already been blocked to get the byte array response.

* Add SecuredRequestTest. Remove unnecessary comments and code.

* Remove unnecessary code and refactor.

* Implement sending requests with uriFunctions, refactor and reformat code.
Add license comments. Javadoc fixes.

* Refactor and reformat code. Add license comments. Javadoc fixes.

* Clean up dependencies.

* Fix method call. Throw IllegalArgumentException when WebTestClientFactory
not assigned.

* Fixes after code review.
  • Loading branch information
OlgaMaciaszek authored and johanhaleby committed Oct 11, 2018
1 parent 850c443 commit 9178668
Show file tree
Hide file tree
Showing 91 changed files with 10,419 additions and 554 deletions.
4 changes: 3 additions & 1 deletion modules/pom.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
~ Copyright 2013 the original author or authors. ~ Copyright 2013-2018 the original author or authors.
~ ~
~ Licensed under the Apache License, Version 2.0 (the "License"); ~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License. ~ you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,5 +29,7 @@
<module>json-schema-validator</module> <module>json-schema-validator</module>
<module>spring-mock-mvc</module> <module>spring-mock-mvc</module>
<module>scala-support</module> <module>scala-support</module>
<module>spring-web-test-client</module>
<module>spring-commons</module>
</modules> </modules>
</project> </project>
41 changes: 41 additions & 0 deletions modules/spring-commons/pom.xml
@@ -0,0 +1,41 @@
<?xml version="1.0"?><!--
~ Copyright 2018 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.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.rest-assured</groupId>
<artifactId>modules</artifactId>
<version>3.1.2-SNAPSHOT</version>
</parent>
<artifactId>spring-commons</artifactId>
<version>3.1.2-SNAPSHOT</version>
<name>spring-commons</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.9.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.1.2-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,84 @@
/*
* Copyright 2016-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.module.spring.commons;

import io.restassured.http.Headers;
import io.restassured.internal.mapping.ObjectMapperSerializationContextImpl;
import io.restassured.internal.mapping.ObjectMapping;
import io.restassured.mapper.ObjectMapper;
import io.restassured.module.spring.commons.config.SpecificationConfig;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static io.restassured.internal.serialization.SerializationSupport.isSerializableCandidate;

public class BodyHelper {

private BodyHelper() {
}

public static String toStringBody(Object object, SpecificationConfig config, Headers headers) {
if (!isSerializableCandidate(object)) {
return object.toString();
}
String requestContentType = HeaderHelper.getRequestContentType(headers);
return ObjectMapping.serialize(object, requestContentType,
Serializer.findEncoderCharsetOrReturnDefault(requestContentType, config), null,
config.getObjectMapperConfig(), config.getEncoderConfig());
}

public static Object toSerializedBody(Object object, ObjectMapper objectMapper, SpecificationConfig config,
Headers headers) {
String requestContentType = HeaderHelper.getRequestContentType(headers);
ObjectMapperSerializationContextImpl ctx = new ObjectMapperSerializationContextImpl();
ctx.setObject(object);
ctx.setCharset(Serializer.findEncoderCharsetOrReturnDefault(requestContentType, config));
ctx.setContentType(requestContentType);
return objectMapper.serialize(ctx);
}

public static byte[] toByteArray(File file) {
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1) {
ous.write(buffer, 0, read);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (ous != null) {
ous.close();
}
if (ios != null) {
ios.close();
}
} catch (IOException ignored) {
}
}

return ous.toByteArray();
}
}
@@ -0,0 +1,88 @@
/*
* Copyright 2016-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.restassured.module.spring.commons;

import io.restassured.http.Cookie;
import io.restassured.http.Cookies;
import io.restassured.http.Headers;
import io.restassured.module.spring.commons.config.SpecificationConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CookieHelper {

private CookieHelper() {
}

public static Cookies cookies(Cookies requestCookies, Map<String, ?> cookies, Headers requestHeaders,
SpecificationConfig config) {
List<Cookie> cookieList = new ArrayList<Cookie>();
if (requestCookies.exist()) {
for (Cookie requestCookie : requestCookies) {
cookieList.add(requestCookie);
}
}
for (Map.Entry<String, ?> stringEntry : cookies.entrySet()) {
cookieList.add(new Cookie.Builder(stringEntry.getKey(), Serializer.serializeIfNeeded(stringEntry.getValue(),
HeaderHelper.getRequestContentType(requestHeaders), config)).build());
}
return new Cookies(cookieList);
}

public static Cookies cookies(Cookies requestCookies, Cookies cookies) {
if (cookies.exist()) {
List<Cookie> cookieList = new ArrayList<Cookie>();
if (requestCookies.exist()) {
for (Cookie cookie : requestCookies) {
cookieList.add(cookie);
}
}
for (Cookie cookie : cookies) {
cookieList.add(cookie);
}
return new Cookies(cookieList);
}
return requestCookies;
}

public static Cookies cookie(final String cookieName, final Object cookieValue, Headers requestHeaders,
final SpecificationConfig config, Object... additionalValues) {
final String contentType = HeaderHelper.getRequestContentType(requestHeaders);
List<Cookie> cookieList = new ArrayList<Cookie>() {{
add(new Cookie.Builder(cookieName, Serializer.serializeIfNeeded(cookieValue, contentType, config)).build());
}};
if (additionalValues != null) {
for (Object additionalCookieValue : additionalValues) {
cookieList.add(new Cookie.Builder(cookieName,
Serializer.serializeIfNeeded(additionalCookieValue, contentType, config)).build());
}
}
return new Cookies(cookieList);
}

public static Cookies sessionId(Cookies cookies, String sessionIdName, String sessionIdValue) {
List<Cookie> allOtherCookies = new ArrayList<Cookie>();
for (Cookie cookie : cookies) {
if (!cookie.getName().equalsIgnoreCase(sessionIdName)) {
allOtherCookies.add(cookie);
}
}
allOtherCookies.add(new Cookie.Builder(sessionIdName, sessionIdValue).build());
return new Cookies(allOtherCookies);
}
}

0 comments on commit 9178668

Please sign in to comment.