client = new OkHttpClientBuilder()
.setConnectTimeoutMs(timeoutConfiguration.getConnectTimeout())
.setReadTimeoutMs(timeoutConfiguration.getReadTimeout())
+ .setFollowRedirects(false)
.build();
}
throw new IllegalArgumentException("Your GitLab token has insufficient scope");
} else if (response.code() == HTTP_UNAUTHORIZED) {
throw new IllegalArgumentException("Invalid personal access token");
+ } else if (response.isRedirect()) {
+ throw new IllegalArgumentException("Request was redirected, please provide the correct URL");
} else {
throw new IllegalArgumentException(errorMessage);
}
.hasMessage("Invalid personal access token");
}
+ @Test
+ public void should_throw_IllegalArgumentException_when_redirected() {
+ MockResponse response = new MockResponse()
+ .setResponseCode(308);
+ server.enqueue(response);
+
+ assertThatThrownBy(() -> underTest.searchProjects(gitlabUrl, "pat", "example", 1, 2))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Request was redirected, please provide the correct URL");
+ }
+
@Test
public void get_project() {
MockResponse response = new MockResponse()
private String credentials;
private String proxyLogin;
private String proxyPassword;
+ private Boolean followRedirects;
private long connectTimeoutMs = -1;
private long readTimeoutMs = -1;
private SSLSocketFactory sslSocketFactory = null;
return this;
}
+ /**
+ * Set if redirects should be followed or not.
+ * Default is defined by OkHttp (true, follow redirects).
+ */
+ public OkHttpClientBuilder setFollowRedirects(Boolean followRedirects) {
+ this.followRedirects = followRedirects;
+ return this;
+ }
+
public OkHttpClient build() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.proxy(proxy);
return null;
});
}
+ if (followRedirects != null) {
+ builder.followRedirects(followRedirects);
+ builder.followSslRedirects(followRedirects);
+ }
ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.allEnabledTlsVersions()
assertThat(okHttpClient.proxy()).isNull();
assertThat(okHttpClient.networkInterceptors()).hasSize(1);
assertThat(okHttpClient.sslSocketFactory()).isNotNull();
+ assertThat(okHttpClient.followRedirects()).isTrue();
+ assertThat(okHttpClient.followSslRedirects()).isTrue();
}
@Test
assertThat(okHttpClient.sslSocketFactory()).isEqualTo(sslSocketFactory);
}
+ @Test
+ public void build_follow_redirects() {
+ OkHttpClient okHttpClientWithRedirect = underTest
+ .setFollowRedirects(true)
+ .build();
+
+ assertThat(okHttpClientWithRedirect.followRedirects()).isTrue();
+ assertThat(okHttpClientWithRedirect.followSslRedirects()).isTrue();
+
+ OkHttpClient okHttpClientWithoutRedirect = underTest
+ .setFollowRedirects(false)
+ .build();
+
+ assertThat(okHttpClientWithoutRedirect.followRedirects()).isFalse();
+ assertThat(okHttpClientWithoutRedirect.followSslRedirects()).isFalse();
+ }
+
@Test
public void build_throws_IAE_if_connect_timeout_is_negative() {
expectedException.expect(IllegalArgumentException.class);