2  * ============LICENSE_START======================================================= ONAP : SO
 
   3  * ================================================================================ Copyright (C) 2018 Nokia.
 
   4  * ============================================================================= Licensed under the Apache License,
 
   5  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy
 
   8  * http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 
  11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 
  12  * specific language governing permissions and limitations under the License.
 
  13  * ============LICENSE_END=========================================================
 
  15 package org.onap.so.cloudify.connector.http;
 
  17 import static org.assertj.core.api.Assertions.assertThat;
 
  18 import static org.mockito.BDDMockito.given;
 
  19 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 
  20 import static org.mockito.Mockito.mock;
 
  22 import java.net.URISyntaxException;
 
  23 import org.apache.http.HttpRequest;
 
  24 import org.apache.http.HttpResponse;
 
  25 import org.apache.http.HttpStatus;
 
  26 import org.apache.http.ProtocolException;
 
  27 import org.apache.http.client.methods.HttpDelete;
 
  28 import org.apache.http.client.methods.HttpGet;
 
  29 import org.apache.http.client.methods.HttpHead;
 
  30 import org.apache.http.client.methods.HttpOptions;
 
  31 import org.apache.http.client.methods.HttpPatch;
 
  32 import org.apache.http.client.methods.HttpPost;
 
  33 import org.apache.http.client.methods.HttpPut;
 
  34 import org.apache.http.client.methods.HttpTrace;
 
  35 import org.apache.http.client.methods.HttpUriRequest;
 
  36 import org.apache.http.protocol.HttpContext;
 
  37 import org.junit.Test;
 
  39 public class HttpClientRedirectStrategyTest {
 
  41     private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
 
  44     public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
 
  45         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
 
  46         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
 
  47         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
 
  48         assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
 
  49         assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
 
  53     public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
 
  54         assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
 
  55         assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
 
  56         assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
 
  60     public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
 
  61         assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
 
  65     public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
 
  66         assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
 
  69     private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
 
  70             throws URISyntaxException, ProtocolException {
 
  72         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
 
  73         given(request.getRequestLine().getMethod()).willReturn(methodName);
 
  74         HttpResponse response = null;
 
  75         HttpContext context = null;
 
  76         URI expectedUri = new URI("http://localhost/host");
 
  78         HttpUriRequest httpUriRequest =
 
  79                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
 
  81         assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
 
  82         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
 
  86     public void getRedirect_shouldReturnHttpGetUri_byDefault() throws URISyntaxException, ProtocolException {
 
  88         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
 
  89         given(request.getRequestLine().getMethod()).willReturn(HttpPost.METHOD_NAME);
 
  90         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
 
  91         given(response.getStatusLine().getStatusCode()).willReturn(HttpStatus.SC_ACCEPTED);
 
  92         URI expectedUri = new URI("http://localhost/host");
 
  93         HttpContext context = null;
 
  95         HttpUriRequest httpUriRequest =
 
  96                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
 
  98         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
 
  99         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
 
 103     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forMovedTemporarilyStatus()
 
 104             throws URISyntaxException, ProtocolException {
 
 105         assertHttpRequestIsCopied(HttpStatus.SC_MOVED_TEMPORARILY);
 
 109     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forTemporaryRedirectStatus()
 
 110             throws URISyntaxException, ProtocolException {
 
 111         assertHttpRequestIsCopied(HttpStatus.SC_TEMPORARY_REDIRECT);
 
 114     private void assertHttpRequestIsCopied(int expectedHttpStatus) throws URISyntaxException, ProtocolException {
 
 116         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
 
 117         given(request.getRequestLine().getMethod()).willReturn(HttpGet.METHOD_NAME);
 
 118         given(request.getRequestLine().getUri()).willReturn("http://hostname");
 
 119         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
 
 120         given(response.getStatusLine().getStatusCode()).willReturn(expectedHttpStatus);
 
 121         URI expectedUri = new URI("http://localhost/host");
 
 122         HttpContext context = null;
 
 124         HttpUriRequest httpUriRequest =
 
 125                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
 
 127         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
 
 128         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
 
 131     private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
 
 133         private final URI expectedUri;
 
 135         public TestableHttpClientRedirectStrategy(URI expectedUri) {
 
 136             this.expectedUri = expectedUri;
 
 140         public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {