Skip to content

Commit e9b32a4

Browse files
@W-21933885: [MSDK Android] App Attestation Implementation (Fix AuthConfigUtilTest Timeout By Mocking Network Calls)
1 parent 1b994fb commit e9b32a4

1 file changed

Lines changed: 72 additions & 1 deletion

File tree

libs/test/SalesforceSDKTest/src/com/salesforce/androidsdk/util/AuthConfigUtilTest.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@
3535
import androidx.test.ext.junit.runners.AndroidJUnit4;
3636
import androidx.test.filters.SmallTest;
3737
import com.salesforce.androidsdk.app.SalesforceSDKManager;
38+
import com.salesforce.androidsdk.auth.HttpAccess;
39+
import java.io.IOException;
3840
import java.util.concurrent.ExecutionException;
3941
import java.util.concurrent.Future;
4042
import java.util.concurrent.CompletableFuture;
43+
import okhttp3.Call;
44+
import okhttp3.OkHttpClient;
45+
import okhttp3.Protocol;
46+
import okhttp3.Request;
47+
import okhttp3.Response;
48+
import okhttp3.ResponseBody;
49+
import okhttp3.MediaType;
50+
import org.json.JSONObject;
4151
import org.junit.Assert;
4252
import org.junit.Test;
4353
import org.junit.runner.RunWith;
@@ -58,6 +68,45 @@ public class AuthConfigUtilTest {
5868
private static final String SANDBOX_ENDPOINT = "https://test.salesforce.com";
5969
private static final String FORWARD_SLASH = "/";
6070

71+
/**
72+
* Mock HttpAccess that returns controlled responses without making real network calls.
73+
*/
74+
private static class MockHttpAccess extends HttpAccess {
75+
private final boolean shouldSucceed;
76+
private final String responseBody;
77+
78+
public MockHttpAccess(boolean shouldSucceed, String responseBody) {
79+
super(null, "MockUserAgent");
80+
this.shouldSucceed = shouldSucceed;
81+
this.responseBody = responseBody;
82+
}
83+
84+
@Override
85+
public OkHttpClient getOkHttpClient() {
86+
return new OkHttpClient.Builder()
87+
.addInterceptor(chain -> {
88+
Request request = chain.request();
89+
Response.Builder responseBuilder = new Response.Builder()
90+
.request(request)
91+
.protocol(Protocol.HTTP_1_1)
92+
.message(shouldSucceed ? "OK" : "Not Found")
93+
.code(shouldSucceed ? 200 : 404);
94+
95+
if (shouldSucceed && responseBody != null) {
96+
responseBuilder.body(ResponseBody.create(
97+
responseBody,
98+
MediaType.get("application/json")
99+
));
100+
} else {
101+
responseBuilder.body(ResponseBody.create("", MediaType.get("text/plain")));
102+
}
103+
104+
return responseBuilder.build();
105+
})
106+
.build();
107+
}
108+
}
109+
61110
private static class TestBroadcastReceiver extends BroadcastReceiver {
62111
private final CompletableFuture<Intent> intentFuture = new CompletableFuture<>();
63112

@@ -128,13 +177,35 @@ public void testBroadcastFails() throws ExecutionException, InterruptedException
128177
testBroadcast(SANDBOX_ENDPOINT, false);
129178
}
130179

180+
/**
181+
* Tests that AUTH_CONFIG_COMPLETE_INTENT_ACTION broadcast is sent with correct success flag.
182+
*
183+
* @param endpoint The endpoint URL to test
184+
* @param expected Whether the auth config request should succeed (true) or fail (false)
185+
*/
131186
private void testBroadcast(String endpoint, Boolean expected) throws InterruptedException, ExecutionException {
132187
final TestBroadcastReceiver receiver = new TestBroadcastReceiver();
133188
ContextCompat.registerReceiver(SalesforceSDKManager.getInstance().getAppContext(), receiver,
134189
new IntentFilter(AuthConfigUtil.AUTH_CONFIG_COMPLETE_INTENT_ACTION), ContextCompat.RECEIVER_NOT_EXPORTED);
135190

136191
try {
137-
AuthConfigUtil.getMyDomainAuthConfig(endpoint);
192+
// Create mock response body for successful auth config requests
193+
String mockAuthConfigJson = "{"
194+
+ "\"MobileSDK\": {"
195+
+ " \"UseAndroidNativeBrowserForAuthentication\": true,"
196+
+ " \"shareBrowserSessionAndroid\": false"
197+
+ "},"
198+
+ "\"SamlProviders\": [{"
199+
+ " \"SsoUrl\": \"https://example.com/sso\""
200+
+ "}]"
201+
+ "}";
202+
203+
// Create mock HttpAccess to avoid real network calls that would timeout in test environment.
204+
// Mock returns 200 with JSON body for expected=true, 404 for expected=false.
205+
MockHttpAccess mockHttpAccess = new MockHttpAccess(expected, expected ? mockAuthConfigJson : null);
206+
207+
// Call with mocked HttpAccess instead of using DEFAULT to prevent network timeout
208+
AuthConfigUtil.getMyDomainAuthConfig(mockHttpAccess, endpoint);
138209

139210
final Intent intent = receiver.getIntent().get();
140211
Assert.assertTrue("The intent extra should be set", intent.hasExtra(AuthConfigUtil.WAS_REQUEST_SUCCESSFUL_EXTRA));

0 commit comments

Comments
 (0)