Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: MLIBZ-2448 Fix setting client app version #165

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -30,6 +30,7 @@
import com.kinvey.java.auth.KinveyAuthResponse;
import com.kinvey.java.auth.ThirdPartyIdentity;
import com.kinvey.java.core.KinveyClientRequestInitializer;
import com.kinvey.java.core.KinveyHeaders;
import com.kinvey.java.dto.BaseUser;
import com.kinvey.java.dto.DeviceId;
import com.kinvey.java.dto.Email;
Expand Down Expand Up @@ -751,7 +752,9 @@ public LoginRequest(Credential credential) {

public LoginRequest buildAuthRequest() {
this.request = builder.build();
this.request.setKinveyHeaders(((KinveyClientRequestInitializer) client.getKinveyRequestInitializer()).getKinveyHeaders());
KinveyHeaders kinveyHeaders = (((KinveyClientRequestInitializer) client.getKinveyRequestInitializer()).getKinveyHeaders());
kinveyHeaders.set("X-Kinvey-Client-App-Version", clientAppVersion);
this.request.setKinveyHeaders(kinveyHeaders);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only be setting the X-Kinvey-Client-App-Version header if the clientAppVersion has been set by the developer. If the clientAppVersion is null or empty, the header should not be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

return this;
}

Expand Down
27 changes: 27 additions & 0 deletions java-api-core/test/com/kinvey/java/BaseUserTest.java
Expand Up @@ -16,11 +16,15 @@
package com.kinvey.java;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Hashtable;

import com.google.api.client.json.GenericJson;
import com.google.api.client.json.gson.GsonFactory;
import com.kinvey.java.auth.KinveyAuthRequest;
import com.kinvey.java.auth.ThirdPartyIdentity;
import com.kinvey.java.core.KinveyClientRequestInitializer;
import com.kinvey.java.core.KinveyHeaders;
import com.kinvey.java.core.KinveyMockUnitTest;
import com.kinvey.java.dto.BaseUser;
import com.kinvey.java.store.UserStoreRequestManager;
Expand Down Expand Up @@ -314,5 +318,28 @@ public void testMICAPIVersionAppendsV() throws IOException{
assertEquals(getClient().getMICApiVersion(), "v2");
}

public void testClientAppVersionHeader() throws IOException, NoSuchFieldException, IllegalAccessException {
String clientAppVersion = "1.2.3";
getClient().setClientAppVersion(clientAppVersion);
UserStoreRequestManager user = new UserStoreRequestManager(getClient(), createBuilder(getClient()));
assertNotNull(user);
UserStoreRequestManager.LoginRequest loginRequest = user.createBlocking("test_name", "test_login").buildAuthRequest();
Field kinveyAuthRequestField = loginRequest.getClass().getDeclaredField("request"); //NoSuchFieldException
kinveyAuthRequestField.setAccessible(true);
KinveyAuthRequest request = (KinveyAuthRequest) kinveyAuthRequestField.get(loginRequest);
Field kinveyHeadersField = request.getClass().getDeclaredField("kinveyHeaders"); //NoSuchFieldException
kinveyHeadersField.setAccessible(true);
KinveyHeaders kinveyHeaders = (KinveyHeaders) kinveyHeadersField.get(request);
String clientAppVersionHeader = (String) kinveyHeaders.get("X-Kinvey-Client-App-Version");
assertEquals(clientAppVersion, clientAppVersionHeader);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test case to verify that if clientAppVersion is not set, that the X-Kinvey-Client-App-Version header is not added to the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

private KinveyAuthRequest.Builder createBuilder(AbstractClient client) {
String appKey = ((KinveyClientRequestInitializer) client.getKinveyRequestInitializer()).getAppKey();
String appSecret = ((KinveyClientRequestInitializer) client.getKinveyRequestInitializer()).getAppSecret();

return new KinveyAuthRequest.Builder(client.getRequestFactory().getTransport(),
client.getJsonFactory(), client.getBaseUrl(), appKey, appSecret, null);
}

}