PageRenderTime 60ms CodeModel.GetById 35ms 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. */
  19. package org.jclouds.http.handlers;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.easymock.classextension.EasyMock.replay;
  23. import static org.easymock.classextension.EasyMock.verify;
  24. import java.net.URI;
  25. import javax.inject.Named;
  26. import javax.inject.Singleton;
  27. import javax.ws.rs.core.HttpHeaders;
  28. import org.jclouds.http.HttpCommand;
  29. import org.jclouds.http.HttpRequest;
  30. import org.jclouds.http.HttpResponse;
  31. import org.jclouds.rest.BaseRestClientTest.MockModule;
  32. import org.jclouds.rest.config.RestModule;
  33. import org.testng.annotations.Test;
  34. import com.google.common.collect.ImmutableMultimap;
  35. import com.google.common.collect.LinkedHashMultimap;
  36. import com.google.common.collect.Multimap;
  37. import com.google.inject.AbstractModule;
  38. import com.google.inject.Guice;
  39. import com.google.inject.Injector;
  40. import com.google.inject.Provides;
  41. /**
  42. * Tests behavior of {@code RedirectionRetryHandler}
  43. *
  44. * @author Adrian Cole
  45. */
  46. @Test(groups = "unit")
  47. public class RedirectionRetryHandlerTest {
  48. Injector injector = Guice.createInjector(new MockModule(), new RestModule(), new AbstractModule() {
  49. @SuppressWarnings("unused")
  50. @Provides
  51. @Singleton
  52. @Named("CONSTANTS")
  53. protected Multimap<String, String> constants() {
  54. return LinkedHashMultimap.create();
  55. }
  56. @Override
  57. protected void configure() {
  58. }
  59. });
  60. @Test
  61. public void test302DoesNotRetry() {
  62. HttpCommand command = createMock(HttpCommand.class);
  63. HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null);
  64. expect(command.incrementRedirectCount()).andReturn(0);
  65. replay(command);
  66. RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
  67. assert !retry.shouldRetryRequest(command, response);
  68. verify(command);
  69. }
  70. @Test
  71. public void test302DoesNotRetryAfterLimit() {
  72. HttpCommand command = createMock(HttpCommand.class);
  73. HttpResponse response = new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(
  74. HttpHeaders.LOCATION, "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645"));
  75. expect(command.incrementRedirectCount()).andReturn(5);
  76. replay(command);
  77. RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
  78. assert !retry.shouldRetryRequest(command, response);
  79. verify(command);
  80. }
  81. @Test
  82. public void test302WithPathOnlyHeader() {
  83. verifyRedirectRoutes(
  84. new HttpRequest("GET", URI
  85. .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
  86. new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
  87. "/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645")),
  88. new HttpRequest(
  89. "GET",
  90. URI
  91. .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/Error.aspx?aspxerrorpath=/api/v0.8b-ext2.5/org.svc/1906645")));
  92. }
  93. @Test
  94. public void test302ToHttps() {
  95. verifyRedirectRoutes(new HttpRequest("GET", URI
  96. .create("http://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
  97. new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
  98. "https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),//
  99. new HttpRequest("GET", URI
  100. .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")));
  101. }
  102. @Test
  103. public void test302ToDifferentPort() {
  104. verifyRedirectRoutes(new HttpRequest("GET", URI
  105. .create("http://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
  106. new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
  107. "http://services.enterprisecloud.terremark.com:3030/api/v0.8b-ext2.5/org/1906645")),//
  108. new HttpRequest("GET", URI
  109. .create("http://services.enterprisecloud.terremark.com:3030/api/v0.8b-ext2.5/org/1906645")));
  110. }
  111. @Test
  112. public void test302WithHeader() {
  113. verifyRedirectRoutes(new HttpRequest("GET", URI
  114. .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
  115. new HttpResponse(302, "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
  116. "https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),
  117. new HttpRequest("GET", URI
  118. .create("https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")));
  119. }
  120. @Test
  121. public void test302WithHeaderReplacesHostHeader() {
  122. verifyRedirectRoutes(new HttpRequest("GET", URI
  123. .create("https://services.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645"),
  124. ImmutableMultimap.of(HttpHeaders.HOST, "services.enterprisecloud.terremark.com")), new HttpResponse(302,
  125. "HTTP/1.1 302 Found", null, ImmutableMultimap.of(HttpHeaders.LOCATION,
  126. "https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645")),//
  127. new HttpRequest("GET", URI
  128. .create("https://services1.enterprisecloud.terremark.com/api/v0.8b-ext2.5/org/1906645"),
  129. ImmutableMultimap.of(HttpHeaders.HOST, "services1.enterprisecloud.terremark.com")));
  130. }
  131. protected void verifyRedirectRoutes(HttpRequest request, HttpResponse response, HttpRequest expected) {
  132. HttpCommand command = createMock(HttpCommand.class);
  133. expect(command.incrementRedirectCount()).andReturn(0);
  134. expect(command.getCurrentRequest()).andReturn(request);
  135. command.setCurrentRequest(expected);
  136. replay(command);
  137. RedirectionRetryHandler retry = injector.getInstance(RedirectionRetryHandler.class);
  138. assert retry.shouldRetryRequest(command, response);
  139. verify(command);
  140. }
  141. }