PageRenderTime 38ms CodeModel.GetById 31ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/http/handlers/RedirectionRetryHandlerTest.java

https://github.com/edwardt/jclouds
Java | 184 lines | 118 code | 43 blank | 23 comment | 0 complexity | 97644819bd9d01334e9c80413b83d949 MD5 | raw file
  1/**
  2 * Licensed to jclouds, Inc. (jclouds) under one or more
  3 * contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  jclouds licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing,
 13 * software distributed under the License is distributed on an
 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 * KIND, either express or implied.  See the License for the
 16 * specific language governing permissions and limitations
 17 * under the License.
 18 */
 19package org.jclouds.http.handlers;
 20
 21import static org.easymock.EasyMock.expect;
 22import static org.easymock.classextension.EasyMock.createMock;
 23import static org.easymock.classextension.EasyMock.replay;
 24import static org.easymock.classextension.EasyMock.verify;
 25
 26import java.net.URI;
 27
 28import javax.inject.Named;
 29import javax.inject.Singleton;
 30import javax.ws.rs.core.HttpHeaders;
 31
 32import org.jclouds.http.HttpCommand;
 33import org.jclouds.http.HttpRequest;
 34import org.jclouds.http.HttpResponse;
 35import org.jclouds.rest.BaseRestClientTest.MockModule;
 36import org.jclouds.rest.config.RestModule;
 37import org.testng.annotations.Test;
 38
 39import com.google.common.collect.ImmutableMultimap;
 40import com.google.common.collect.LinkedHashMultimap;
 41import com.google.common.collect.Multimap;
 42import com.google.inject.AbstractModule;
 43import com.google.inject.Guice;
 44import com.google.inject.Injector;
 45import com.google.inject.Provides;
 46
 47/**
 48 * Tests behavior of {@code RedirectionRetryHandler}
 49 * 
 50 * @author Adrian Cole
 51 */
 52@Test(groups = "unit")
 53public class RedirectionRetryHandlerTest {
 54   Injector injector = Guice.createInjector(new MockModule(), new RestModule(), new AbstractModule() {
 55      @SuppressWarnings("unused")
 56      @Provides
 57      @Singleton
 58      @Named("CONSTANTS")
 59      protected Multimap<String, String> constants() {
 60         return LinkedHashMultimap.create();
 61      }
 62
 63      @Override
 64      protected void configure() {
 65      }
 66   });
 67
 68   @Test
 69   public void test302DoesNotRetry() {
 70
 71      HttpCommand command = createMock(HttpCommand.class);
 72      HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null);
 73
 74      expect(command.incrementRedirectCount()).andReturn(0);
 75
 76      replay(command);
 77
 78      RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
 79
 80      assert !retry.shouldRetryRequest(command, response);
 81
 82      verify(command);
 83
 84   }
 85
 86   @Test
 87   public void test302DoesNotRetryAfterLimit() {
 88
 89      HttpCommand command = createMock(HttpCommand.class);
 90      HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(
 91               HttpHeaders.LOCATION, "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645"));
 92
 93      expect(command.incrementRedirectCount()).andReturn(5);
 94
 95      replay(command);
 96
 97      RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
 98
 99      assert !retry.shouldRetryRequest(command, response);
100
101      verify(command);
102
103   }
104
105   @Test
106   public void test302WithPathOnlyHeader() {
107
108      verifyRedirectRoutes(
109               new HttpRequest("GET", URI
110                        .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
111               new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
112                        "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645")),
113               new HttpRequest(
114                        "GET",
115                        URI
116                                 .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645")));
117
118   }
119
120   @Test
121   public void test302ToHttps() {
122
123      verifyRedirectRoutes(new HttpRequest("GET", URI
124               .create("http://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
125               new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
126                        "https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),//
127               new HttpRequest("GET", URI
128                        .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")));
129
130   }
131
132   @Test
133   public void test302ToDifferentPort() {
134
135      verifyRedirectRoutes(new HttpRequest("GET", URI
136               .create("http://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
137               new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
138                        "http://services.enterprisecloud.terremark.com:3030/api/v0.8b-ext2.5/org/1906645")),//
139               new HttpRequest("GET", URI
140                        .create("http://services.enterprisecloud.terremark.com:3030/api/v0.8b-ext2.5/org/1906645")));
141
142   }
143
144   @Test
145   public void test302WithHeader() {
146
147      verifyRedirectRoutes(new HttpRequest("GET", URI
148               .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
149               new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
150                        "https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
151               new HttpRequest("GET", URI
152                        .create("https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")));
153
154   }
155
156   @Test
157   public void test302WithHeaderReplacesHostHeader() {
158
159      verifyRedirectRoutes(new HttpRequest("GET", URI
160               .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645"),
161               ImmutableMultimap.of(HttpHeaders.HOST, "services.enterprisecloud.terremark.com")), new HttpResponse(302,
162               "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
163                        "https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),//
164               new HttpRequest("GET", URI
165                        .create("https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645"),
166                        ImmutableMultimap.of(HttpHeaders.HOST, "services1.enterprisecloud.terremark.com")));
167
168   }
169
170   protected void verifyRedirectRoutes(HttpRequest request, HttpResponse response, HttpRequest expected) {
171      HttpCommand command = createMock(HttpCommand.class);
172
173      expect(command.incrementRedirectCount()).andReturn(0);
174      expect(command.getCurrentRequest()).andReturn(request);
175      command.setCurrentRequest(expected);
176
177      replay(command);
178
179      RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
180
181      assert retry.shouldRetryRequest(command, response);
182      verify(command);
183   }
184}