diff --git a/modules/portal/test/com/haulmont/rest/demo/http/rest/RestTestUtils.java b/modules/portal/test/com/haulmont/rest/demo/http/rest/RestTestUtils.java index 0e95cc8..7835e65 100644 --- a/modules/portal/test/com/haulmont/rest/demo/http/rest/RestTestUtils.java +++ b/modules/portal/test/com/haulmont/rest/demo/http/rest/RestTestUtils.java @@ -96,6 +96,24 @@ public static CloseableHttpResponse sendPut(String url, String token, String bod return httpClient.execute(httpPut); } + public static CloseableHttpResponse sendPutWithHeaders(String url, String token, String body, @Nullable Map params, Map headers) throws Exception { + URIBuilder uriBuilder = new URIBuilder(URI_BASE + url); + if (params != null) { + for (Map.Entry entry : params.entrySet()) { + uriBuilder.addParameter(entry.getKey(), entry.getValue()); + } + } + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPut httpPut = new HttpPut(uriBuilder.build()); + StringEntity stringEntity = new StringEntity(body); + httpPut.setEntity(stringEntity); + httpPut.setHeader("Authorization", "Bearer " + token); + for (Map.Entry entry : headers.entrySet()) { + httpPut.setHeader(entry.getKey(), entry.getValue()); + } + return httpClient.execute(httpPut); + } + public static CloseableHttpResponse sendDelete(String url, String token, @Nullable Map params) throws Exception { URIBuilder uriBuilder = new URIBuilder(URI_BASE + url); if (params != null) { diff --git a/modules/portal/test/com/haulmont/rest/demo/http/rest/UserSessionControllerFT.java b/modules/portal/test/com/haulmont/rest/demo/http/rest/UserSessionControllerFT.java new file mode 100644 index 0000000..49c2bca --- /dev/null +++ b/modules/portal/test/com/haulmont/rest/demo/http/rest/UserSessionControllerFT.java @@ -0,0 +1,57 @@ +package com.haulmont.rest.demo.http.rest; + +import org.apache.http.HttpHeaders; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.junit.Test; + +import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +import static com.haulmont.rest.demo.http.rest.RestTestUtils.*; +import static org.junit.Assert.*; + +public class UserSessionControllerFT extends AbstractRestControllerFT { + + @Test + public void setSessionLocale() throws Exception { + setSessionLocale( + "en", + oauthToken, + response -> assertEquals(HttpStatus.SC_OK, statusCode(response)) + ); + } + + @Test + public void setSessionLocaleUnauthorized() throws Exception { + setSessionLocale( + "en", + null, + response -> assertEquals(HttpStatus.SC_UNAUTHORIZED, statusCode(response)) + ); + } + + @Test + public void setSessionLocaleUnsupported() throws Exception { + setSessionLocale( + "a string representing unsupported locale", + oauthToken, + response -> assertEquals(HttpStatus.SC_UNPROCESSABLE_ENTITY, statusCode(response)) + ); + } + + private void setSessionLocale( + String locale, @Nullable String token, Consumer assertionsCallback + ) throws Exception { + String url = "/user-session/locale"; + + Map headers = new HashMap<>(); + headers.put(HttpHeaders.ACCEPT_LANGUAGE, locale); + + try (CloseableHttpResponse response = sendPutWithHeaders(url, token, "", null, headers)) { + assertionsCallback.accept(response); + } + } +} diff --git a/modules/portal/test/com/haulmont/rest/demo/http/rest/VersionControllerFT.java b/modules/portal/test/com/haulmont/rest/demo/http/rest/VersionControllerFT.java new file mode 100644 index 0000000..d2c204c --- /dev/null +++ b/modules/portal/test/com/haulmont/rest/demo/http/rest/VersionControllerFT.java @@ -0,0 +1,23 @@ +package com.haulmont.rest.demo.http.rest; + +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.junit.Test; + +import static com.haulmont.rest.demo.http.rest.RestTestUtils.*; +import static org.junit.Assert.*; + +public class VersionControllerFT extends AbstractRestControllerFT { + + @Test + public void getApiVersion() throws Exception { + String url = "/version"; + try (CloseableHttpResponse response = sendGet(url, oauthToken, null)) { + assertEquals(HttpStatus.SC_OK, statusCode(response)); + + String version = responseToString(response); + assertNotNull(version); + assertTrue(version.length() > 0); + } + } +}