Skip to content

Commit 4fbe34c

Browse files
author
Clint Checketts
committed
Add http prefix cleaning
1 parent 18b1bf9 commit 4fbe34c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

domo-java-sdk-all/src/main/java/com/domo/sdk/request/Config.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import okhttp3.OkHttpClient;
44
import okhttp3.logging.HttpLoggingInterceptor;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
57

68
import java.util.ArrayList;
79
import java.util.Collections;
810
import java.util.List;
911
import java.util.concurrent.TimeUnit;
1012

1113
public class Config {
14+
private static final Logger LOG = LoggerFactory.getLogger(Config.class);
1215
private final String clientId;
1316
private final String secret;
1417
private final List<Scope> scopes;
@@ -27,7 +30,7 @@ public Config(String clientId,
2730
this.clientId = clientId;
2831
this.secret = secret;
2932
this.scopes = Collections.unmodifiableList(scopes);
30-
this.apiHost = apiHost;
33+
this.apiHost = stripPrefix(apiHost);
3134
this.useHttps = useHttps;
3235

3336
if(httpLoggingLevel == null) {
@@ -50,6 +53,20 @@ public Config(String clientId,
5053

5154
}
5255

56+
// Visible for testing.
57+
String stripPrefix(String apiHost) {
58+
String httpPrefix = "http://";
59+
String httpsPrefix = "https://";
60+
if(apiHost.toLowerCase().startsWith(httpPrefix)) {
61+
LOG.warn("Ignoring http hpiHost scheme, set 'useHttps' to false for http");
62+
return apiHost.substring(httpPrefix.length());
63+
} else if(apiHost.toLowerCase().startsWith(httpsPrefix)) {
64+
return apiHost.substring(httpsPrefix.length());
65+
} else {
66+
return apiHost;
67+
}
68+
}
69+
5370
public String getClientId() {
5471
return clientId;
5572
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.domo.sdk.request;
2+
3+
import okhttp3.logging.HttpLoggingInterceptor;
4+
import org.junit.Test;
5+
6+
import java.util.ArrayList;
7+
8+
import static org.assertj.core.api.Assertions.*;
9+
10+
public class ConfigTest {
11+
12+
@Test
13+
public void apiHost_shouldRemoveScheme() {
14+
Config c = newConfig("hostwithhttp.com:1234");
15+
assertThat(c.getApiHost()).isEqualTo("hostwithhttp.com:1234");
16+
17+
assertThat( newConfig("http://example.com").getApiHost()).isEqualTo("example.com");
18+
assertThat( newConfig("https://other.com:1234").getApiHost()).isEqualTo("other.com:1234");
19+
20+
}
21+
22+
private Config newConfig(String host) {
23+
return new Config("a", "b", host, true, new ArrayList<>(), null, HttpLoggingInterceptor.Level.BASIC);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)