Skip to content

Commit

Permalink
Add http prefix cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Clint Checketts committed Jul 7, 2017
1 parent 18b1bf9 commit 4fbe34c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
19 changes: 18 additions & 1 deletion domo-java-sdk-all/src/main/java/com/domo/sdk/request/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Config {
private static final Logger LOG = LoggerFactory.getLogger(Config.class);
private final String clientId;
private final String secret;
private final List<Scope> scopes;
Expand All @@ -27,7 +30,7 @@ public Config(String clientId,
this.clientId = clientId;
this.secret = secret;
this.scopes = Collections.unmodifiableList(scopes);
this.apiHost = apiHost;
this.apiHost = stripPrefix(apiHost);
this.useHttps = useHttps;

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

}

// Visible for testing.
String stripPrefix(String apiHost) {
String httpPrefix = "http://";
String httpsPrefix = "https://";
if(apiHost.toLowerCase().startsWith(httpPrefix)) {
LOG.warn("Ignoring http hpiHost scheme, set 'useHttps' to false for http");
return apiHost.substring(httpPrefix.length());
} else if(apiHost.toLowerCase().startsWith(httpsPrefix)) {
return apiHost.substring(httpsPrefix.length());
} else {
return apiHost;
}
}

public String getClientId() {
return clientId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.domo.sdk.request;

import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.Test;

import java.util.ArrayList;

import static org.assertj.core.api.Assertions.*;

public class ConfigTest {

@Test
public void apiHost_shouldRemoveScheme() {
Config c = newConfig("hostwithhttp.com:1234");
assertThat(c.getApiHost()).isEqualTo("hostwithhttp.com:1234");

assertThat( newConfig("http://example.com").getApiHost()).isEqualTo("example.com");
assertThat( newConfig("https://other.com:1234").getApiHost()).isEqualTo("other.com:1234");

}

private Config newConfig(String host) {
return new Config("a", "b", host, true, new ArrayList<>(), null, HttpLoggingInterceptor.Level.BASIC);
}

}

0 comments on commit 4fbe34c

Please sign in to comment.