diff --git a/scripts/build-test-app.sh b/scripts/build-test-app.sh index 441df5c..0447e35 100755 --- a/scripts/build-test-app.sh +++ b/scripts/build-test-app.sh @@ -9,7 +9,7 @@ CDV=$ROOT/node_modules/.bin/cordova rm -rf $ROOT/temp mkdir $ROOT/temp cp -r $ROOT/test/app-template/ $ROOT/temp/ -cp $ROOT/test/app-test-definitions.js $ROOT/temp/www/js/ +cp $ROOT/test/app-test-definitions.js $ROOT/temp/www/ cd $ROOT/temp $CDV prepare $CDV plugins add $ROOT diff --git a/src/android/com/synconset/cordovahttp/CordovaHttp.java b/src/android/com/synconset/cordovahttp/CordovaHttp.java index 3cf3be9..c1a048a 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttp.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttp.java @@ -28,13 +28,13 @@ abstract class CordovaHttp { private static AtomicBoolean disableRedirect = new AtomicBoolean(false); private String urlString; - private JSONObject params; + private Object params; private String serializerName; private JSONObject headers; private int timeoutInMilliseconds; private CallbackContext callbackContext; - public CordovaHttp(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttp(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) { this.urlString = urlString; this.params = params; this.serializerName = "default"; @@ -43,7 +43,7 @@ abstract class CordovaHttp { this.callbackContext = callbackContext; } - public CordovaHttp(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttp(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { this.urlString = urlString; this.params = params; this.serializerName = serializerName; @@ -78,7 +78,7 @@ abstract class CordovaHttp { return this.urlString; } - protected JSONObject getParamsObject() { + protected Object getParamsObject() { return this.params; } @@ -86,8 +86,12 @@ abstract class CordovaHttp { return this.serializerName; } - protected HashMap getParamsMap() throws JSONException { - return this.getMapFromJSONObject(this.params); + protected HashMap getParamsMap() throws JSONException, Exception { + if (this.params instanceof JSONObject) { + return this.getMapFromJSONObject((JSONObject) this.params); + } else { + throw new Exception("unsupported params type, needs to be a JSON object"); + } } protected JSONObject getHeadersObject() { diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpDelete.java b/src/android/com/synconset/cordovahttp/CordovaHttpDelete.java index f522d1e..e91aab5 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpDelete.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpDelete.java @@ -17,7 +17,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpDelete extends CordovaHttp implements Runnable { - public CordovaHttpDelete(String urlString, JSONObject data, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpDelete(String urlString, Object data, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, data, headers, timeout, callbackContext); } @@ -59,6 +59,8 @@ class CordovaHttpDelete extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpDownload.java b/src/android/com/synconset/cordovahttp/CordovaHttpDownload.java index 53aa517..aa655c7 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpDownload.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpDownload.java @@ -23,7 +23,7 @@ import org.json.JSONObject; class CordovaHttpDownload extends CordovaHttp implements Runnable { private String filePath; - public CordovaHttpDownload(String urlString, JSONObject params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) { + public CordovaHttpDownload(String urlString, Object params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) { super(urlString, params, headers, timeout, callbackContext); this.filePath = filePath; } @@ -69,6 +69,8 @@ class CordovaHttpDownload extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpGet.java b/src/android/com/synconset/cordovahttp/CordovaHttpGet.java index 8ce9a89..a3de313 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpGet.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpGet.java @@ -17,7 +17,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpGet extends CordovaHttp implements Runnable { - public CordovaHttpGet(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpGet(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, params, headers, timeout, callbackContext); } @@ -59,6 +59,8 @@ class CordovaHttpGet extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpHead.java b/src/android/com/synconset/cordovahttp/CordovaHttpHead.java index f545a59..9423046 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpHead.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpHead.java @@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpHead extends CordovaHttp implements Runnable { - public CordovaHttpHead(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpHead(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, params, headers, timeout, callbackContext); } @@ -58,6 +58,8 @@ class CordovaHttpHead extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java index cf88bb9..54894ba 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java @@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpPatch extends CordovaHttp implements Runnable { - public CordovaHttpPatch(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpPatch(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, params, serializerName, headers, timeout, callbackContext); } @@ -65,6 +65,8 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java index 8595659..2cd8ded 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java @@ -36,7 +36,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { if (action.equals("post")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); String serializerName = args.getString(2); JSONObject headers = args.getJSONObject(3); int timeoutInMilliseconds = args.getInt(4) * 1000; @@ -45,7 +45,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { cordova.getThreadPool().execute(post); } else if (action.equals("get")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); JSONObject headers = args.getJSONObject(2); int timeoutInMilliseconds = args.getInt(3) * 1000; CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, timeoutInMilliseconds, callbackContext); @@ -53,7 +53,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { cordova.getThreadPool().execute(get); } else if (action.equals("put")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); String serializerName = args.getString(2); JSONObject headers = args.getJSONObject(3); int timeoutInMilliseconds = args.getInt(4) * 1000; @@ -62,7 +62,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { cordova.getThreadPool().execute(put); } else if (action.equals("patch")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); String serializerName = args.getString(2); JSONObject headers = args.getJSONObject(3); int timeoutInMilliseconds = args.getInt(4) * 1000; @@ -72,7 +72,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { } else if (action.equals("delete")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); JSONObject headers = args.getJSONObject(2); int timeoutInMilliseconds = args.getInt(3) * 1000; CordovaHttpDelete delete = new CordovaHttpDelete(urlString, params, headers, timeoutInMilliseconds, callbackContext); @@ -80,7 +80,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { cordova.getThreadPool().execute(delete); } else if (action.equals("head")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); JSONObject headers = args.getJSONObject(2); int timeoutInMilliseconds = args.getInt(3) * 1000; CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, timeoutInMilliseconds, callbackContext); @@ -103,7 +103,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { callbackContext.success(); } else if (action.equals("uploadFile")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); JSONObject headers = args.getJSONObject(2); String filePath = args.getString(3); String name = args.getString(4); @@ -113,7 +113,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { cordova.getThreadPool().execute(upload); } else if (action.equals("downloadFile")) { String urlString = args.getString(0); - JSONObject params = args.getJSONObject(1); + Object params = args.get(1); JSONObject headers = args.getJSONObject(2); String filePath = args.getString(3); int timeoutInMilliseconds = args.getInt(4) * 1000; diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPost.java b/src/android/com/synconset/cordovahttp/CordovaHttpPost.java index 7fbb2d0..2ebe50e 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPost.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPost.java @@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpPost extends CordovaHttp implements Runnable { - public CordovaHttpPost(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpPost(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, params, serializerName, headers, timeout, callbackContext); } @@ -65,6 +65,8 @@ class CordovaHttpPost extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPut.java b/src/android/com/synconset/cordovahttp/CordovaHttpPut.java index 5d58d8a..4b52630 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpPut.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPut.java @@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest; import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; class CordovaHttpPut extends CordovaHttp implements Runnable { - public CordovaHttpPut(String urlString, JSONObject data, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { + public CordovaHttpPut(String urlString, Object data, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) { super(urlString, data, serializerName, headers, timeout, callbackContext); } @@ -55,7 +55,7 @@ class CordovaHttpPut extends CordovaHttp implements Runnable { } } catch (JSONException e) { this.respondWithError("There was an error generating the response"); - } catch (HttpRequestException e) { + } catch (HttpRequestException e) { if (e.getCause() instanceof UnknownHostException) { this.respondWithError(0, "The host could not be resolved"); } else if (e.getCause() instanceof SocketTimeoutException) { @@ -65,6 +65,8 @@ class CordovaHttpPut extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpUpload.java b/src/android/com/synconset/cordovahttp/CordovaHttpUpload.java index 2224b3c..4f5e8fc 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttpUpload.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttpUpload.java @@ -30,7 +30,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable { private String filePath; private String name; - public CordovaHttpUpload(String urlString, JSONObject params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) { + public CordovaHttpUpload(String urlString, Object params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) { super(urlString, params, headers, timeout, callbackContext); this.filePath = filePath; this.name = name; @@ -97,7 +97,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable { this.respondWithError("There was an error loading the file"); } catch (JSONException e) { this.respondWithError("There was an error generating the response"); - } catch (HttpRequestException e) { + } catch (HttpRequestException e) { if (e.getCause() instanceof UnknownHostException) { this.respondWithError(0, "The host could not be resolved"); } else if (e.getCause() instanceof SocketTimeoutException) { @@ -107,6 +107,8 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable { } else { this.respondWithError("There was an error with the request"); } + } catch (Exception e) { + this.respondWithError(-1, e.getMessage()); } } } diff --git a/test/app-template/www/img/logo.png b/test/app-template/www/img/logo.png deleted file mode 100644 index 9519e7d..0000000 Binary files a/test/app-template/www/img/logo.png and /dev/null differ diff --git a/test/app-template/www/css/index.css b/test/app-template/www/index.css similarity index 90% rename from test/app-template/www/css/index.css rename to test/app-template/www/index.css index cc47e1c..c0452f4 100644 --- a/test/app-template/www/css/index.css +++ b/test/app-template/www/index.css @@ -15,9 +15,15 @@ body { button, input, textarea { display: block; + box-sizing: border-box; width: 100%; } +button { + height: 2.5em; + font-size: 10pt; +} + input { text-align: center; } diff --git a/test/app-template/www/index.html b/test/app-template/www/index.html index 94da10f..24c8e89 100644 --- a/test/app-template/www/index.html +++ b/test/app-template/www/index.html @@ -5,17 +5,17 @@ - +

Advanced HTTP test suite

- - + + - - + + diff --git a/test/app-template/www/js/index.js b/test/app-template/www/index.js similarity index 100% rename from test/app-template/www/js/index.js rename to test/app-template/www/index.js diff --git a/test/app-test-definitions.js b/test/app-test-definitions.js index 626aabc..d1ccf89 100644 --- a/test/app-test-definitions.js +++ b/test/app-test-definitions.js @@ -5,7 +5,8 @@ const hooks = { }; const helpers = { - acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); } + acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); }, + setJsonSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('json')); } }; const tests = [ @@ -51,7 +52,7 @@ const tests = [ } },{ description: 'should accept bad cert (GET)', - expected: 'resolved: {\"status\":200, ...', + expected: 'resolved: {"status":200, ...', before: helpers.acceptAllCerts, func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -60,7 +61,7 @@ const tests = [ } },{ description: 'should accept bad cert (PUT)', - expected: 'rejected: {\"status\":405, ... // will be rejected because PUT is not allowed', + expected: 'rejected: {"status":405, ... // will be rejected because PUT is not allowed', before: helpers.acceptAllCerts, func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -69,7 +70,7 @@ const tests = [ } },{ description: 'should accept bad cert (POST)', - expected: 'rejected: {\"status\":405, ... // will be rejected because POST is not allowed', + expected: 'rejected: {"status":405, ... // will be rejected because POST is not allowed', before: helpers.acceptAllCerts, func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -78,7 +79,7 @@ const tests = [ } },{ description: 'should accept bad cert (PATCH)', - expected: 'rejected: {\"status\":405, ... // will be rejected because PATCH is not allowed', + expected: 'rejected: {"status":405, ... // will be rejected because PATCH is not allowed', before: helpers.acceptAllCerts, func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -87,7 +88,7 @@ const tests = [ } },{ description: 'should accept bad cert (DELETE)', - expected: 'rejected: {\"status\":405, ... // will be rejected because DELETE is not allowed', + expected: 'rejected: {"status":405, ... // will be rejected because DELETE is not allowed', before: helpers.acceptAllCerts, func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, validationFunc: function(driver, result) { @@ -95,14 +96,68 @@ const tests = [ result.data.should.include({ status: 405 }); } },{ - description: 'should fetch data from http://google.com/ (GET)', - expected: 'resolved: {\"status\":200, ...', + description: 'should fetch data from http://httpbin.org/ (GET)', + expected: 'resolved: {"status":200, ...', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); }, + func: function(resolve, reject) { cordova.plugin.http.get('http://httpbin.org/', {}, {}, resolve, reject); }, validationFunc: function(driver, result) { result.type.should.be.equal('resolved'); result.data.should.include({ status: 200 }); } + },{ + description: 'should send JSON object correctly (POST)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql({ test: 'testString' }); + } + },{ + description: 'should send JSON object correctly (PUT)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql({ test: 'testString' }); + } + },{ + description: 'should send JSON object correctly (PATCH)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql({ test: 'testString' }); + } + },{ + description: 'should send JSON array correctly (POST)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]); + } + },{ + description: 'should send JSON array correctly (PUT)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]); + } + },{ + description: 'should send JSON array correctly (PATCH)', + 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) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]); + } } ];