fix #45: does not encode arrays correctly as HTTP GET parameter on Android

This commit is contained in:
Sefa Ilkimen
2017-11-26 03:42:22 +01:00
parent 55ba423d2f
commit d67e80dfe0
4 changed files with 47 additions and 11 deletions
+4
View File
@@ -1,5 +1,9 @@
# Changelog # Changelog
## 1.9.1
- Fixed #45: does not encode arrays correctly as HTTP GET parameter on Android
## v1.9.0 ## v1.9.0
- Feature #44: "getCookieString" method is exposed - Feature #44: "getCookieString" method is exposed
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "cordova-plugin-advanced-http", "name": "cordova-plugin-advanced-http",
"version": "1.9.0", "version": "1.9.1",
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning", "description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
"scripts": { "scripts": {
"testandroid": "./scripts/build-test-app.sh --android --emulator && ./scripts/test-app.sh --android --emulator", "testandroid": "./scripts/build-test-app.sh --android --emulator && ./scripts/test-app.sh --android --emulator",
@@ -6,6 +6,7 @@ package com.synconset.cordovahttp;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@@ -13,6 +14,7 @@ import java.net.UnknownHostException;
import javax.net.ssl.SSLHandshakeException; import javax.net.ssl.SSLHandshakeException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -191,14 +193,29 @@ abstract class CordovaHttp {
return map; return map;
} }
protected ArrayList<Object> getListFromJSONArray(JSONArray array) throws JSONException {
ArrayList<Object> list = new ArrayList<Object>();
for (int i = 0; i < array.length(); i++) {
list.add(array.get(i));
}
return list;
}
protected HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException { protected HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException {
HashMap<String, Object> map = new HashMap<String, Object>(); HashMap<String, Object> map = new HashMap<String, Object>();
Iterator<?> i = object.keys(); Iterator<?> i = object.keys();
while(i.hasNext()) { while(i.hasNext()) {
String key = (String)i.next(); String key = (String)i.next();
Object value = object.get(key);
if (value instanceof JSONArray) {
map.put(key, getListFromJSONArray((JSONArray)value));
} else {
map.put(key, object.get(key)); map.put(key, object.get(key));
} }
}
return map; return map;
} }
+24 -9
View File
@@ -130,7 +130,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON object correctly (POST)', description: 'should send JSON object correctly (POST)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -139,7 +139,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON object correctly (PUT)', description: 'should send JSON object correctly (PUT)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -148,7 +148,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON object correctly (PATCH)', description: 'should send JSON object correctly (PATCH)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -157,7 +157,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON array correctly (POST) #26', description: 'should send JSON array correctly (POST) #26',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...', expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -166,7 +166,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON array correctly (PUT) #26', description: 'should send JSON array correctly (PUT) #26',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...', expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -175,7 +175,7 @@ const tests = [
} }
},{ },{
description: 'should send JSON array correctly (PATCH) #26', description: 'should send JSON array correctly (PATCH) #26',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...', expected: 'resolved: {"status": 200, "data": "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer, before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -185,7 +185,7 @@ const tests = [
} }
},{ },{
description: 'should send url encoded data correctly (POST) #41', description: 'should send url encoded data correctly (POST) #41',
expected: 'resolved: {"status": 200, data: "{\\"form\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setUrlEncodedSerializer, before: helpers.setUrlEncodedSerializer,
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -194,7 +194,7 @@ const tests = [
} }
},{ },{
description: 'should send url encoded data correctly (PUT)', description: 'should send url encoded data correctly (PUT)',
expected: 'resolved: {"status": 200, data: "{\\"form\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setUrlEncodedSerializer, before: helpers.setUrlEncodedSerializer,
func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -203,7 +203,7 @@ const tests = [
} }
},{ },{
description: 'should send url encoded data correctly (PATCH)', description: 'should send url encoded data correctly (PATCH)',
expected: 'resolved: {"status": 200, data: "{\\"form\\":\\"test\\": \\"testString\\"}\" ...', expected: 'resolved: {"status": 200, "data": "{\\"form\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setUrlEncodedSerializer, before: helpers.setUrlEncodedSerializer,
func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) { validationFunc: function(driver, result) {
@@ -267,6 +267,21 @@ const tests = [
.files[fileName] .files[fileName]
.should.be.equal(fileContent); .should.be.equal(fileContent);
} }
},{
description: 'should encode HTTP array params correctly (GET) #45',
expected: 'resolved: {"status": 200, "data": "{\\"url\\":\\"http://httpbin.org/get?myArray[]=val1&myArray[]=val2&myArray[]=val3\\"}\" ...',
func: function(resolve, reject) {
cordova.plugin.http.get('http://httpbin.org/get', { myArray: [ 'val1', 'val2', 'val3' ], myString: 'testString' }, {}, resolve, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.data.should.be.a('string');
JSON
.parse(result.data.data)
.url
.should.be.equal('http://httpbin.org/get?myArray[]=val1&myArray[]=val2&myArray[]=val3&myString=testString');
}
} }
]; ];