Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Fix #164: allow creation of MobileServiceClient with no app key.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosfigueira committed Apr 10, 2014
1 parent 1a88bb3 commit ae76cee
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,6 @@ private void initialize(URL appUrl, String appKey,
throw new IllegalArgumentException("Invalid Application URL");
}

if (appKey == null || appKey.toString().trim().length() == 0) {
throw new IllegalArgumentException("Invalid Application Key");
}

if (context == null) {
throw new IllegalArgumentException("Context cannot be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,11 @@ private void configureHeadersOnRequest(ServiceFilterRequest request) {
// Set the User Agent header
request.addHeader(HTTP.USER_AGENT, getUserAgent());

// Set the special Application key header
request.addHeader(X_ZUMO_APPLICATION_HEADER, mClient.getAppKey());
// Set the special Application key header, if present
String appKey = mClient.getAppKey();
if (appKey != null && appKey.trim().length() > 0) {
request.addHeader(X_ZUMO_APPLICATION_HEADER, mClient.getAppKey());
}

// Set the special Installation ID header
request.addHeader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.windowsazure.mobileservices.ApiJsonOperationCallback;
import com.microsoft.windowsazure.mobileservices.MobileServiceAuthenticationProvider;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable;
Expand Down Expand Up @@ -89,26 +90,80 @@ public void testNewMobileServiceClientWithNullAppUrlShouldThrowException() {
}
}

public void testNewMobileServiceClientWithEmptyAppKeyShouldThrowException() {
try {
new MobileServiceClient(appUrl, "", getInstrumentation().getTargetContext());
fail("Expected Exception MalformedURLException");
} catch (IllegalArgumentException e) {
// do nothing, it's OK
} catch (MalformedURLException e) {
fail("This should not happen");
}
private void mobileServiceClientWithoutAppKeyShouldNotSendXZumoApplicationHeader(final String appKeyValue) throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);

// Container to store callback's results and do the asserts.
final ResultsContainer container = new ResultsContainer();

runTestOnUiThread(new Runnable() {

@Override
public void run() {
MobileServiceClient client = null;
try {
client = new MobileServiceClient(appUrl, appKeyValue, getInstrumentation().getTargetContext());
} catch (MalformedURLException e) {
e.printStackTrace();
}

client = client.withFilter(new ServiceFilter() {

@Override
public void handleRequest(ServiceFilterRequest request, NextServiceFilterCallback nextServiceFilterCallback,
ServiceFilterResponseCallback responseCallback) {
String requestAppKey = null;
for (Header header : request.getHeaders()) {
if (header.getName().equalsIgnoreCase("x-zumo-application")) {
requestAppKey = header.getValue();
}
}

ServiceFilterResponseMock response = new ServiceFilterResponseMock();
response.setStatus(new StatusLineMock(200));
response.setContent("{\"appKeyPresent\":" +
(requestAppKey == null ? "false" : "true") + "}");

// create a mock request to replace the existing one
ServiceFilterRequestMock requestMock = new ServiceFilterRequestMock(response);
nextServiceFilterCallback.onNext(requestMock, responseCallback);
}
});

try {
client.invokeApi("someApi", new ApiJsonOperationCallback() {

@Override
public void onCompleted(JsonElement jsonObject, Exception exception,
ServiceFilterResponse response) {
// TODO Auto-generated method stub
container.setJsonResult(jsonObject);
container.setException(exception);
latch.countDown();
}
});
} catch (Exception e) {
latch.countDown();
}
}
});

latch.await();

// Asserts
assertNull(container.getException());
JsonObject result = container.getJsonResult().getAsJsonObject();
assertNotNull(result);
boolean hasAppKeyHeader = result.get("appKeyPresent").getAsBoolean();
assertFalse(hasAppKeyHeader);
}

public void testNewMobileServiceClientWithNullAppKeyShouldThrowException() {
try {
new MobileServiceClient(appUrl, null, getInstrumentation().getTargetContext());
fail("Expected Exception MalformedURLException");
} catch (IllegalArgumentException e) {
// do nothing, it's OK
} catch (MalformedURLException e) {
fail("This should not happen");
}
public void testNewMobileServiceClientWithEmptyAppKeyShouldNotSendXZumoApplicationHeader() throws Throwable {
this.mobileServiceClientWithoutAppKeyShouldNotSendXZumoApplicationHeader("");
}

public void testNewMobileServiceClientWithNullAppKeyShouldNotSendXZumoApplicationHeader() throws Throwable {
this.mobileServiceClientWithoutAppKeyShouldNotSendXZumoApplicationHeader(null);
}

public void testMobileServiceClientWithNullServiceFilterShouldThrowException() {
Expand Down

0 comments on commit ae76cee

Please sign in to comment.