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.ProtocolException;
32 import org.apache.http.client.methods.HttpDelete;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpHead;
35 import org.apache.http.client.methods.HttpOptions;
36 import org.apache.http.client.methods.HttpPatch;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.client.methods.HttpTrace;
40 import org.apache.http.client.methods.HttpUriRequest;
41 import org.apache.http.protocol.HttpContext;
42 import org.junit.Test;
44 public class HttpClientRedirectStrategyTest {
46 private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
49 public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
50 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
51 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
52 assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
53 assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
54 assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
58 public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
59 assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
60 assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
61 assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
65 public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
66 assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
70 public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
71 assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
74 private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
75 throws URISyntaxException, ProtocolException {
77 HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
78 given(request.getRequestLine().getMethod()).willReturn(methodName);
79 HttpResponse response = null;
80 HttpContext context = null;
81 URI expectedUri = new URI("http://localhost/host");
83 HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
84 .getRedirect(request, response, context);
86 assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
87 assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
90 private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
92 private final URI expectedUri;
94 public TestableHttpClientRedirectStrategy(URI expectedUri) {
95 this.expectedUri = expectedUri;
99 public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {