2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia.
6 * =============================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.so.cloudify.connector.http;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
25 import static org.mockito.Mockito.mock;
28 import java.net.URISyntaxException;
29 import org.apache.http.HttpRequest;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.ProtocolException;
33 import org.apache.http.client.methods.HttpDelete;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.client.methods.HttpHead;
36 import org.apache.http.client.methods.HttpOptions;
37 import org.apache.http.client.methods.HttpPatch;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpPut;
40 import org.apache.http.client.methods.HttpTrace;
41 import org.apache.http.client.methods.HttpUriRequest;
42 import org.apache.http.protocol.HttpContext;
43 import org.junit.Test;
45 public class HttpClientRedirectStrategyTest {
47 private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
50 public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
51 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
52 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
53 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
54 assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
55 assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
59 public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
60 assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
61 assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
62 assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
66 public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
67 assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
71 public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
72 assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
75 private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
76 throws URISyntaxException, ProtocolException {
78 HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
79 given(request.getRequestLine().getMethod()).willReturn(methodName);
80 HttpResponse response = null;
81 HttpContext context = null;
82 URI expectedUri = new URI("http://localhost/host");
84 HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
85 .getRedirect(request, response, context);
87 assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
88 assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
92 public void getRedirect_shouldReturnHttpGetUri_byDefault() throws URISyntaxException, ProtocolException {
94 HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
95 given(request.getRequestLine().getMethod()).willReturn(HttpPost.METHOD_NAME);
96 HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
97 given(response.getStatusLine().getStatusCode()).willReturn(HttpStatus.SC_ACCEPTED);
98 URI expectedUri = new URI("http://localhost/host");
99 HttpContext context = null;
101 HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
102 .getRedirect(request, response, context);
104 assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
105 assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
109 public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forMovedTemporarilyStatus() throws URISyntaxException, ProtocolException {
110 assertHttpRequestIsCopied(HttpStatus.SC_MOVED_TEMPORARILY);
114 public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forTemporaryRedirectStatus() throws URISyntaxException, ProtocolException {
115 assertHttpRequestIsCopied(HttpStatus.SC_TEMPORARY_REDIRECT);
118 private void assertHttpRequestIsCopied(int expectedHttpStatus) throws URISyntaxException, ProtocolException {
120 HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
121 given(request.getRequestLine().getMethod()).willReturn(HttpGet.METHOD_NAME);
122 given(request.getRequestLine().getUri()).willReturn("http://hostname");
123 HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
124 given(response.getStatusLine().getStatusCode()).willReturn(expectedHttpStatus);
125 URI expectedUri = new URI("http://localhost/host");
126 HttpContext context = null;
128 HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
129 .getRedirect(request, response, context);
131 assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
132 assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
135 private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
137 private final URI expectedUri;
139 public TestableHttpClientRedirectStrategy(URI expectedUri) {
140 this.expectedUri = expectedUri;
143 public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {