Skip to content

Commit

Permalink
remove custom http client from the builder class
Browse files Browse the repository at this point in the history
  • Loading branch information
tinahollygb committed Jul 31, 2023
1 parent 02cae60 commit 0b68127
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class GBFeaturesRepository implements IGBFeaturesRepository {
* @param encryptionKey optional key for decrypting encrypted payload
* @param swrTtlSeconds How often the cache should be invalidated when using {@link FeatureRefreshStrategy#STALE_WHILE_REVALIDATE} (default: 60)
*/
@Builder
public GBFeaturesRepository(
@Nullable String apiHost,
String clientKey,
Expand All @@ -89,7 +90,6 @@ public GBFeaturesRepository(
* @param swrTtlSeconds How often the cache should be invalidated when using {@link FeatureRefreshStrategy#STALE_WHILE_REVALIDATE} (default: 60)
* @param okHttpClient HTTP client (optional)
*/
@Builder
public GBFeaturesRepository(
@Nullable String apiHost,
String clientKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ void refreshesFeaturesWhenGetFeaturesCalledAfterCacheExpired() throws IOExceptio
"}";
String encryptionKey = "o0maZL/O7AphxcbRvaJIzw==";
OkHttpClient mockOkHttpClient = mockHttpClient(fakeResponseJson);
GBFeaturesRepository subject = GBFeaturesRepository.builder()
.apiHost("http://localhost:80")
.clientKey("sdk-abc123")
.encryptionKey(encryptionKey)
.swrTtlSeconds(ttlSeconds)
.okHttpClient(mockOkHttpClient)
.build();
GBFeaturesRepository subject = new GBFeaturesRepository(
"http://localhost:80",
"sdk-abc123",
encryptionKey,
FeatureRefreshStrategy.STALE_WHILE_REVALIDATE,
ttlSeconds,
mockOkHttpClient
);
subject.initialize();

// Advance time 3 seconds. We are still within the cache TTL so it should not trigger a refresh.
Expand Down Expand Up @@ -66,13 +67,15 @@ void doesNotRefreshFeaturesWhenGetFeaturesCalledWithinCacheTime() throws IOExcep
"}";
String encryptionKey = "o0maZL/O7AphxcbRvaJIzw==";
OkHttpClient mockOkHttpClient = mockHttpClient(fakeResponseJson);
GBFeaturesRepository subject = GBFeaturesRepository.builder()
.apiHost("http://localhost:80")
.clientKey("sdk-abc123")
.encryptionKey(encryptionKey)
.swrTtlSeconds(ttlSeconds)
.okHttpClient(mockOkHttpClient)
.build();

GBFeaturesRepository subject = new GBFeaturesRepository(
"http://localhost:80",
"sdk-abc123",
encryptionKey,
FeatureRefreshStrategy.STALE_WHILE_REVALIDATE,
ttlSeconds,
mockOkHttpClient
);
subject.initialize();

// Advance time 2 seconds. We are still within the cache TTL so it should not trigger a refresh.
Expand All @@ -99,13 +102,14 @@ void refreshesFeaturesWhenGetFeaturesCalledAfterCacheExpired_multipleTimes() thr
"}";
String encryptionKey = "o0maZL/O7AphxcbRvaJIzw==";
OkHttpClient mockOkHttpClient = mockHttpClient(fakeResponseJson);
GBFeaturesRepository subject = GBFeaturesRepository.builder()
.apiHost("http://localhost:80")
.clientKey("sdk-abc123")
.encryptionKey(encryptionKey)
.swrTtlSeconds(ttlSeconds)
.okHttpClient(mockOkHttpClient)
.build();
GBFeaturesRepository subject = new GBFeaturesRepository(
"http://localhost:80",
"sdk-abc123",
encryptionKey,
FeatureRefreshStrategy.STALE_WHILE_REVALIDATE,
ttlSeconds,
mockOkHttpClient
);
subject.initialize();

// Advance time 3 seconds. We are still within the cache TTL so it should not trigger a refresh.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ void canFetchUnencryptedFeatures_mockedResponse() throws FeatureFetchException,
/*
@Test
void canFetchEncryptedFeatures_real() throws FeatureFetchException {
String endpoint = "https://cdn.growthbook.io/api/features/sdk-862b5mHcP9XPugqD";
String encryptionKey = "BhB1wORFmZLTDjbvstvS8w==";
GBFeaturesRepository subject = new GBFeaturesRepository(endpoint, encryptionKey, null);
GBFeaturesRepository subject = GBFeaturesRepository.builder()
.apiHost("https://cdn.growthbook.io")
.clientKey("sdk-862b5mHcP9XPugqD")
.encryptionKey(encryptionKey)
.build();
subject.initialize();
String expected = "{\"greeting\":{\"defaultValue\":\"hello\",\"rules\":[{\"condition\":{\"country\":\"france\"},\"force\":\"bonjour\"},{\"condition\":{\"country\":\"mexico\"},\"force\":\"holaaaaa\"}]}}";
String expected = "{\"greeting\":{\"defaultValue\":\"hello, this is a message from encrypted features!\",\"rules\":[{\"condition\":{\"country\":\"france\"},\"force\":\"bonjour\"},{\"condition\":{\"country\":\"mexico\"},\"force\":\"holaaaaa\"}]}}";
String actual = subject.getFeaturesJson();
System.out.println(actual);
Expand All @@ -131,12 +134,14 @@ void canFetchEncryptedFeatures_mockedResponse() throws IOException, FeatureFetch
String encryptionKey = "o0maZL/O7AphxcbRvaJIzw==";
OkHttpClient mockOkHttpClient = mockHttpClient(fakeResponseJson);

GBFeaturesRepository subject = GBFeaturesRepository.builder()
.apiHost("http://localhost:80")
.okHttpClient(mockOkHttpClient)
.clientKey("abc-123")
.encryptionKey(encryptionKey)
.build();
GBFeaturesRepository subject = new GBFeaturesRepository(
"http://localhost:80",
"abc-123",
encryptionKey,
null,
null,
mockOkHttpClient
);
subject.initialize();

String expected = "{\"targeted_percentage_rollout\":{\"defaultValue\":false,\"rules\":[{\"condition\":{\"id\":\"foo\"},\"force\":true,\"coverage\":0.5,\"hashAttribute\":\"id\"}]},\"test_feature\":{\"defaultValue\":false,\"rules\":[{\"condition\":{\"id\":{\"$not\":{\"$regex\":\"foo\"},\"$eq\":\"\"}},\"force\":true}]},\"sample_json\":{\"defaultValue\":{}},\"string_feature\":{\"defaultValue\":\"hello, world!\"},\"some_test_feature\":{\"defaultValue\":true},\"my_new_feature_jan17_5\":{\"defaultValue\":true},\"my_new_feature_jan17_13\":{\"defaultValue\":true}}";
Expand Down

0 comments on commit 0b68127

Please sign in to comment.