Skip to content

Commit

Permalink
feat: validate required params
Browse files Browse the repository at this point in the history
  • Loading branch information
arifBurakDemiray committed Sep 27, 2023
1 parent de86ef2 commit 0411543
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import ly.count.sdk.java.Config;
import ly.count.sdk.java.Countly;
import org.json.JSONArray;
Expand Down Expand Up @@ -155,6 +156,12 @@ public void getAvailableFeedbackWidgets() {
responseJson.put("result", widgetsJson);

ImmediateRequestI requestMaker = (requestData, customEndpoint, cp, requestShouldBeDelayed, networkingIsEnabled, callback, log) -> {
Assert.assertEquals("/o/sdk", customEndpoint);
Map<String, String> params = TestUtils.parseQueryParams(requestData);
Assert.assertEquals("feedback", params.get("method"));
Assert.assertFalse(requestShouldBeDelayed);
Assert.assertTrue(networkingIsEnabled);
validateRequiredParams(params);
callback.callback(responseJson);
};
SDKCore.instance.config.immediateRequestGenerator = () -> requestMaker;
Expand Down Expand Up @@ -222,6 +229,21 @@ public void getAvailableFeedbackWidgets_null() {
});
}

private void validateRequiredParams(Map<String, String> params) {
int hour = Integer.parseInt(params.get("hour"));
int dow = Integer.parseInt(params.get("dow"));
int tz = Integer.parseInt(params.get("tz"));

Assert.assertEquals(SDKCore.instance.config.getSdkVersion(), params.get("sdk_version"));
Assert.assertEquals(SDKCore.instance.config.getDeviceId().id, params.get("device_id"));
Assert.assertEquals(SDKCore.instance.config.getSdkName(), params.get("sdk_name"));
Assert.assertEquals(SDKCore.instance.config.getServerAppKey(), params.get("app_key"));
Assert.assertTrue(Long.valueOf(params.get("timestamp")) > 0);
Assert.assertTrue(hour > 0 && hour < 24);
Assert.assertTrue(dow >= 0 && dow < 7);
Assert.assertTrue(tz >= -720 && tz <= 840);
}

private CountlyFeedbackWidget createFeedbackWidget(FeedbackWidgetType type, String name, String id, String[] tags) {
CountlyFeedbackWidget widget = new CountlyFeedbackWidget();
widget.type = type;
Expand Down
20 changes: 20 additions & 0 deletions sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,24 @@ private static Map<String, String> parseRequestParams(File file) throws IOExcept
return paramMap;
}
}

/**
* Parse query params from string. String contains are urlencoded and
* separated by "&" symbol and key-value pairs are separated by "=" symbol (key=value).
*
* @param data string with query params
* @return map of query params
*/
public static Map<String, String> parseQueryParams(String data) {
if (data.contains("?")) {
data = data.replace("?", "");
}
String[] params = data.split("&");
Map<String, String> paramMap = new HashMap<>();
for (String param : params) {
String[] pair = param.split("=");
paramMap.put(pair[0], pair[1]);
}
return paramMap;
}
}

0 comments on commit 0411543

Please sign in to comment.