diff --git a/CHANGELOG.md b/CHANGELOG.md index d963112..5de476a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.9.1 + +- Fixed #45: does not encode arrays correctly as HTTP GET parameter on Android + ## v1.9.0 - Feature #44: "getCookieString" method is exposed diff --git a/package.json b/package.json index 2808347..b871497 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "scripts": { "testandroid": "./scripts/build-test-app.sh --android --emulator && ./scripts/test-app.sh --android --emulator", diff --git a/src/android/com/synconset/cordovahttp/CordovaHttp.java b/src/android/com/synconset/cordovahttp/CordovaHttp.java index 5e5c245..0dfd3fb 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttp.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttp.java @@ -6,6 +6,7 @@ package com.synconset.cordovahttp; import org.apache.cordova.CallbackContext; import org.json.JSONException; +import org.json.JSONArray; import org.json.JSONObject; import java.net.SocketTimeoutException; @@ -13,6 +14,7 @@ import java.net.UnknownHostException; import javax.net.ssl.SSLHandshakeException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -191,13 +193,28 @@ abstract class CordovaHttp { return map; } + protected ArrayList getListFromJSONArray(JSONArray array) throws JSONException { + ArrayList list = new ArrayList(); + + for (int i = 0; i < array.length(); i++) { + list.add(array.get(i)); + } + return list; + } + protected HashMap getMapFromJSONObject(JSONObject object) throws JSONException { HashMap map = new HashMap(); Iterator i = object.keys(); while(i.hasNext()) { String key = (String)i.next(); - map.put(key, object.get(key)); + Object value = object.get(key); + + if (value instanceof JSONArray) { + map.put(key, getListFromJSONArray((JSONArray)value)); + } else { + map.put(key, object.get(key)); + } } return map; } diff --git a/test/app-test-definitions.js b/test/app-test-definitions.js index 7d0c850..d7a99c1 100644 --- a/test/app-test-definitions.js +++ b/test/app-test-definitions.js @@ -130,7 +130,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -139,7 +139,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -148,7 +148,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -157,7 +157,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -166,7 +166,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -175,7 +175,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -185,7 +185,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -194,7 +194,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -203,7 +203,7 @@ const tests = [ } },{ 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, func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -267,6 +267,21 @@ const tests = [ .files[fileName] .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'); + } } ];