Compare commits

..
9 Commits
9 changed files with 234 additions and 19 deletions
+6
View File
@@ -1,5 +1,11 @@
# Changelog
## 1.9.1
- Fixed #45: does not encode arrays correctly as HTTP GET parameter on Android
- Fixed #54: requests are not responding on iOS with non-string values in header object
- Fixed #58: white-list of allowed content-types should be removed for iOS
## v1.9.0
- Feature #44: "getCookieString" method is exposed
+6 -5
View File
@@ -64,7 +64,7 @@ cordova.plugin.http.useBasicAuth('user', 'password');
```
### setHeader
Set a header for all future requests to a specified host. Takes a hostname, a header and a value.
Set a header for all future requests to a specified host. Takes a hostname, a header and a value (must be a string value).
```js
cordova.plugin.http.setHeader('Hostname', 'Header', 'Value');
@@ -293,12 +293,13 @@ cordova.plugin.http.downloadFile("https://google.com/", {
## Libraries
This plugin utilizes some awesome open source networking libraries. These are both MIT licensed:
This plugin utilizes some awesome open source libraries:
- iOS - [AFNetworking](https://github.com/AFNetworking/AFNetworking)
- Android - [http-request](https://github.com/kevinsawicki/http-request)
- iOS - [AFNetworking](https://github.com/AFNetworking/AFNetworking) (MIT licensed)
- Android - [http-request](https://github.com/kevinsawicki/http-request) (MIT licensed)
- Cookie handling - [tough-cookie](https://github.com/salesforce/tough-cookie) (BSD-3-Clause licensed)
We made a few modifications to both of them.
We made a few modifications to the networking libraries.
## Contribute & Develop
+1 -1
View File
@@ -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",
+1 -1
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-advanced-http" version="1.9.0">
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-advanced-http" version="1.9.1">
<name>Advanced HTTP plugin</name>
<description>
Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning
@@ -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<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 {
HashMap<String, Object> map = new HashMap<String, Object>();
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;
}
+2 -1
View File
@@ -19,11 +19,12 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"text/json", @"application/hal+json", @"application/json", @"text/xml", @"application/xml", @"text/css", @"application/javascript", nil];
self.acceptableContentTypes = nil;
return self;
}
+16 -1
View File
@@ -33,6 +33,16 @@ const app = {
};
},
throw: function(error) {
document.getElementById('statusInput').value = 'finished';
app.printResult('result - throwed', error.message);
app.lastResult = {
type: 'throwed',
message: error.message
};
},
getResult: function(cb) {
cb(app.lastResult);
},
@@ -46,7 +56,12 @@ const app = {
document.getElementById('expectedTextarea').value = expectedText;
document.getElementById('resultTextarea').value = '';
document.getElementById('descriptionLbl').innerText = titleText;
testDefinition.func(app.resolve, app.reject);
try {
testDefinition.func(app.resolve, app.reject);
} catch (error) {
app.throw(error);
}
},
onBeforeTest: function(testIndex, cb) {
+124 -9
View File
@@ -1,5 +1,6 @@
const hooks = {
onBeforeEachTest: function(done) {
cordova.plugin.http.clearCookies();
cordova.plugin.http.acceptAllCerts(false, done, done);
}
};
@@ -130,7 +131,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 +140,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 +149,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 +158,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 +167,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 +176,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 +186,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 +195,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 +204,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 +268,120 @@ 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');
}
},{
description: 'should reject non-string values in local header object #54',
expected: 'rejected: {"status": 0, "error": "advanced-http: header values must be strings" ...',
func: function(resolve, reject) {
cordova.plugin.http.get('http://httpbin.org/get', {}, { myTestHeader: 1 }, resolve, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.error.should.be.equal('advanced-http: header values must be strings');
}
},{
description: 'should throw an error while setting non-string value as global header #54',
expected: 'throwed: "advanced-http: header values must be strings"',
func: function(resolve, reject) {
cordova.plugin.http.setHeader('myTestHeader', 2);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('throwed');
result.message.should.be.equal('advanced-http: header values must be strings');
}
},{
description: 'should accept content-type "application/xml" #58',
expected: 'resolved: {"status": 200, ...',
func: function(resolve, reject) {
cordova.plugin.http.get('http://httpbin.org/xml', {}, {}, resolve, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.status.should.be.equal(200);
}
},{
description: 'should send programmatically set cookies correctly (GET)',
expected: 'resolved: {"status": 200, ...',
func: function(resolve, reject) {
cordova.plugin.http.setCookie('http://httpbin.org/get', 'myCookie=myValue');
cordova.plugin.http.setCookie('http://httpbin.org/get', 'mySecondCookie=mySecondValue');
cordova.plugin.http.get('http://httpbin.org/get', {}, {}, resolve, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.data.should.be.a('string');
JSON
.parse(result.data.data)
.headers
.Cookie
.should.be.equal('myCookie=myValue; mySecondCookie=mySecondValue');
}
},{
description: 'should not send any cookies after running "clearCookies" (GET) #59',
expected: 'resolved: {"status": 200, "data": "{\"headers\": {\"Cookie\": \"\"...',
func: function(resolve, reject) {
cordova.plugin.http.setCookie('http://httpbin.org/get', 'myCookie=myValue');
cordova.plugin.http.setCookie('http://httpbin.org/get', 'mySecondCookie=mySecondValue');
cordova.plugin.http.clearCookies();
cordova.plugin.http.get('http://httpbin.org/get', {}, {}, resolve, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.data.should.be.a('string');
JSON
.parse(result.data.data)
.headers
.Cookie
.should.be.equal('');
}
},{
description: 'should send programmatically set cookies correctly (DOWNLOAD) #57',
expected: 'resolved: {"content":{"cookies":{"myCookie":"myValue ...',
func: function(resolve, reject) {
var sourceUrl = 'http://httpbin.org/cookies';
var targetPath = cordova.file.cacheDirectory + 'cookies.json';
cordova.plugin.http.setCookie('http://httpbin.org/get', 'myCookie=myValue');
cordova.plugin.http.setCookie('http://httpbin.org/get', 'mySecondCookie=mySecondValue');
cordova.plugin.http.downloadFile(sourceUrl, {}, {}, targetPath, function(entry) {
helpers.getWithXhr(function(content) {
resolve({
sourceUrl: sourceUrl,
targetPath: targetPath,
fullPath: entry.fullPath,
name: entry.name,
content: content
});
}, targetPath);
}, reject);
},
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.name.should.be.equal('cookies.json');
result.data.content.should.be.a('string');
var cookies = JSON.parse(result.data.content).cookies;
cookies.myCookie.should.be.equal('myValue');
cookies.mySecondCookie.should.be.equal('mySecondValue');
}
}
];
+60
View File
@@ -42,6 +42,7 @@ var cookieHandler = require(pluginId + '.cookie-handler');
var MANDATORY_SUCCESS = 'advanced-http: missing mandatory "onSuccess" callback function';
var MANDATORY_FAIL = 'advanced-http: missing mandatory "onFail" callback function';
var ADDING_COOKIES_NOT_SUPPORTED = 'advanced-http: "setHeader" does not support adding cookies, please use "setCookie" function instead';
var HEADER_VALUE_MUST_BE_STRING = 'advanced-http: header values must be strings';
// Thanks Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
function b64EncodeUnicode(str) {
@@ -65,6 +66,29 @@ function mergeHeaders(globalHeaders, localHeaders) {
return localHeaders;
}
function checkHeaders(headers) {
var keys = Object.keys(headers);
var key;
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (typeof headers[key] !== 'string') {
return false;
}
}
return true;
}
function onInvalidHeader(handler) {
handler({
status: 0,
error: HEADER_VALUE_MUST_BE_STRING,
headers: {}
});
}
function checkSerializer(serializer) {
serializer = serializer || '';
serializer = serializer.trim().toLowerCase();
@@ -173,6 +197,10 @@ var http = {
throw new Error(ADDING_COOKIES_NOT_SUPPORTED);
}
if (typeof value !== 'string') {
throw new Error(HEADER_VALUE_MUST_BE_STRING);
}
this.headers[host] = this.headers[host] || {};
this.headers[host][header] = value;
},
@@ -212,6 +240,10 @@ var http = {
data = data || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -223,6 +255,10 @@ var http = {
params = params || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -234,6 +270,10 @@ var http = {
data = data || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -246,6 +286,10 @@ var http = {
data = data || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -258,6 +302,10 @@ var http = {
params = params || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -269,6 +317,10 @@ var http = {
params = params || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -280,6 +332,10 @@ var http = {
params = params || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -291,6 +347,10 @@ var http = {
params = params || {};
headers = getMergedHeaders(url, headers, this.headers);
if (!checkHeaders(headers)) {
return onInvalidHeader(failure);
}
var onSuccess = injectCookieHandler(url, injectFileEntryHandler(success));
var onFail = injectCookieHandler(url, failure);