Compare commits

...

32 Commits

Author SHA1 Message Date
Sefa Ilkimen
3ea9fd4fd1 release v1.7.0 2017-10-24 14:08:06 +02:00
Sefa Ilkimen
56791016e6 updated version number 2017-10-24 14:06:19 +02:00
Sefa Ilkimen
9655c89486 - added indicator for running tests
- validation function is triggered when test is finished
2017-10-23 16:54:34 +02:00
Sefa Ilkimen
15bf6281bc added missing documentation for "patch " method 2017-10-20 17:50:16 +02:00
Sefa Ilkimen
d342ca054a updated readme and changelog 2017-10-20 17:36:46 +02:00
Sefa Ilkimen
d7e13b046b - feature #24: extended behaviour of "setHeader" method
- added test suite for www interface of plugin (js files)
2017-10-20 17:22:51 +02:00
Sefa Ilkimen
1375727316 release v1.6.2 2017-10-19 17:58:48 +02:00
Sefa Ilkimen
d136d90893 - improved release process (using npm script)
- updated version number
2017-10-19 17:56:30 +02:00
Sefa Ilkimen
a8f1d2b1c2 changed behaviour #29: removed "validateDomainName" function and disabling it when "acceptAllCerts" is enabled 2017-10-19 16:24:54 +02:00
Sefa Ilkimen
c29ce85df3 Merge pull request #28 from ryandegruyter/patch-1
(iOS) Add support for Content-Type application/hal+json
2017-10-19 09:42:13 +02:00
Sefa Ilkimen
aeefc9f5ef tests: increased timeout value because test vm takes long time to spin up 2017-10-16 10:57:05 +02:00
Sefa Ilkimen
4ea9c24d6d travis config: added travis_wait to prevent timeout issue 2017-10-16 10:17:05 +02:00
Sefa Ilkimen
7037fd2fa7 - improved test app
- defined new tests
2017-10-13 16:30:48 +02:00
Sefa Ilkimen
6f210a70fb fixed issue #31 - request fails throwing error on erroneous cookies 2017-10-13 12:16:29 +02:00
Sefa Ilkimen
10eb102d43 fixed travis config (jwt section) 2017-10-12 09:08:03 +02:00
Sefa Ilkimen
9dc0bd56e0 WIP: integrating saucelabs tests 2017-10-11 16:33:07 +02:00
Ryan de Gruyter
190c44b30a Add support for Content-Type application/hal+json
Currently when the server responds with a content type of application/hal+json, the response might be 200 but the AFNetwork will call the fail callback anyway because the content type is not acceptable.
2017-10-09 13:34:40 +02:00
Sefa Ilkimen
807af7adfa release 1.6.1 2017-10-04 13:01:50 +02:00
Sefa Ilkimen
dd66698f96 updated version number 2017-10-04 13:00:32 +02:00
Sefa Ilkimen
3476ff7ffb added script which is updating version number in plugin.xml automatically 2017-10-04 12:59:33 +02:00
Sefa Ilkimen
52b2efe71b fixed PATCH method on android 2017-10-02 16:17:29 +02:00
Sefa Ilkimen
8b4c6de4b9 implemented PATCH method for iOS 2017-10-02 12:29:55 +02:00
Sefa Ilkimen
3138d423ee temporary removed patch method in www interface 2017-10-02 11:09:05 +02:00
Sefa Ilkimen
f0194ad55a Merge pull request #22 from akhatri/feature-patch-method-android
Feature patch method android
2017-10-02 10:50:12 +02:00
Akshat Khatri - AK
09da3c9a97 patch method android 2017-10-01 17:26:20 +11:00
Akshat Khatri - AK
52ccfc6aca Merge remote-tracking branch 'refs/remotes/silkimen/master' into feature-patch-method-android 2017-10-01 17:11:41 +11:00
Sefa Ilkimen
fc89a64675 update changelog and version number 2017-09-29 16:42:36 +02:00
Sefa Ilkimen
4954385361 - fix issue #16: removed build script and prebuild hook
- implement release script
2017-09-29 16:35:34 +02:00
Sefa Ilkimen
08554c02cb Merge pull request #21 from kesozjura/master
Add global redirection control
2017-09-29 15:51:56 +02:00
Martin Kasa
18a2fc0c69 temporary umd-tough-cookie.js fix 2017-09-29 14:44:44 +02:00
Martin Kasa
a4a6440bcb Add global redirection control
copied from https://github.com/wymsee/cordova-HTTP/pull/137
2017-09-29 14:36:00 +02:00
Akshat Khatri - AK
792515e1c6 patch method android 2017-09-29 21:02:44 +10:00
46 changed files with 6047 additions and 195 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,5 @@
node_modules/**
tags
.zedstate
/www/umd-tough-cookie.js
npm-debug.log
/temp

View File

@@ -2,3 +2,4 @@ tags
.zedstate
/scripts
/temp
/test

View File

@@ -1,6 +1,44 @@
language: node_js
node_js:
- lts/*
sudo: false
language: objective-c
os: osx
osx_image: xcode9.1
cache: false
addons:
sauce_connect:
username: silkimen
jwt: RZHC8x5ggrXdu1xzhhHmQDwege9s3putJXY1ZG8+MNMaBTLfLBWzFsHuR6Lvz+XnrAT/CTGG957kw6ikSaV6VEkGvtO91irZoJyShjPq2hBbn2CaEbo91Eq8iNYaRG+4FVMxoHa6xpF/HtvOLQdIkURc/lzLj/BOFMmtV/d3adLDcryQZuYB211iN4d6exGHMQe8uvJdkCy4ac85guhF8KmAELyaVa0rx0m6gNaszWU9BJ8Rn+4HJbJtfpQcU1bJN43Dx8forRU/Com53K+KFtDKPUdBHZnuENrZrQ+8RU59vdLrHwsCvP9elJFkcX9xk/r45oUGXhqVV3vMyaqTa9YUO772rhgKuAi4TqiaR51H58KWqynp7wOZwL5FqJs0KzL5rNDDIoKKrklnOidS4RAUKXCi1CUNXwDdOo4Kd96cYOIHqpwa7Beci8CtVlzd/49yE9/AXnZYVwp10uKZvJJdliAOHhVEAtCIp8PzHOOPYJsXvUcEU4ORWWWpusBAz6wFiLOL4d/CAHNj6wDFliRexBrv/Wunu5ifva4oVJWrtAtyGDtsX8kBkf8OcIdy/aPUIIBrbW1sapjPFymMGeL/KkSZRjEy7KjFZKZh9L7sJE56sq+IUremFbcyaX1E3rho6Cs3ongLaKhL9f9vz8EFs7//w8pNlNS9EUdOU68=
before_install:
- export LANG=en_US.UTF-8
- brew update
install:
- npm install
- brew install gradle
- wget http://dl.google.com/android/android-sdk_r24.4-macosx.zip
- tar -xvf android-sdk_r24.4-macosx.zip
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter platform-tools
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter build-tools-25.0.0
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter android-25
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-android-support
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-android-m2repository
- echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-google-m2repository
- export ANDROID_HOME=./android-sdk-macosx
- export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools/23.0.2
script:
- travis_wait scripts/build-test-app.sh
- scripts/upload-artifact.sh
- npm run testjs
- scripts/test-app.sh --ios --emulator
after_success:
deploy:
notifications:
slack:
secure: lXE+2AgsxZU5G5dI91LkMAIgo8MAWfdM7DB5UOtn5LpuNln+2FmJo1gOI7tkdmLOqpXTGYnpI2VyQN3H4nOF21YhuouzD1Sh8n2wtQg1iTm353kuQpqiVhSBX8ZJ7Be1e1G8OsnxoYOxbs4Zo9qI40EruwkvqLCBHWM5MRGyd4M7EFWwb9Z29VZN0y1Nt5g/c3bT76kdKmF+JCLur2OeEKxAity7sIKgZekSqeIMwEVLSxXnda6Dbjc/cg0MJ0iDArkD7iu6fz/Fcrrxgm/pUxjcgvqze7Gy5i31mjEfspnrglWV1cshMd48BTDKCJ2AMmxH8O3GPSWE2txjIvGRWUve7iViNylvmQCVz3Eyf99+4EuuVGa+5PSodQ/CqODx/65EwtcN3PE1tNz2puKOK8nrOJcFkcbG8KTHKUlQtHCkjitbykUnj/hvhLK5/oWlQYVOLWWrHwdGUh8FI8aFPVGjRjWbHbhdayjEIqxwr1ns+6mYrP1EFNXbaeZxnLNC59XpJl1ifuezqYAk7YEiU5j4rtC7YKgyQ3ueb7anOHTJoTMyDn8mpZXgwuyhoBaeEYytQVgRyMtL6Y5cP98Jn2kv0+vdne3rkk9/JEBTo32HOjvoij6rsqEvXC0LhUDJSNadOVdHht0jjoN6zBH37HIE5/3zysLlPcAcHAS83ow=

View File

@@ -1,6 +1,33 @@
# Changelog
## v.1.5.10
## v1.7.0
- Feature #24: "setHeader" allows configuring headers for specified host
## v1.6.2
- Change #29: removed "validateDomainName" (see info notice)
- Fixed #31: request fails throwing error on erroneous cookies
- Fixed #28: added support for content type "application/hal+json" on iOS (thanks ryandegruyter)
#### Important information
We've decided to remove the `validateDomainName()` method, because people were complaining that `acceptAllCerts(true)` is not behaving as expected. And also it's not a good idea to disable domain name validation while using valid certs, because it pretends having a secure connection, but it isn't.
You should either use valid certs with domain name validation enabled (safe for production use) or accept any certs without domain name validation (only for private dev environments). I strongly discourage using fake certs in public networks.
Therefore we are disabling domain name validation automatically, when you set `acceptAllCerts(true)`. So if you were using `validateDomainName()` function, you need to remove this function call for v1.6.2+.
## v1.6.1
- Fixed #23: PATCH method broken on android
## v1.6.0
- Feature #18: implemented PATCH method (thanks akhatri for android implementation)
- Feature #21: added redirection control (thanks to notsyncing and kesozjura)
- Fixed #16: cordova tries to run build script during plugin install
## v1.5.10
- Fixed #10: fix gzip decompression when request header accepts gzip compression (thanks to DayBr3ak)
- Fixed #13: fix angular integration for `setDataSerializer` (thanks to RangerRick)

View File

@@ -1,7 +1,11 @@
Cordova Advanced HTTP
=====================
[![npm version](https://badge.fury.io/js/cordova-plugin-advanced-http.svg)](https://badge.fury.io/js/cordova-plugin-advanced-http)
[![downloads/month](https://img.shields.io/npm/dm/cordova-plugin-advanced-http.svg)](https://www.npmjs.com/package/cordova-plugin-advanced-http)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.png)](https://opensource.org/licenses/mit-license.php)
[![Build Status](https://travis-ci.org/silkimen/cordova-plugin-advanced-http.svg?branch=master)](https://travis-ci.org/silkimen/cordova-plugin-advanced-http)
Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and Android.
This is a fork of [Wymsee's Cordova-HTTP plugin](https://github.com/wymsee/cordova-HTTP).
@@ -44,22 +48,32 @@ You can then inject the cordovaHTTP service into your controllers. The function
### getBasicAuthHeader
This returns an object representing a basic HTTP Authorization header of the form `{'Authorization': 'Basic base64encodedusernameandpassword'}`
var header = cordovaHTTP.getBasicAuthHeader("user", "password");
var header = cordova.plugin.http.getBasicAuthHeader("user", "password");
### useBasicAuth
This sets up all future requests to use Basic HTTP authentication with the given username and password.
cordovaHTTP.useBasicAuth("user", "password");
cordova.plugin.http.useBasicAuth("user", "password");
### setHeader
Set a header for all future requests. Takes a header and a value.
Set a header for all future requests to a specified host. Takes a hostname, a header and a value.
cordovaHTTP.setHeader("Header", "Value");
cordova.plugin.http.setHeader("Hostname", "Header", "Value");
You can also define headers used for all hosts by using wildcard character "\*" or providing only two params.
cordova.plugin.http.setHeader("\*", "Header", "Value");
cordova.plugin.http.setHeader("Header", "Value");
### disableRedirect
If set to `true`, it won't follow redirects automatically. This is a global setting.
cordova.plugin.http.disableRedirect(true);
### setDataSerializer
Set the data serializer which will be used for all future POST and PUT requests. Takes a string representing the name of the serializer.
Set the data serializer which will be used for all future PATCH, POST and PUT requests. Takes a string representing the name of the serializer.
cordovaHTTP.setDataSerializer("urlencoded");
cordova.plugin.http.setDataSerializer("urlencoded");
You can choose one of these two:
* `urlencoded`: send data as url encoded content in body (content type "application/x-www-form-urlencoded")
@@ -70,12 +84,12 @@ Caution: `urlencoded` does not support serializing deep structures whereas `json
### setRequestTimeout
Set how long to wait for a request to respond, in seconds.
cordovaHTTP.setRequestTimeout(5.0);
cordova.plugin.http.setRequestTimeout(5.0);
### clearCookies
Clear the cookie store.
cordovaHTTP.clearCookies();
cordova.plugin.http.clearCookies();
## Asynchronous Functions
These functions all take success and error callbacks as their last 2 arguments.
@@ -87,7 +101,7 @@ To use SSL pinning you must include at least one .cer SSL certificate in your ap
As an alternative, you can store your .cer files in the www/certificates folder.
cordovaHTTP.enableSSLPinning(true, function() {
cordova.plugin.http.enableSSLPinning(true, function() {
console.log('success!');
}, function() {
console.log('error :(');
@@ -96,25 +110,19 @@ As an alternative, you can store your .cer files in the www/certificates folder.
### acceptAllCerts
Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.
cordovaHTTP.acceptAllCerts(true, function() {
cordova.plugin.http.acceptAllCerts(true, function() {
console.log('success!');
}, function() {
console.log('error :(');
});
### validateDomainName
Whether or not to validate the domain name in the certificate. This defaults to true.
cordovaHTTP.validateDomainName(false, function() {
console.log('success!');
}, function() {
console.log('error :(');
});
This function was removed in v1.6.2. Domain name validation is disabled automatically when you enable "acceptAllCerts".
### removeCookies
Remove all cookies associated with a given URL.
cordovaHTTP.removeCookies(url);
cordova.plugin.http.removeCookies(url);
### post<a name="post"></a>
Execute a POST request. Takes a URL, data, and headers.
@@ -132,7 +140,7 @@ The success function receives a response object with 3 properties: status, data,
Most apis will return JSON meaning you'll want to parse the data like in the example below:
cordovaHTTP.post("https://google.com/", {
cordova.plugin.http.post("https://google.com/", {
id: 12,
message: "test"
}, { Authorization: "OAuth2: token" }, function(response) {
@@ -168,7 +176,7 @@ The error function receives a response object with 3 properties: status, error a
### get
Execute a GET request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
cordovaHTTP.get("https://google.com/", {
cordova.plugin.http.get("https://google.com/", {
id: 12,
message: "test"
}, { Authorization: "OAuth2: token" }, function(response) {
@@ -180,6 +188,9 @@ Execute a GET request. Takes a URL, parameters, and headers. See the [post](#p
### put
Execute a PUT request. Takes a URL, data, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
### patch
Execute a PATCH request. Takes a URL, data, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
### delete
Execute a DELETE request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
@@ -189,7 +200,7 @@ Execute a HEAD request. Takes a URL, parameters, and headers. See the [post](#
### uploadFile
Uploads a file saved on the device. Takes a URL, parameters, headers, filePath, and the name of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure.
cordovaHTTP.uploadFile("https://google.com/", {
cordova.plugin.http.uploadFile("https://google.com/", {
id: 12,
message: "test"
}, { Authorization: "OAuth2: token" }, "file:///somepicture.jpg", "picture", function(response) {
@@ -201,7 +212,7 @@ Uploads a file saved on the device. Takes a URL, parameters, headers, filePath,
### downloadFile
Downloads a file and saves it to the device. Takes a URL, parameters, headers, and a filePath. See [post](#post) documentation for details on what is returned on failure. On success this function returns a cordova [FileEntry object](http://cordova.apache.org/docs/en/3.3.0/cordova_file_file.md.html#FileEntry).
cordovaHTTP.downloadFile("https://google.com/", {
cordova.plugin.http.downloadFile("https://google.com/", {
id: 12,
message: "test"
}, { Authorization: "OAuth2: token" }, "file:///somepicture.jpg", function(entry) {
@@ -222,4 +233,4 @@ This plugin utilizes some awesome open source networking libraries. These are bo
- iOS - [AFNetworking](https://github.com/AFNetworking/AFNetworking)
- Android - [http-request](https://github.com/kevinsawicki/http-request)
We made a few modifications to http-request.
We made a few modifications to both of them.

View File

@@ -1,11 +1,12 @@
{
"name": "cordova-plugin-advanced-http",
"version": "1.5.10",
"version": "1.7.0",
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
"scripts": {
"build": "cp node_modules/umd-tough-cookie/lib/umd-tough-cookie.js www/umd-tough-cookie.js",
"prepublish": "npm run build",
"test": "npm run build && ./scripts/test-installation.sh"
"testapp": "./scripts/build-test-app.sh && ./scripts/test-app.sh --ios --emulator",
"testjs": "mocha ./test/js-mocha-specs.js",
"test": "npm run testjs && npm run testapp",
"release": "npm run test && ./scripts/release.sh"
},
"cordova": {
"id": "cordova-plugin-advanced-http",
@@ -51,7 +52,15 @@
},
"homepage": "https://github.com/silkimen/cordova-plugin-advanced-http#readme",
"devDependencies": {
"chai": "4.1.2",
"chai-as-promised": "7.1.1",
"colors": "1.1.2",
"cordova": "7.0.1",
"umd-tough-cookie": "2.3.2"
"mocha": "4.0.0",
"mock-require": "2.0.2",
"mz": "2.7.0",
"umd-tough-cookie": "2.3.2",
"wd": "1.4.1",
"xml2js": "0.4.19"
}
}

View File

@@ -1,95 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.5.10">
<name>Advanced HTTP plugin</name>
<description>
<?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.7.0">
<name>Advanced HTTP plugin</name>
<description>
Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning
</description>
<engines>
<engine name="cordova" version=">=4.0.0" />
</engines>
<dependency id="cordova-plugin-file" version=">=2.0.0" />
<js-module src="www/lodash.js" name="lodash">
</js-module>
<js-module src="www/umd-tough-cookie.js" name="tough-cookie">
</js-module>
<js-module src="www/local-storage-store.js" name="local-storage-store">
</js-module>
<js-module src="www/cookie-handler.js" name="cookie-handler">
</js-module>
<js-module src="www/angular-integration.js" name="angular-integration">
</js-module>
<js-module src="www/advanced-http.js" name="http">
<clobbers target="cordova.plugin.http" />
</js-module>
<!-- ios -->
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="CordovaHttpPlugin">
<param name="ios-package" value="CordovaHttpPlugin"/>
</feature>
</config-file>
<header-file src="src/ios/CordovaHttpPlugin.h" />
<source-file src="src/ios/CordovaHttpPlugin.m" />
<header-file src="src/ios/TextResponseSerializer.h" />
<source-file src="src/ios/TextResponseSerializer.m" />
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h" />
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m" />
<header-file src="src/ios/AFNetworking/AFNetworking.h" />
<header-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.h" />
<source-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.m" />
<header-file src="src/ios/AFNetworking/AFSecurityPolicy.h" />
<source-file src="src/ios/AFNetworking/AFSecurityPolicy.m" />
<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h" />
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m" />
<header-file src="src/ios/AFNetworking/AFURLResponseSerialization.h" />
<source-file src="src/ios/AFNetworking/AFURLResponseSerialization.m" />
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h" />
<source-file src="src/ios/AFNetworking/AFURLSessionManager.m" />
<framework src="Security.framework" />
<framework src="SystemConfiguration.framework" />
</platform>
<!--android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="CordovaHttpPlugin">
<param name="android-package" value="com.synconset.cordovahttp.CordovaHttpPlugin"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
</config-file>
<source-file src="src/android/com/github/kevinsawicki/http/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
<source-file src="src/android/com/github/kevinsawicki/http/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttp.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDelete.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDownload.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpGet.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpHead.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPost.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPut.java" target-dir="src/com/synconset/cordovahttp" />
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp" />
</platform>
</plugin>
<engines>
<engine name="cordova" version=">=4.0.0"/>
</engines>
<dependency id="cordova-plugin-file" version=">=2.0.0"/>
<js-module src="www/lodash.js" name="lodash"/>
<js-module src="www/umd-tough-cookie.js" name="tough-cookie"/>
<js-module src="www/local-storage-store.js" name="local-storage-store"/>
<js-module src="www/cookie-handler.js" name="cookie-handler"/>
<js-module src="www/angular-integration.js" name="angular-integration"/>
<js-module src="www/advanced-http.js" name="http">
<clobbers target="cordova.plugin.http"/>
</js-module>
<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="CordovaHttpPlugin">
<param name="ios-package" value="CordovaHttpPlugin"/>
</feature>
</config-file>
<header-file src="src/ios/CordovaHttpPlugin.h"/>
<header-file src="src/ios/TextResponseSerializer.h"/>
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h"/>
<header-file src="src/ios/AFNetworking/AFNetworking.h"/>
<header-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.h"/>
<header-file src="src/ios/AFNetworking/AFSecurityPolicy.h"/>
<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h"/>
<header-file src="src/ios/AFNetworking/AFURLResponseSerialization.h"/>
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
<source-file src="src/ios/CordovaHttpPlugin.m"/>
<source-file src="src/ios/TextResponseSerializer.m"/>
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m"/>
<source-file src="src/ios/AFNetworking/AFNetworkReachabilityManager.m"/>
<source-file src="src/ios/AFNetworking/AFSecurityPolicy.m"/>
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m"/>
<source-file src="src/ios/AFNetworking/AFURLResponseSerialization.m"/>
<source-file src="src/ios/AFNetworking/AFURLSessionManager.m"/>
<framework src="Security.framework"/>
<framework src="SystemConfiguration.framework"/>
</platform>
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="CordovaHttpPlugin">
<param name="android-package" value="com.synconset.cordovahttp.CordovaHttpPlugin"/>
</feature>
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET"/>
</config-file>
<source-file src="src/android/com/github/kevinsawicki/http/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http"/>
<source-file src="src/android/com/github/kevinsawicki/http/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttp.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDelete.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDownload.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpGet.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpHead.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPost.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPut.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPatch.java" target-dir="src/com/synconset/cordovahttp"/>
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
</platform>
</plugin>

16
scripts/build-test-app.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -e
PLATFORM=$([[ "${@#--android}" = "$@" ]] && echo "ios" || echo "android")
TARGET=$([[ "${@#--device}" = "$@" ]] && echo "emulator" || echo "device")
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
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/
cd $ROOT/temp
$CDV prepare
$CDV plugins add $ROOT
$CDV build $PLATFORM --$TARGET

13
scripts/release.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -e
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
pushd $ROOT
VERSION=$(node -e "console.log(require('./package.json').version)")
./scripts/update-tough-cookie.sh
node ./scripts/update-plugin-xml.js $VERSION
git commit -a -m "release v$VERSION"
git tag "v$VERSION"
npm publish
popd

8
scripts/test-app.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
pushd $ROOT
./node_modules/.bin/mocha ./test/app-mocha-specs/test.js "$@"
popd

View File

@@ -1,12 +0,0 @@
#!/usr/bin/env bash
set -e
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
CDV=$ROOT/node_modules/.bin/cordova
rm -rf $ROOT/temp
$CDV create $ROOT/temp
cd $ROOT/temp
$CDV platforms add android
$CDV platforms add ios
$CDV plugins add $ROOT

View File

@@ -0,0 +1,31 @@
const args = process.argv.slice(2);
const fs = require('mz/fs');
const path = require('path');
const xml2js = require('xml2js');
const xmlPath = path.join(__dirname, '..', 'plugin.xml');
const parse = xml => new Promise((resolve, reject) => {
const parser = new xml2js.Parser();
parser.parseString(xml, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
const stringify = obj => {
const builder = new xml2js.Builder();
return builder.buildObject(obj);
};
fs.readFile(xmlPath, 'utf-8')
.then(xml => parse(xml))
.then(parsed => {
parsed.plugin.$.version = args[0];
return fs.writeFile(xmlPath, stringify(parsed));
});

8
scripts/update-tough-cookie.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
cd $ROOT
npm i
cp node_modules/umd-tough-cookie/lib/umd-tough-cookie.js www/umd-tough-cookie.js

16
scripts/upload-artifact.sh Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -e
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
TEMP=$ROOT/temp
rm -rf $TEMP/HttpDemo.app.zip
pushd $TEMP/platforms/ios/build/emulator
zip -r $TEMP/HttpDemo.app.zip ./HttpDemo.app
popd
curl -u $SAUCE_USERNAME:$SAUCE_ACCESS_KEY \
-X POST \
-H "Content-Type: application/octet-stream" \
https://saucelabs.com/rest/v1/storage/$SAUCE_USERNAME/HttpDemo.app.zip?overwrite=true \
--data-binary @$TEMP/HttpDemo.app.zip

View File

@@ -236,6 +236,11 @@ public class HttpRequest {
*/
public static final String METHOD_POST = "POST";
/**
* 'PATCH' request method
*/
public static final String METHOD_PATCH = "PATCH";
/**
* 'PUT' request method
*/
@@ -1176,6 +1181,70 @@ public class HttpRequest {
return post(encode ? encode(url) : url);
}
/**
* Start a 'PATCH' request to the given URL
*
* @param url
* @return request
* @throws HttpRequestException
*/
public static HttpRequest patch(final CharSequence url)
throws HttpRequestException {
return new HttpRequest(url, METHOD_PATCH);
}
/**
* Start a 'PATCH' request to the given URL
*
* @param url
* @return request
* @throws HttpRequestException
*/
public static HttpRequest patch(final URL url) throws HttpRequestException {
return new HttpRequest(url, METHOD_PATCH);
}
/**
* Start a 'PATCH' request to the given URL along with the query params
*
* @param baseUrl
* @param params
* the query parameters to include as part of the baseUrl
* @param encode
* true to encode the full URL
*
* @see #append(CharSequence, Map)
* @see #encode(CharSequence)
*
* @return request
*/
public static HttpRequest patch(final CharSequence baseUrl,
final Map<?, ?> params, final boolean encode) {
String url = append(baseUrl, params);
return patch(encode ? encode(url) : url);
}
/**
* Start a 'PATCH' request to the given URL along with the query params
*
* @param baseUrl
* @param encode
* true to encode the full URL
* @param params
* the name/value query parameter pairs to include as part of the
* baseUrl
*
* @see #append(CharSequence, Object...)
* @see #encode(CharSequence)
*
* @return request
*/
public static HttpRequest patch(final CharSequence baseUrl,
final boolean encode, final Object... params) {
String url = append(baseUrl, params);
return patch(encode ? encode(url) : url);
}
/**
* Start a 'PUT' request to the given URL
*

View File

@@ -25,6 +25,7 @@ abstract class CordovaHttp {
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
private static AtomicBoolean disableRedirect = new AtomicBoolean(false);
private String urlString;
private JSONObject params;
@@ -69,6 +70,10 @@ abstract class CordovaHttp {
validateDomainName.set(accept);
}
public static void disableRedirect(boolean disable) {
disableRedirect.set(disable);
}
protected String getUrlString() {
return this.urlString;
}
@@ -114,6 +119,13 @@ abstract class CordovaHttp {
return request;
}
protected HttpRequest setupRedirect(HttpRequest request) {
if (disableRedirect.get()) {
request.followRedirects(false);
}
return request;
}
protected void respondWithError(int status, String msg) {
try {
JSONObject response = new JSONObject();

View File

@@ -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, CallbackContext callbackContext, int timeout) {
public CordovaHttpDelete(String urlString, JSONObject data, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, data, headers, timeout, callbackContext);
}
@@ -27,6 +27,7 @@ class CordovaHttpDelete extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.delete(this.getUrlString(), this.getParamsMap(), false);
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -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, CallbackContext callbackContext, String filePath, int timeout) {
public CordovaHttpDownload(String urlString, JSONObject params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
this.filePath = filePath;
}
@@ -34,6 +34,7 @@ class CordovaHttpDownload extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParamsMap(), true);
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -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, CallbackContext callbackContext, int timeout) {
public CordovaHttpGet(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
}
@@ -27,6 +27,7 @@ class CordovaHttpGet extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParamsMap(), false);
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -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, CallbackContext callbackContext, int timeout) {
public CordovaHttpHead(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
}
@@ -26,6 +26,7 @@ class CordovaHttpHead extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParamsMap(), true);
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -0,0 +1,70 @@
/**
* A HTTP plugin for Cordova / Phonegap
*/
package com.synconset.cordovahttp;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import org.apache.cordova.CallbackContext;
import org.json.JSONException;
import org.json.JSONObject;
import javax.net.ssl.SSLHandshakeException;
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) {
super(urlString, params, serializerName, headers, timeout, callbackContext);
}
@Override
public void run() {
try {
HttpRequest request = HttpRequest.patch(this.getUrlString());
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());
request.uncompress(true);
if (new String("json").equals(this.getSerializerName())) {
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
request.send(this.getParamsObject().toString());
} else {
request.form(this.getParamsMap());
}
int code = request.code();
String body = request.body(CHARSET);
JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
response.put("data", body);
this.getCallbackContext().success(response);
} else {
response.put("error", body);
this.getCallbackContext().error(response);
}
} catch (JSONException e) {
this.respondWithError("There was an error generating the response");
} catch (HttpRequestException e) {
if (e.getCause() instanceof UnknownHostException) {
this.respondWithError(0, "The host could not be resolved");
} else if (e.getCause() instanceof SocketTimeoutException) {
this.respondWithError(1, "The request timed out");
} else if (e.getCause() instanceof SSLHandshakeException) {
this.respondWithError("SSL handshake failed");
} else {
this.respondWithError("There was an error with the request");
}
}
}
}

View File

@@ -40,7 +40,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
CordovaHttpPost post = new CordovaHttpPost(urlString, params, serializerName, headers, callbackContext, timeoutInMilliseconds);
CordovaHttpPost post = new CordovaHttpPost(urlString, params, serializerName, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(post);
} else if (action.equals("get")) {
@@ -48,7 +48,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
JSONObject params = args.getJSONObject(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, callbackContext, timeoutInMilliseconds);
CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(get);
} else if (action.equals("put")) {
@@ -57,15 +57,25 @@ public class CordovaHttpPlugin extends CordovaPlugin {
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, callbackContext, timeoutInMilliseconds);
CordovaHttpPut put = new CordovaHttpPut(urlString, params, serializerName, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(put);
} else if (action.equals("delete")) {
} else if (action.equals("patch")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
CordovaHttpPatch patch = new CordovaHttpPatch(urlString, params, serializerName, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(patch);
}
else if (action.equals("delete")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpDelete delete = new CordovaHttpDelete(urlString, params, headers, callbackContext, timeoutInMilliseconds);
CordovaHttpDelete delete = new CordovaHttpDelete(urlString, params, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(delete);
} else if (action.equals("head")) {
@@ -73,7 +83,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
JSONObject params = args.getJSONObject(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, callbackContext, timeoutInMilliseconds);
CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(head);
} else if (action.equals("enableSSLPinning")) {
@@ -89,11 +99,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
boolean accept = args.getBoolean(0);
CordovaHttp.acceptAllCerts(accept);
callbackContext.success();
} else if (action.equals("validateDomainName")) {
boolean accept = args.getBoolean(0);
CordovaHttp.validateDomainName(accept);
CordovaHttp.validateDomainName(!accept);
callbackContext.success();
} else if (action.equals("uploadFile")) {
String urlString = args.getString(0);
@@ -102,7 +108,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
String filePath = args.getString(3);
String name = args.getString(4);
int timeoutInMilliseconds = args.getInt(5) * 1000;
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, params, headers, callbackContext, filePath, name, timeoutInMilliseconds);
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, params, headers, filePath, name, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(upload);
} else if (action.equals("downloadFile")) {
@@ -111,9 +117,13 @@ public class CordovaHttpPlugin extends CordovaPlugin {
JSONObject headers = args.getJSONObject(2);
String filePath = args.getString(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
CordovaHttpDownload download = new CordovaHttpDownload(urlString, params, headers, callbackContext, filePath, timeoutInMilliseconds);
CordovaHttpDownload download = new CordovaHttpDownload(urlString, params, headers, filePath, timeoutInMilliseconds, callbackContext);
cordova.getThreadPool().execute(download);
} else if (action.equals("disableRedirect")) {
boolean disable = args.getBoolean(0);
CordovaHttp.disableRedirect(disable);
callbackContext.success();
} else {
return false;
}

View File

@@ -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, CallbackContext callbackContext, int timeout) {
public CordovaHttpPost(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, serializerName, headers, timeout, callbackContext);
}
@@ -26,6 +26,7 @@ class CordovaHttpPost extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.post(this.getUrlString());
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -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, CallbackContext callbackContext, int timeout) {
public CordovaHttpPut(String urlString, JSONObject data, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, data, serializerName, headers, timeout, callbackContext);
}
@@ -26,6 +26,7 @@ class CordovaHttpPut extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.put(this.getUrlString());
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -30,7 +30,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable {
private String filePath;
private String name;
public CordovaHttpUpload(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext, String filePath, String name, int timeout) {
public CordovaHttpUpload(String urlString, JSONObject params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
this.filePath = filePath;
this.name = name;
@@ -42,6 +42,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable {
HttpRequest request = HttpRequest.post(this.getUrlString());
request.readTimeout(this.getRequestTimeout());
this.setupRedirect(request);
this.setupSecurity(request);
request.acceptCharset(CHARSET);
request.headers(this.getHeadersMap());

View File

@@ -6,10 +6,11 @@
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command;
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command;
- (void)validateDomainName:(CDVInvokedUrlCommand*)command;
- (void)disableRedirect:(CDVInvokedUrlCommand*)command;
- (void)post:(CDVInvokedUrlCommand*)command;
- (void)get:(CDVInvokedUrlCommand*)command;
- (void)put:(CDVInvokedUrlCommand*)command;
- (void)patch:(CDVInvokedUrlCommand*)command;
- (void)delete:(CDVInvokedUrlCommand*)command;
- (void)uploadFile:(CDVInvokedUrlCommand*)command;
- (void)downloadFile:(CDVInvokedUrlCommand*)command;

View File

@@ -11,15 +11,18 @@
- (NSNumber*)getStatusCode:(NSError*) error;
- (NSMutableDictionary*)copyHeaderFields:(NSDictionary*)headerFields;
- (void)setTimeout:(NSTimeInterval)timeout forManager:(AFHTTPSessionManager*)manager;
- (void)setRedirect:(AFHTTPSessionManager*)manager;
@end
@implementation CordovaHttpPlugin {
AFSecurityPolicy *securityPolicy;
bool redirect;
}
- (void)pluginInitialize {
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
redirect = true;
}
- (void)setRequestSerializer:(NSString*)serializerName forManager:(AFHTTPSessionManager*)manager {
@@ -36,6 +39,16 @@
}];
}
- (void)setRedirect:(AFHTTPSessionManager*)manager {
[manager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
if (redirect) {
return request;
} else {
return nil;
}
}];
}
- (void)handleSuccess:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response andData:(id)data {
if (response != nil) {
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
@@ -95,6 +108,7 @@
- (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
bool enable = [[command.arguments objectAtIndex:0] boolValue];
if (enable) {
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
} else {
@@ -105,21 +119,22 @@
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command {
- (void)disableRedirect:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
bool allow = [[command.arguments objectAtIndex:0] boolValue];
bool disable = [[command.arguments objectAtIndex:0] boolValue];
securityPolicy.allowInvalidCertificates = allow;
redirect = !disable;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)validateDomainName:(CDVInvokedUrlCommand*)command {
- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
bool validate = [[command.arguments objectAtIndex:0] boolValue];
bool allow = [[command.arguments objectAtIndex:0] boolValue];
securityPolicy.validatesDomainName = validate;
securityPolicy.allowInvalidCertificates = allow;
securityPolicy.validatesDomainName = !allow;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@@ -138,6 +153,7 @@
[self setRequestSerializer: serializerName forManager: manager];
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
@@ -169,6 +185,7 @@
[self setRequestSerializer: @"default" forManager: manager];
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
@@ -201,6 +218,7 @@
[self setRequestSerializer: serializerName forManager: manager];
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
@@ -219,6 +237,38 @@
}];
}
- (void)patch:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
NSString *url = [command.arguments objectAtIndex:0];
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSString *serializerName = [command.arguments objectAtIndex:2];
NSDictionary *headers = [command.arguments objectAtIndex:3];
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
[self setRequestSerializer: serializerName forManager: manager];
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
[manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} failure:^(NSURLSessionTask *task, NSError *error) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)delete:(CDVInvokedUrlCommand*)command {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
@@ -231,6 +281,7 @@
[self setRequestSerializer: @"default" forManager: manager];
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
@@ -260,6 +311,7 @@
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
@@ -295,6 +347,7 @@
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
@@ -337,6 +390,7 @@
[self setRequestHeaders: headers forManager: manager];
[self setTimeout:timeoutInSeconds forManager:manager];
[self setRedirect: manager];
if ([filePath hasPrefix:@"file://"]) {
filePath = [filePath substringFromIndex:7];

View File

@@ -23,7 +23,7 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
return nil;
}
self.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"text/json", @"application/json", @"text/xml", @"application/xml", @"text/css", nil];
self.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"text/json", @"application/hal+json", @"application/json", @"text/xml", @"application/xml", @"text/css", nil];
return self;
}
@@ -55,4 +55,4 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
return responseString;
}
@end
@end

View File

@@ -0,0 +1,10 @@
const path = require('path');
if (process.env.SAUCE_USERNAME) {
exports.iosTestApp = 'sauce-storage:HttpDemo.app.zip';
exports.androidTestApp = 'sauce-storage:HttpDemo.apk';
} else {
// these paths are relative to working directory
exports.iosTestApp = path.resolve('temp/platforms/ios/build/emulator/HttpDemo.app');
exports.androidTestApp = path.resolve('temp/platforms/android/build/outputs/apk/android-debug.apk');
}

View File

@@ -0,0 +1,65 @@
const local = {
iosDevice: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'iOS',
platformVersion: '10.3',
deviceName: 'iPhone 6',
autoWebview: true,
app: undefined // will be set later
},
iosEmulator: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'iOS',
platformVersion: '11.0',
deviceName: 'iPhone Simulator',
autoWebview: true,
app: undefined // will be set later
},
androidEmulator: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'Android',
platformVersion: '5.1',
deviceName: 'Android Emulator',
autoWebview: true,
app: undefined // will be set later
}
};
const sauce = {
iosDevice: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'iOS',
platformVersion: '10.3',
deviceName: 'iPhone 6',
autoWebview: true,
app: undefined // will be set later
},
iosEmulator: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'iOS',
platformVersion: '10.3',
deviceName: 'iPhone Simulator',
autoWebview: true,
app: undefined // will be set later
},
androidEmulator: {
browserName: '',
'appium-version': '1.7.1',
platformName: 'Android',
platformVersion: '5.1',
deviceName: 'Android Emulator',
autoWebview: true,
app: undefined // will be set later
}
};
if (process.env.SAUCE_USERNAME) {
module.exports = sauce;
} else {
module.exports = local;
}

View File

@@ -0,0 +1,13 @@
exports.configure = driver => {
driver.on('status', info => {
console.log(info.cyan);
});
driver.on('command', (meth, path, data) => {
console.log(' > ' + meth.yellow, path.grey, data || '');
});
driver.on('http', (meth, path, data) => {
console.log(' > ' + meth.magenta, path, (data || '').grey);
});
};

View File

@@ -0,0 +1,16 @@
const local = {
host: 'localhost',
port: 4723
};
const sauce = {
host: 'ondemand.saucelabs.com',
port: 80,
auth: process.env.SAUCE_USERNAME + ":" + process.env.SAUCE_ACCESS_KEY
};
if (process.env.SAUCE_USERNAME) {
module.exports = sauce;
} else {
module.exports = local;
}

View File

@@ -0,0 +1,12 @@
const wd = require("wd");
require('colors');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const should = chai.should();
chaiAsPromised.transferPromiseness = wd.transferPromiseness;
exports.should = should;

View File

@@ -0,0 +1,91 @@
require('./helpers/setup');
const wd = require('wd');
const apps = require('./helpers/apps');
const caps = Object.assign({}, require('./helpers/caps'));
const serverConfig = require('./helpers/server');
const testDefinitions = require('../app-test-definitions');
const pkgjson = require('../../package.json');
describe('Advanced HTTP', function() {
let driver = null;
let allPassed = true;
this.timeout(900000);
const getCaps = appName => {
const isDevice = process.argv.includes('--device');
const isAndroid = process.argv.includes('--android');
const desiredCaps = caps[(isAndroid ? 'android' : 'ios') + (isDevice ? 'Device' : 'Emulator')];
const desiredApp = apps[(isAndroid ? 'android' : 'ios') + appName];
desiredCaps.name = pkgjson.name;
desiredCaps.app = desiredApp;
return desiredCaps;
};
const validateTestIndex = number => driver
.elementById('descriptionLbl')
.text()
.then(text => parseInt(text.match(/(\d+):/)[1], 10))
.should.eventually.become(number, 'Test index is not matching!');
const validateTestTitle = testTitle => driver
.elementById('descriptionLbl')
.text()
.then(text => text.match(/\d+:\ (.*)/)[1])
.should.eventually.become(testTitle, 'Test description is not matching!');
const waitToBeFinished = timeout => new Promise((resolve, reject) => {
const timeoutTimestamp = Date.now() + timeout;
const checkIfFinished = () => driver
.elementById('statusInput')
.getValue()
.then(value => {
if (value === 'finished') {
resolve();
} else if (Date.now() > timeoutTimestamp) {
reject('Test function timed out!');
} else {
setTimeout(checkIfFinished, 500);
}
});
checkIfFinished();
});
const validateResult = testDefinition => driver
.safeExecute('app.lastResult')
.then(result => testDefinition.validationFunc(driver, result));
const clickNext = () => driver
.elementById('nextBtn')
.click()
.sleep(1000);
before(() => {
driver = wd.promiseChainRemote(serverConfig);
require('./helpers/logging').configure(driver);
return driver.init(getCaps('TestApp'));
});
after(() => driver
.quit()
.finally(function () {
if (process.env.SAUCE_USERNAME) {
return driver.sauceJobStatus(allPassed);
}
}));
testDefinitions.tests.forEach((definition, index) => {
it(definition.description, function() {
return clickNext()
.then(() => validateTestIndex(index))
.then(() => validateTestTitle(this.test.title))
.then(() => waitToBeFinished(definition.timeout || 10000))
.then(() => validateResult(definition))
});
});
});

View File

@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.ilkimen.http.demo" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>HttpDemo</name>
<description>
A sample Apache Cordova application that demonstrates advanced HTTP plugin.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Sefa Ilkimen
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<engine name="android" spec="6.2.3" />
<engine name="ios" spec="4.4.0" />
</widget>

View File

@@ -0,0 +1,25 @@
{
"name": "com.ilkimen.http.demo",
"displayName": "HttpDemo",
"version": "1.0.0",
"description": "A sample Apache Cordova application that demonstrates advanced HTTP plugin.",
"main": "index.js",
"scripts": {
"build": "scripts/build.sh",
"test": "npm run build && scripts/test.sh"
},
"author": "Sefa Ilkimen",
"license": "Apache-2.0",
"dependencies": {
"cordova": "7.0.1",
"cordova-android": "6.2.3",
"cordova-ios": "4.4.0"
},
"cordova": {
"platforms": [
"android",
"ios"
]
},
"devDependencies": {}
}

View File

@@ -0,0 +1,28 @@
* {
-webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}
body {
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
height:100%;
margin:0px;
padding:20px 0;
width:100%;
font-family: sans-serif;
}
button, input, textarea {
display: block;
width: 100%;
}
input {
text-align: center;
}
h1 {
font-size: 12pt;
text-align: center;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
</head>
<body>
<h1 id="descriptionLbl">Advanced HTTP test suite</h1>
<input value="idle" id="statusInput" readonly></input>
<textarea rows="16" cols="50" id="expectedTextarea">Click next to run first test.</textarea>
<textarea rows="16" cols="50" id="resultTextarea"></textarea>
<button id="nextBtn">Run next test</button>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/app-test-definitions.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>

View File

@@ -0,0 +1,92 @@
const app = {
testIndex: -1,
lastResult: null,
initialize: function() {
document.getElementById('nextBtn').addEventListener('click', app.onNextBtnClick);
},
printResult: function(prefix, content) {
const text = prefix + ': ' + JSON.stringify(content);
document.getElementById('resultTextarea').value += text;
},
reject: function(content) {
document.getElementById('statusInput').value = 'finished';
app.printResult('result - rejected', content);
app.lastResult = {
type: 'rejected',
data: content
};
},
resolve: function(content) {
document.getElementById('statusInput').value = 'finished';
app.printResult('result - resolved', content);
app.lastResult = {
type: 'resolved',
data: content
};
},
getResult: function(cb) {
cb(app.lastResult);
},
runTest: function(index) {
const testDefinition = tests[index];
const titleText = app.testIndex + ': ' + testDefinition.description;
const expectedText = 'expected - ' + testDefinition.expected;
document.getElementById('statusInput').value = 'running';
document.getElementById('expectedTextarea').value = expectedText;
document.getElementById('resultTextarea').value = '';
document.getElementById('descriptionLbl').innerText = titleText;
testDefinition.func(app.resolve, app.reject);
},
onBeforeTest: function(testIndex, cb) {
app.lastResult = null;
if (hooks && hooks.onBeforeEachTest) {
return hooks.onBeforeEachTest(function() {
const testDefinition = tests[testIndex];
if (testDefinition.before) {
testDefinition.before(cb);
} else {
cb();
}
});
} else {
cb();
}
},
onFinishedAllTests: function() {
const titleText = 'No more tests';
const expectedText = 'You have run all available tests.';
document.getElementById('expectedTextarea').value = expectedText;
document.getElementById('resultTextarea').value = '';
document.getElementById('descriptionLbl').innerText = titleText;
},
onNextBtnClick: function() {
app.testIndex += 1;
if (app.testIndex < tests.length) {
app.onBeforeTest(app.testIndex, function() {
app.runTest(app.testIndex);
});
} else {
app.onFinishedAllTests();
}
}
};
app.initialize();

View File

@@ -0,0 +1,111 @@
const hooks = {
onBeforeEachTest: function(done) {
cordova.plugin.http.acceptAllCerts(false, done, done);
}
};
const helpers = {
acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); }
};
const tests = [
{
description: 'should reject self signed cert (GET)',
expected: 'rejected: {"status":-1,"error":"cancelled"}',
func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.should.be.eql({ status: -1, error:'cancelled' });
}
},{
description: 'should reject self signed cert (PUT)',
expected: 'rejected: {"status":-1,"error":"cancelled"}',
func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.should.be.eql({ status: -1, error:'cancelled' });
}
},{
description: 'should reject self signed cert (POST)',
expected: 'rejected: {"status":-1,"error":"cancelled"}',
func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.should.be.eql({ status: -1, error:'cancelled' });
}
},{
description: 'should reject self signed cert (PATCH)',
expected: 'rejected: {"status":-1,"error":"cancelled"}',
func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.should.be.eql({ status: -1, error:'cancelled' });
}
},{
description: 'should reject self signed cert (DELETE)',
expected: 'rejected: {"status":-1,"error":"cancelled"}',
func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('rejected');
result.data.should.be.eql({ status: -1, error:'cancelled' });
}
},{
description: 'should accept bad cert (GET)',
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) {
result.type.should.be.equal('resolved');
result.data.should.include({ status: 200 });
}
},{
description: 'should accept bad cert (PUT)',
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) {
result.type.should.be.equal('rejected');
result.data.should.include({ status: 405 });
}
},{
description: 'should accept bad cert (POST)',
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) {
result.type.should.be.equal('rejected');
result.data.should.include({ status: 405 });
}
},{
description: 'should accept bad cert (PATCH)',
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) {
result.type.should.be.equal('rejected');
result.data.should.include({ status: 405 });
}
},{
description: 'should accept bad cert (DELETE)',
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) {
result.type.should.be.equal('rejected');
result.data.should.include({ status: 405 });
}
},{
description: 'should fetch data from http://google.com/ (GET)',
expected: 'resolved: {\"status\":200, ...',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.should.include({ status: 200 });
}
}
];
if (typeof module !== 'undefined' && module.exports) {
module.exports = { tests: tests, hooks: hooks };
}

94
test/js-mocha-specs.js Normal file
View File

@@ -0,0 +1,94 @@
const chai = require('chai');
const mock = require('mock-require');
const path = require('path');
const should = chai.should();
const PLUGIN_ID = path.resolve(__dirname, '..', 'www', 'advanced-http');
describe('Advanced HTTP www interface', function() {
let http = {};
const noop = () => { /* intentionally doing nothing */ };
const loadHttp = () => {
http = mock.reRequire('../www/advanced-http');
};
this.timeout(900000);
beforeEach(() => {
mock('cordova/exec', noop);
mock(`${PLUGIN_ID}.angular-integration`, { registerService: noop });
mock(`${PLUGIN_ID}.cookie-handler`, {});
loadHttp();
});
it('sets global headers correctly with two args (old interface)', () => {
http.setHeader('myKey', 'myValue');
http.headers['*'].myKey.should.equal('myValue');
});
it('sets global headers correctly with three args (new interface)', () => {
http.setHeader('*', 'myKey', 'myValue');
http.headers['*'].myKey.should.equal('myValue');
});
it('sets host headers correctly', () => {
http.setHeader('www.google.de', 'myKey', 'myValue');
http.headers['www.google.de'].myKey.should.equal('myValue');
});
it('resolves global headers correctly', () => {
mock(`${PLUGIN_ID}.cookie-handler`, {
getCookieString: () => 'fakeCookieString'
});
mock('cordova/exec', (onSuccess, onFail, namespace, method, params) => {
const headers = params[2];
headers.should.eql({
Cookie: 'fakeCookieString',
myKey: 'myValue'
});
});
loadHttp();
http.setHeader('*', 'myKey', 'myValue');
http.get('url', {}, {}, noop, noop);
});
it('resolves host headers correctly', () => {
mock(`${PLUGIN_ID}.cookie-handler`, {
getCookieString: () => 'fakeCookieString'
});
mock('cordova/exec', (onSuccess, onFail, namespace, method, params) => {
const headers = params[2];
headers.should.eql({
Cookie: 'fakeCookieString',
myKey: 'myValue'
});
});
loadHttp();
http.setHeader('www.google.de', 'myKey', 'myValue');
http.get('https://www.google.de/?gws_rd=ssl', {}, {}, noop, noop);
});
it('resolves request headers correctly', () => {
mock(`${PLUGIN_ID}.cookie-handler`, {
getCookieString: () => 'fakeCookieString'
});
mock('cordova/exec', (onSuccess, onFail, namespace, method, params) => {
const headers = params[2];
headers.should.eql({
Cookie: 'fakeCookieString',
myKey: 'myValue'
});
});
loadHttp();
http.get('https://www.google.de/?gws_rd=ssl', {}, { myKey: 'myValue' }, noop, noop);
});
});

View File

@@ -114,6 +114,24 @@ function getCookieHeader(url) {
return { Cookie: cookieHandler.getCookieString(url) };
}
function getMatchingHostHeaders(url, headersList) {
var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
var domain = matches && matches[1];
return headersList[domain] || null;
}
function getMergedHeaders(url, requestHeaders, predefinedHeaders) {
var globalHeaders = predefinedHeaders['*'] || {};
var hostHeaders = getMatchingHostHeaders(url, predefinedHeaders) || {};
var mergedHeaders = mergeHeaders(globalHeaders, hostHeaders);
mergedHeaders = mergeHeaders(mergedHeaders, requestHeaders);
mergedHeaders = mergeHeaders(mergedHeaders, getCookieHeader(url));
return mergedHeaders;
}
function handleMissingCallbacks(successFn, failFn) {
if (Object.prototype.toString.call(successFn) !== '[object Function]') {
throw new Error('advanced-http: missing mandatory "onSuccess" callback function');
@@ -133,10 +151,22 @@ var http = {
return {'Authorization': 'Basic ' + b64EncodeUnicode(username + ':' + password)};
},
useBasicAuth: function (username, password) {
this.headers.Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password);
this.headers['*'].Authorization = 'Basic ' + b64EncodeUnicode(username + ':' + password);
},
setHeader: function (header, value) {
this.headers[header] = value;
setHeader: function () {
// this one is for being backward compatible
var host = '*';
var header = arguments[0];
var value = arguments[1];
if (arguments.length === 3) {
host = arguments[0];
header = arguments[1];
value = arguments[2];
}
this.headers[host] = this.headers[host] || {};
this.headers[host][header] = value;
},
setDataSerializer: function (serializer) {
this.dataSerializer = checkSerializer(serializer);
@@ -156,16 +186,17 @@ var http = {
acceptAllCerts: function (allow, success, failure) {
return exec(success, failure, 'CordovaHttpPlugin', 'acceptAllCerts', [allow]);
},
disableRedirect: function(disable, success, failure) {
return exec(success, failure, 'CordovaHttpPlugin', 'disableRedirect', [disable]);
},
validateDomainName: function (validate, success, failure) {
return exec(success, failure, 'CordovaHttpPlugin', 'validateDomainName', [validate]);
failure('advanced-http: "validateDomainName" is no more supported, please see change log for further info');
},
post: function (url, data, headers, success, failure) {
handleMissingCallbacks(success, failure);
data = data || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -176,9 +207,7 @@ var http = {
handleMissingCallbacks(success, failure);
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -189,22 +218,31 @@ var http = {
handleMissingCallbacks(success, failure);
data = data || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]);
},
patch: function (url, data, headers, success, failure) {
handleMissingCallbacks(success, failure);
data = data || {};
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]);
},
delete: function (url, params, headers, success, failure) {
handleMissingCallbacks(success, failure);
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -215,9 +253,7 @@ var http = {
handleMissingCallbacks(success, failure);
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -228,9 +264,7 @@ var http = {
handleMissingCallbacks(success, failure);
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, success);
var onFail = injectCookieHandler(url, failure);
@@ -241,9 +275,7 @@ var http = {
handleMissingCallbacks(success, failure);
params = params || {};
headers = headers || {};
headers = mergeHeaders(this.headers, headers);
headers = mergeHeaders(getCookieHeader(url), headers);
headers = getMergedHeaders(url, headers, this.headers);
var onSuccess = injectCookieHandler(url, injectFileEntryHandler(success));
var onFail = injectCookieHandler(url, failure);

View File

@@ -38,8 +38,8 @@ function registerService(http) {
useBasicAuth: function (username, password) {
return http.useBasicAuth(username, password);
},
setHeader: function (header, value) {
return http.setHeader(header, value);
setHeader: function (host, header, value) {
return http.setHeader(host, header, value);
},
setDataSerializer: function (serializer) {
return http.setDataSerializer(serializer);
@@ -59,6 +59,9 @@ function registerService(http) {
acceptAllCerts: function (allow) {
return makePromise(http.acceptAllCerts, [allow]);
},
disableRedirect: function(disable) {
return makePromise(http.disableRedirect, [disable]);
},
validateDomainName: function (validate) {
return makePromise(http.validateDomainName, [validate]);
},

View File

@@ -41,7 +41,7 @@ function setCookieFromString(url, cookieStr) {
var cookies = splitCookieString(cookieStr);
for (var i = 0; i < cookies.length; ++i) {
cookieJar.setCookieSync(cookies[i], url);
cookieJar.setCookieSync(cookies[i], url, { ignoreError: true });
}
}

4851
www/umd-tough-cookie.js Normal file

File diff suppressed because one or more lines are too long