mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-02-11 00:00:06 +08:00
Compare commits
144 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6a8acd25b | ||
|
|
0ccf64e289 | ||
|
|
986ddeefd4 | ||
|
|
67c2c733f5 | ||
|
|
d077d02062 | ||
|
|
060794354d | ||
|
|
4ebd259c7e | ||
|
|
bbd4cf01ab | ||
|
|
7eb3395aa4 | ||
|
|
05abdfcd91 | ||
|
|
bda4eedfb9 | ||
|
|
9d6005af29 | ||
|
|
94126b9021 | ||
|
|
007d5c609f | ||
|
|
badf6dcdc2 | ||
|
|
c081060a9e | ||
|
|
2ce130133c | ||
|
|
cfcc572ca0 | ||
|
|
d7688b485d | ||
|
|
81ba667e37 | ||
|
|
5ad1967b46 | ||
|
|
90ed474b29 | ||
|
|
1947906c4c | ||
|
|
b25b07a2a7 | ||
|
|
a01625ecfb | ||
|
|
b03ae7d6d1 | ||
|
|
fdaea418be | ||
|
|
d84df0fb00 | ||
|
|
baedd60329 | ||
|
|
80b22d4202 | ||
|
|
89ac260ef6 | ||
|
|
6a266218fb | ||
|
|
6050830423 | ||
|
|
95c9eb85d0 | ||
|
|
7e3ff25195 | ||
|
|
8a6b9e583e | ||
|
|
b32dfc7143 | ||
|
|
a72bf758c6 | ||
|
|
45f9cbfaa4 | ||
|
|
f8f52e1e97 | ||
|
|
9b995724f4 | ||
|
|
25a0a9a7ae | ||
|
|
e44def06a5 | ||
|
|
3d288951bf | ||
|
|
f39063cec9 | ||
|
|
1bcb8016ff | ||
|
|
6339b9b83d | ||
|
|
389534d661 | ||
|
|
c10722eca0 | ||
|
|
6a60058fc6 | ||
|
|
fcedfae1ac | ||
|
|
aca165b900 | ||
|
|
09c2b383ff | ||
|
|
6918a2ed15 | ||
|
|
5b827d500d | ||
|
|
1c27b62148 | ||
|
|
f33a911e7e | ||
|
|
097faad07a | ||
|
|
62c400c6db | ||
|
|
f823d24438 | ||
|
|
64a7148444 | ||
|
|
f6736d9150 | ||
|
|
bc90ae85fb | ||
|
|
389e860125 | ||
|
|
2367d264c1 | ||
|
|
269d5d4c8a | ||
|
|
b6ee4de379 | ||
|
|
5f327dc82a | ||
|
|
1639efe8d0 | ||
|
|
9bb0c58e35 | ||
|
|
57562a0dcf | ||
|
|
ad4079625e | ||
|
|
98d3d38e07 | ||
|
|
c748406090 | ||
|
|
dc6cf4d45b | ||
|
|
7661e02598 | ||
|
|
20ec4bee31 | ||
|
|
4eed89e530 | ||
|
|
f43b2f9f5c | ||
|
|
7b4d37acd9 | ||
|
|
ca5306cb47 | ||
|
|
c0c1af5ee6 | ||
|
|
42e629e34d | ||
|
|
3bec8dde5f | ||
|
|
8a3bc17810 | ||
|
|
1eb83478dc | ||
|
|
1f0df54111 | ||
|
|
ef09e466ec | ||
|
|
88de0550f4 | ||
|
|
7f680b07ec | ||
|
|
a259e7cf8d | ||
|
|
1e3c9e6645 | ||
|
|
e0cf458179 | ||
|
|
4f4e7ffa33 | ||
|
|
65e4a4d4dc | ||
|
|
fed2b03cc6 | ||
|
|
29e0b385de | ||
|
|
bfc6ba2008 | ||
|
|
060aa088f5 | ||
|
|
e775057389 | ||
|
|
81874c6bc8 | ||
|
|
658bbd8b28 | ||
|
|
a1e4be37d4 | ||
|
|
5be52d78d1 | ||
|
|
0a23e29403 | ||
|
|
6009ae82f7 | ||
|
|
0bfb81c57f | ||
|
|
f8f4bdc9df | ||
|
|
d84e3995d2 | ||
|
|
f5597dd176 | ||
|
|
b25b7db4be | ||
|
|
92be8f8e96 | ||
|
|
9ffdc5d2eb | ||
|
|
23a98ae491 | ||
|
|
ab9dad8f46 | ||
|
|
69344b5357 | ||
|
|
c88d1b6cc7 | ||
|
|
b6f369b868 | ||
|
|
39fa17e4ed | ||
|
|
5a19c6ad06 | ||
|
|
192059d34e | ||
|
|
7193b636f1 | ||
|
|
662b4352f5 | ||
|
|
e18f9eee51 | ||
|
|
c7eb60e670 | ||
|
|
6a930de82f | ||
|
|
588e4a0e57 | ||
|
|
78db1dc516 | ||
|
|
aded59e3d1 | ||
|
|
9ef582b37f | ||
|
|
33fea67603 | ||
|
|
faffe0e078 | ||
|
|
99c7f5d331 | ||
|
|
b74ad73742 | ||
|
|
7fd7ab223c | ||
|
|
937010bd4e | ||
|
|
7ab4b634ca | ||
|
|
32187a12fe | ||
|
|
a4f121728c | ||
|
|
21d991b04e | ||
|
|
31b1eee355 | ||
|
|
3d1b195831 | ||
|
|
014753e127 | ||
|
|
5ee26bc2cc |
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
github: silkimen
|
||||
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
@@ -36,6 +36,9 @@ jobs:
|
||||
|
||||
build-android:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
|
||||
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Install Node.js ${{ env.nodejs }}
|
||||
@@ -50,7 +53,13 @@ jobs:
|
||||
java-version: 1.8
|
||||
- name: Update test cert for httpbin.org
|
||||
run: npm run updatecert
|
||||
- name: Add workaround for mipsel reference
|
||||
run: sudo mkdir -p $ANDROID_HOME/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64
|
||||
- name: Add workaround for missing DX files in build-tools 31 (https://stackoverflow.com/a/68430992)
|
||||
run: ln -s $ANDROID_HOME/build-tools/31.0.0/d8 $ANDROID_HOME/build-tools/31.0.0/dx && ln -s $ANDROID_HOME/build-tools/31.0.0/lib/d8.jar $ANDROID_HOME/build-tools/31.0.0/lib/dx.jar
|
||||
- name: Build test app
|
||||
run: scripts/build-test-app.sh --android --emulator
|
||||
run: scripts/build-test-app.sh --android --device
|
||||
- name: Upload artifact to BrowserStack
|
||||
if: env.BROWSERSTACK_USERNAME != ''
|
||||
run: scripts/upload-browserstack.sh --android
|
||||
- name: Run e2e tests
|
||||
if: env.BROWSERSTACK_USERNAME != ''
|
||||
run: scripts/test-app.sh --android --device
|
||||
|
||||
62
.github/workflows/codeql-analysis.yml
vendored
Normal file
62
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [master]
|
||||
schedule:
|
||||
- cron: '0 20 * * 3'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
# Override automatic language detection by changing the below list
|
||||
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
|
||||
language: ['javascript']
|
||||
# Learn more...
|
||||
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v1
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v1
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@ npm-debug.log
|
||||
/temp
|
||||
/android-sdk-macosx.zip
|
||||
/android-sdk-macosx/**
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
19
.travis.yml
19
.travis.yml
@@ -1,7 +1,3 @@
|
||||
notifications:
|
||||
slack:
|
||||
secure: twDT06GAiu0jsKizow7TcghZj70KbuuTrlo02QGmbSxBk2rJfsXSrHAsA3+s/9Q4mudENk6na7fs1aPCxz+u2etUGp+2PaJKVKR5n3jrNNt3SnYeWsBgVo7o7H1aLXatX3a+TdPXh1F5gQ4Ycr93nTYbW/077jsOholwbOHDZE3VcU9dzNPwFaEvhrDbr/ei3tef0ZiM1qxIad74TgwWMKClwai3I7HCVkZOPsyV+ve6cdIJ8Dt47JzFUHSW3SZuoe5Kywxvp0VvMo/QAJw95y3edNafx4EXHwbaN71rpGWSJXIKSZzcSQalZJ9DxGYspIBkWvGsNuQRzG9CzIoNQK10iERlIVC5vKDfKX22gayOQPSDkswJzIduylBUC8zdTPCndXyNEM/Lrj6hg+ksFWN58vYNPgfUeiga7X+LV5HytftsMFW+xx2kbnGeU8doGeX8Q8G7h9OIkHCTTG7R0ldYMIqTm8YJGPkRIv4OReC5ZOhiZD+wSg4KQ0wmMeRi+hyn+I5UPnKEOHAIN8FmLNCZFbgr1wuPFp9xnJIOcumQnQVZ2t6vk6IjIbwhYPWCnf7Sr4BvJxE8eyiLrEaXK0FiPb3My9wK9tLFjj1zdD7e4+SLq+WFMeCxp2eXOGF0Bu+2VK2tGjgWhaudaIpjbRQAAQ5nPa43h16NruEvNWI=
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
||||
@@ -15,10 +11,11 @@ matrix:
|
||||
language: objective-c
|
||||
sudo: false
|
||||
os: osx
|
||||
osx_image: xcode10.1
|
||||
osx_image: xcode12.5
|
||||
|
||||
before_install:
|
||||
- export LANG=en_US.UTF-8
|
||||
- export LANG=en_US.UTF-8 &&
|
||||
nvm use 14
|
||||
|
||||
install:
|
||||
- npm install
|
||||
@@ -27,7 +24,7 @@ matrix:
|
||||
- npm run testjs &&
|
||||
npm run updatecert &&
|
||||
scripts/build-test-app.sh --ios --emulator &&
|
||||
scripts/upload-artifact.sh --ios &&
|
||||
scripts/upload-saucelabs.sh --ios &&
|
||||
scripts/test-app.sh --ios --emulator;
|
||||
|
||||
- name: "Android Build & Test"
|
||||
@@ -36,17 +33,19 @@ matrix:
|
||||
|
||||
android:
|
||||
components:
|
||||
- tools
|
||||
- platform-tools
|
||||
- build-tools-28.0.3
|
||||
- android-27
|
||||
- android-28
|
||||
- extra-android-support
|
||||
- extra-android-m2repository
|
||||
- extra-google-m2repository
|
||||
|
||||
before_install:
|
||||
- export LANG=en_US.UTF-8 &&
|
||||
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - &&
|
||||
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - &&
|
||||
sudo apt-get install -y nodejs
|
||||
- yes | sdkmanager --update
|
||||
|
||||
install:
|
||||
- npm install
|
||||
@@ -55,5 +54,5 @@ matrix:
|
||||
- npm run testjs &&
|
||||
npm run updatecert &&
|
||||
scripts/build-test-app.sh --android --emulator &&
|
||||
scripts/upload-artifact.sh --android &&
|
||||
scripts/upload-saucelabs.sh --android &&
|
||||
scripts/test-app.sh --android --emulator;
|
||||
|
||||
53
CHANGELOG.md
53
CHANGELOG.md
@@ -1,5 +1,58 @@
|
||||
# Changelog
|
||||
|
||||
## 3.2.0
|
||||
|
||||
- Feature #420: implement blacklist feature to disable SSL/TLS versions on Android (thanks to @MobisysGmbH)
|
||||
|
||||
## 3.1.1
|
||||
|
||||
- Fixed #372: malformed empty multipart request on Android
|
||||
- Fixed #399: memory leakage leads to app crashes on iOS (thanks avargaskun)
|
||||
|
||||
## 3.1.0
|
||||
|
||||
- Feature #272: add support for aborting requests (thanks russaa)
|
||||
|
||||
## 3.0.1
|
||||
|
||||
- Fixed #359: memory leakage leads to app crashes on Android
|
||||
- Fixed #355: responseType "json" not working with valid JSON response on browser (thanks millerg6711)
|
||||
|
||||
## 3.0.0
|
||||
|
||||
- Feature #158: support removing headers which were previously set via "setHeader"
|
||||
|
||||
- Fixed #345: empty file names are not handled correctly (thanks ikosta)
|
||||
|
||||
- :warning: **Breaking Change**: Dropped support for Android < 5.1
|
||||
- :warning: **Breaking Change**: Removed "disableRedirect", use "setFollowRedirect" instead
|
||||
- :warning: **Breaking Change**: Removed "setSSLCertMode", use "setServerTrustMode" instead
|
||||
|
||||
## 2.5.1
|
||||
|
||||
- Fixed #334: empty JSON response triggers error even though request is successful (thanks antikalk)
|
||||
- Fixed #248: clearCookies() does not work on iOS
|
||||
|
||||
## 2.5.0
|
||||
|
||||
- Feature #56: add support for X.509 client certificate based authentication
|
||||
|
||||
## 2.4.1
|
||||
|
||||
- Fixed #296: multipart requests are not serialized on browser platform
|
||||
- Fixed #301: data is not decoded correctly when responseType is "json" (thanks antikalk)
|
||||
- Fixed #300: FormData object containing null or undefined value is not serialized correctly
|
||||
|
||||
## 2.4.0
|
||||
|
||||
- Feature #291: add support for sending 'raw' requests (thanks to jachstet-sea and chuchuva)
|
||||
- Feature #155: add OPTIONS method
|
||||
- Feature #283: improve error message on timeout on browser platform
|
||||
|
||||
## 2.3.1
|
||||
|
||||
- Fixed #275: getAllCookies() is broken because of a typo (thanks ath0mas)
|
||||
|
||||
## 2.3.0
|
||||
|
||||
- Feature #101: Support "multipart/form-data" requests (thanks SDA SE Open Industry Solutions)
|
||||
|
||||
137
README.md
137
README.md
@@ -1,10 +1,10 @@
|
||||
Cordova Advanced HTTP
|
||||
=====================
|
||||
[](https://badge.fury.io/js/cordova-plugin-advanced-http)
|
||||
[](https://www.npmjs.com/package/cordova-plugin-advanced-http?activeTab=versions)
|
||||
[](https://opensource.org/licenses/mit-license.php)
|
||||
[](https://www.npmjs.com/package/cordova-plugin-advanced-http)
|
||||
[](https://opensource.org/licenses/mit-license.php)
|
||||
|
||||
[](https://travis-ci.org/silkimen/cordova-plugin-advanced-http)
|
||||
[](https://travis-ci.com/silkimen/cordova-plugin-advanced-http)
|
||||
[](https://github.com/silkimen/cordova-plugin-advanced-http/actions)
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ This is a fork of [Wymsee's Cordova-HTTP plugin](https://github.com/wymsee/cordo
|
||||
|
||||
- SSL / TLS Pinning
|
||||
- CORS restrictions do not apply
|
||||
- X.509 client certificate based authentication
|
||||
- Handling of HTTP code 401 - read more at [Issue CB-2415](https://issues.apache.org/jira/browse/CB-2415)
|
||||
|
||||
## Updates
|
||||
@@ -33,6 +34,15 @@ phonegap plugin add cordova-plugin-advanced-http
|
||||
cordova plugin add cordova-plugin-advanced-http
|
||||
```
|
||||
|
||||
### Plugin Preferences
|
||||
|
||||
`AndroidBlacklistSecureSocketProtocols`: define a blacklist of secure socket protocols for Android. This preference allows you to disable protocols which are considered unsafe. You need to provide a comma-separated list of protocols ([check Android SSLSocket#protocols docu for protocol names](https://developer.android.com/reference/javax/net/ssl/SSLSocket#protocols)).
|
||||
|
||||
e.g. blacklist `SSLv3` and `TLSv1`:
|
||||
```xml
|
||||
<preference name="AndroidBlacklistSecureSocketProtocols" value="SSLv3,TLSv1" />
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Plain Cordova
|
||||
@@ -60,7 +70,7 @@ cordova.plugin.http.useBasicAuth('user', 'password');
|
||||
```
|
||||
|
||||
### setHeader<a name="setHeader"></a>
|
||||
Set a header for all future requests to a specified host. Takes a hostname, a header and a value (must be a string 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 or null).
|
||||
|
||||
```js
|
||||
cordova.plugin.http.setHeader('Hostname', 'Header', 'Value');
|
||||
@@ -91,10 +101,21 @@ cordova.plugin.http.setDataSerializer('urlencoded');
|
||||
```
|
||||
|
||||
You can choose one of these:
|
||||
* `urlencoded`: send data as url encoded content in body (content type "application/x-www-form-urlencoded")
|
||||
* `json`: send data as JSON encoded content in body (content type "application/json")
|
||||
* `utf8`: send data as plain UTF8 encoded string in body (content type "plain/text")
|
||||
* `multipart`: send FormData objects as multipart content in body (content type "multipart/form-data")
|
||||
* `urlencoded`: send data as url encoded content in body
|
||||
* default content type "application/x-www-form-urlencoded"
|
||||
* data must be an dictionary style `Object`
|
||||
* `json`: send data as JSON encoded content in body
|
||||
* default content type "application/json"
|
||||
* data must be an `Array` or an dictionary style `Object`
|
||||
* `utf8`: send data as plain UTF8 encoded string in body
|
||||
* default content type "plain/text"
|
||||
* data must be a `String`
|
||||
* `multipart`: send FormData objects as multipart content in body
|
||||
* default content type "multipart/form-data"
|
||||
* data must be an `FormData` instance
|
||||
* `raw`: send data as is, without any processing
|
||||
* default content type "application/octet-stream"
|
||||
* data must be an `Uint8Array` or an `ArrayBuffer`
|
||||
|
||||
This defaults to `urlencoded`. You can also override the default content type headers by specifying your own headers (see [setHeader](#setHeader)).
|
||||
|
||||
@@ -103,7 +124,7 @@ This defaults to `urlencoded`. You can also override the default content type he
|
||||
:warning: `multipart` depends on several Web API standards which need to be supported in your web view. Check out https://github.com/silkimen/cordova-plugin-advanced-http/wiki/Web-APIs-required-for-Multipart-requests for more info.
|
||||
|
||||
### setRequestTimeout
|
||||
Set how long to wait for a request to respond, in seconds.
|
||||
Set the "read" timeout in seconds. This is the timeout interval to use when waiting for additional data.
|
||||
|
||||
```js
|
||||
cordova.plugin.http.setRequestTimeout(5.0);
|
||||
@@ -175,11 +196,28 @@ cordova.plugin.http.setServerTrustMode('nocheck', function() {
|
||||
});
|
||||
```
|
||||
|
||||
### disableRedirect (deprecated)
|
||||
This function was deprecated in 2.0.9. Use ["setFollowRedirect"](#setFollowRedirect) instead.
|
||||
### setClientAuthMode<a name="setClientAuthMode"></a>
|
||||
Configure X.509 client certificate authentication. Takes mode and options. `mode` being one of following values:
|
||||
|
||||
### setSSLCertMode (deprecated)
|
||||
This function was deprecated in 2.0.8. Use ["setServerTrustMode"](#setServerTrustMode) instead.
|
||||
* `none`: disable client certificate authentication
|
||||
* `systemstore` (only on Android): use client certificate installed in the Android system store; user will be presented with a list of all installed certificates
|
||||
* `buffer`: use given client certificate; you will need to provide an options object:
|
||||
* `rawPkcs`: ArrayBuffer containing raw PKCS12 container with client certificate and private key
|
||||
* `pkcsPassword`: password of the PKCS container
|
||||
|
||||
```js
|
||||
// enable client auth using PKCS12 container given in ArrayBuffer `myPkcs12ArrayBuffer`
|
||||
cordova.plugin.http.setClientAuthMode('buffer', {
|
||||
rawPkcs: myPkcs12ArrayBuffer,
|
||||
pkcsPassword: 'mySecretPassword'
|
||||
}, success, fail);
|
||||
|
||||
// enable client auth using certificate in system store (only on Android)
|
||||
cordova.plugin.http.setClientAuthMode('systemstore', {}, success, fail);
|
||||
|
||||
// disable client auth
|
||||
cordova.plugin.http.setClientAuthMode('none', {}, success, fail);
|
||||
```
|
||||
|
||||
### removeCookies
|
||||
Remove all cookies associated with a given URL.
|
||||
@@ -196,15 +234,15 @@ Execute a HTTP request. Takes a URL and an options object. This is the internal
|
||||
The options object contains following keys:
|
||||
|
||||
* `method`: HTTP method to be used, defaults to `get`, needs to be one of the following values:
|
||||
* `get`, `post`, `put`, `patch`, `head`, `delete`, `upload`, `download`
|
||||
* `get`, `post`, `put`, `patch`, `head`, `delete`, `options`, `upload`, `download`
|
||||
* `data`: payload to be send to the server (only applicable on `post`, `put` or `patch` methods)
|
||||
* `params`: query params to be appended to the URL (only applicable on `get`, `head`, `delete`, `upload` or `download` methods)
|
||||
* `serializer`: data serializer to be used (only applicable on `post`, `put` or `patch` methods), defaults to global serializer value, see [setDataSerializer](#setDataSerializer) for supported values
|
||||
* `responseType`: expected response type, defaults to `text`, needs to be one of the following values:
|
||||
* `text`: data is returned as decoded string, use this for all kinds of string responses (e.g. XML, HTML, plain text, etc.)
|
||||
* `json` data is treated as JSON and returned as parsed object
|
||||
* `arraybuffer`: data is returned as [ArrayBuffer instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
|
||||
* `blob`: data is returned as [Blob instance](https://developer.mozilla.org/en-US/docs/Web/API/Blob)
|
||||
* `json` data is treated as JSON and returned as parsed object, returns `undefined` when response body is empty
|
||||
* `arraybuffer`: data is returned as [ArrayBuffer instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), returns `null` when response body is empty
|
||||
* `blob`: data is returned as [Blob instance](https://developer.mozilla.org/en-US/docs/Web/API/Blob), returns `null` when response body is empty
|
||||
* `timeout`: timeout value for the request in seconds, defaults to global timeout value
|
||||
* `followRedirect`: enable or disable automatically following redirects
|
||||
* `headers`: headers object (key value pair), will be merged with global values
|
||||
@@ -332,6 +370,9 @@ Execute a DELETE request. Takes a URL, parameters, and headers. See the [post]
|
||||
### head<a name="head"></a>
|
||||
Execute a HEAD request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
|
||||
|
||||
### options<a name="options"></a>
|
||||
Execute a OPTIONS request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure.
|
||||
|
||||
### uploadFile<a name="uploadFile"></a>
|
||||
Uploads one or more file(s) saved on the device. Takes a URL, parameters, headers, filePath(s), and the name(s) of the parameter to pass the file along as. See the [post](#post) documentation for details on what is returned on success and failure.
|
||||
|
||||
@@ -372,6 +413,46 @@ cordova.plugin.http.downloadFile("https://google.com/", {
|
||||
});
|
||||
```
|
||||
|
||||
### abort<a name="abort"></a>
|
||||
Abort a HTTP request. Takes the `requestId` which is returned by [sendRequest](#sendRequest) and its shorthand functions ([post](#post), [get](#get), [put](#put), [patch](#patch), [delete](#delete), [head](#head), [uploadFile](#uploadFile) and [downloadFile](#downloadFile)).
|
||||
|
||||
If the request already has finished, the request will finish normally and the abort call result will be `{ aborted: false }`.
|
||||
|
||||
If the request is still in progress, the request's `failure` callback will be invoked with response `{ status: -8 }`, and the abort call result `{ aborted: true }`.
|
||||
|
||||
:warning: Not supported for Android < 6 (API level < 23). For Android 5.1 and below, calling `abort(reqestId)` will have no effect, i.e. the requests will finish as if the request was not cancelled.
|
||||
|
||||
```js
|
||||
// start a request and get its requestId
|
||||
var requestId = cordova.plugin.http.downloadFile("https://google.com/", {
|
||||
id: '12',
|
||||
message: 'test'
|
||||
}, { Authorization: 'OAuth2: token' }, 'file:///somepicture.jpg', function(entry) {
|
||||
// prints the filename
|
||||
console.log(entry.name);
|
||||
|
||||
// prints the filePath
|
||||
console.log(entry.fullPath);
|
||||
}, function(response) {
|
||||
// if request was actually aborted, failure callback with status -8 will be invoked
|
||||
if(response.status === -8){
|
||||
console.log('download aborted');
|
||||
} else {
|
||||
console.error(response.error);
|
||||
}
|
||||
});
|
||||
|
||||
//...
|
||||
|
||||
// abort request
|
||||
cordova.plugin.http.abort(requestId, function(result) {
|
||||
// prints if request was aborted: true | false
|
||||
console.log(result.aborted);
|
||||
}, function(response) {
|
||||
console.error(response.error);
|
||||
});
|
||||
```
|
||||
|
||||
## Browser support<a name="browserSupport"></a>
|
||||
|
||||
This plugin supports a very restricted set of functions on the browser platform.
|
||||
@@ -405,6 +486,28 @@ This plugin uses amazing cloud services to maintain quality. CI Builds and E2E t
|
||||
* [BrowserStack](https://www.browserstack.com/)
|
||||
* [Sauce Labs](https://saucelabs.com/)
|
||||
* [httpbin.org](https://httpbin.org/)
|
||||
* [go-httpbin](https://httpbingo.org/)
|
||||
|
||||
### Local Testing
|
||||
|
||||
First, install current package with `npm install` to fetch dev dependencies.
|
||||
|
||||
Then, to execute Javascript tests:
|
||||
```shell
|
||||
npm run testjs
|
||||
```
|
||||
|
||||
And, to execute E2E tests:
|
||||
- setup local Android sdk and emulators, or Xcode and simulators for iOS
|
||||
- launch emulator or simulator
|
||||
- install [Appium](http://appium.io/) (see [Getting Started](https://github.com/appium/appium/blob/HEAD/docs/en/about-appium/getting-started.md))
|
||||
- start `appium`
|
||||
- run
|
||||
- updating client and server certificates, building test app, and running e2e tests
|
||||
```shell
|
||||
npm run testandroid
|
||||
npm run testios
|
||||
```
|
||||
|
||||
## Contribute & Develop
|
||||
|
||||
|
||||
17079
package-lock.json
generated
17079
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cordova-plugin-advanced-http",
|
||||
"version": "2.3.0",
|
||||
"version": "3.2.0",
|
||||
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
|
||||
"scripts": {
|
||||
"updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js",
|
||||
@@ -50,7 +50,7 @@
|
||||
"pvsaikrishna",
|
||||
"cvillerm",
|
||||
"hideov",
|
||||
"Mobisys"
|
||||
"silkimen"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
@@ -58,15 +58,12 @@
|
||||
},
|
||||
"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": "8.1.2",
|
||||
"mocha": "4.0.0",
|
||||
"mock-require": "2.0.2",
|
||||
"mz": "2.7.0",
|
||||
"chai": "4.2.0",
|
||||
"colors": "1.4.0",
|
||||
"cordova": "10.0.0",
|
||||
"mocha": "8.2.0",
|
||||
"umd-tough-cookie": "2.4.3",
|
||||
"wd": "1.4.1",
|
||||
"xml2js": "0.4.19"
|
||||
"wd": "1.12.1",
|
||||
"xml2js": "0.4.23"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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="2.3.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="3.2.0">
|
||||
<name>Advanced HTTP plugin</name>
|
||||
<description>
|
||||
Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning
|
||||
@@ -8,6 +8,7 @@
|
||||
<engine name="cordova" version=">=4.0.0"/>
|
||||
</engines>
|
||||
<dependency id="cordova-plugin-file" version=">=2.0.0"/>
|
||||
<preference name="AndroidBlacklistSecureSocketProtocols" default="SSLv3,TLSv1"/>
|
||||
<js-module src="www/cookie-handler.js" name="cookie-handler"/>
|
||||
<js-module src="www/dependency-validator.js" name="dependency-validator"/>
|
||||
<js-module src="www/error-codes.js" name="error-codes"/>
|
||||
@@ -31,6 +32,7 @@
|
||||
</feature>
|
||||
</config-file>
|
||||
<header-file src="src/ios/CordovaHttpPlugin.h"/>
|
||||
<header-file src="src/ios/BinaryRequestSerializer.h"/>
|
||||
<header-file src="src/ios/BinaryResponseSerializer.h"/>
|
||||
<header-file src="src/ios/TextResponseSerializer.h"/>
|
||||
<header-file src="src/ios/TextRequestSerializer.h"/>
|
||||
@@ -43,6 +45,7 @@
|
||||
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
|
||||
<header-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.h"/>
|
||||
<source-file src="src/ios/CordovaHttpPlugin.m"/>
|
||||
<source-file src="src/ios/BinaryRequestSerializer.m"/>
|
||||
<source-file src="src/ios/BinaryResponseSerializer.m"/>
|
||||
<source-file src="src/ios/TextResponseSerializer.m"/>
|
||||
<source-file src="src/ios/TextRequestSerializer.m"/>
|
||||
@@ -72,16 +75,14 @@
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpResponse.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaObservableCallbackContext.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/cordovahttp/CordovaServerTrust.java" target-dir="src/com/silkimen/cordovahttp"/>
|
||||
<source-file src="src/android/com/silkimen/http/HttpBodyDecoder.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/HttpRequest.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/JsonUtils.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/KeyChainKeyManager.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/OkConnectionFactory.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/TLSConfiguration.java" target-dir="src/com/silkimen/http"/>
|
||||
<source-file src="src/android/com/silkimen/http/TLSSocketFactory.java" target-dir="src/com/silkimen/http"/>
|
||||
<preference name="OKHTTP_VERSION" default="3.10.0"/>
|
||||
<framework src="com.squareup.okhttp3:okhttp-urlconnection:$OKHTTP_VERSION"/>
|
||||
</platform>
|
||||
<platform name="browser">
|
||||
<config-file target="config.xml" parent="/*">
|
||||
|
||||
@@ -3,12 +3,12 @@ set -e
|
||||
|
||||
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd ..; pwd )"
|
||||
|
||||
if [ $CI == "true" ] && ([ -z $SAUCE_USERNAME ] || [ -z $SAUCE_ACCESS_KEY ]); then
|
||||
echo "Skipping CI tests, because Saucelabs credentials are not set.";
|
||||
if [ $CI == "true" ] && ([ -z $SAUCE_USERNAME ] || [ -z $SAUCE_ACCESS_KEY ]) && ([ -z $BROWSERSTACK_USERNAME ] || [ -z $BROWSERSTACK_ACCESS_KEY ]); then
|
||||
echo "Skipping CI tests, because Saucelabs and BrowserStack credentials are not set.";
|
||||
exit 0;
|
||||
fi
|
||||
|
||||
printf 'Running e2e tests\n'
|
||||
pushd $ROOT
|
||||
./node_modules/.bin/mocha ./test/e2e-tooling/test.js "$@"
|
||||
./node_modules/.bin/mocha --reporter ./test/e2e-tooling/reporter.js ./test/e2e-tooling/test.js "$@"
|
||||
popd
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const args = process.argv.slice(2);
|
||||
const fs = require('mz/fs');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const xml2js = require('xml2js');
|
||||
const xmlPath = path.join(__dirname, '..', 'plugin.xml');
|
||||
@@ -22,10 +22,12 @@ const stringify = obj => {
|
||||
return builder.buildObject(obj);
|
||||
};
|
||||
|
||||
fs.readFile(xmlPath, 'utf-8')
|
||||
.then(xml => parse(xml))
|
||||
.then(parsed => {
|
||||
parsed.plugin.$.version = args[0];
|
||||
const update = async (version) => {
|
||||
const xml = fs.readFileSync(xmlPath, 'utf-8');
|
||||
const parsed = await parse(xml);
|
||||
|
||||
return fs.writeFile(xmlPath, stringify(parsed));
|
||||
});
|
||||
parsed.plugin.$.version = version;
|
||||
fs.writeFileSync(xmlPath, stringify(parsed));
|
||||
};
|
||||
|
||||
return update(args[0]);
|
||||
|
||||
33
scripts/upload-browserstack.sh
Executable file
33
scripts/upload-browserstack.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
PLATFORM=$([[ "${@#--android}" = "$@" ]] && echo "ios" || echo "android")
|
||||
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd ..; pwd )"
|
||||
TEMP=$ROOT/temp
|
||||
|
||||
if [ -z $BROWSERSTACK_USERNAME ] || [ -z $BROWSERSTACK_ACCESS_KEY ]; then
|
||||
echo "Skipping uploading artifact, because BrowserStack credentials are not set.";
|
||||
exit 0;
|
||||
fi
|
||||
|
||||
if [ $PLATFORM = "android" ]; then
|
||||
curl -u $BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_KEY \
|
||||
-X POST \
|
||||
https://api-cloud.browserstack.com/app-automate/upload \
|
||||
-F "file=@$TEMP/platforms/android/app/build/outputs/apk/debug/app-debug.apk" \
|
||||
-F "data={\"custom_id\": \"HttpTestAppAndroid\"}"
|
||||
else
|
||||
rm -rf $TEMP/HttpDemo.ipa
|
||||
pushd $TEMP/platforms/ios/build/emulator
|
||||
rm -rf ./Payload
|
||||
mkdir -p ./Payload
|
||||
cp -r ./HttpDemo.app ./Payload/HttpDemo.app
|
||||
zip -r $TEMP/HttpDemo.ipa ./Payload
|
||||
popd
|
||||
|
||||
curl -u $BROWSERSTACK_USERNAME:$BROWSERSTACK_ACCESS_KEY \
|
||||
-X POST \
|
||||
https://api-cloud.browserstack.com/app-automate/upload \
|
||||
-F "file=@$TEMP/HttpDemo.ipa" \
|
||||
-F "data={\"custom_id\": \"HttpTestAppIos\"}"
|
||||
fi
|
||||
@@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@@ -16,11 +17,8 @@ import com.silkimen.http.HttpBodyDecoder;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||
import com.silkimen.http.JsonUtils;
|
||||
import com.silkimen.http.OkConnectionFactory;
|
||||
import com.silkimen.http.TLSConfiguration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -40,11 +38,11 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
protected int timeout;
|
||||
protected boolean followRedirects;
|
||||
protected TLSConfiguration tlsConfiguration;
|
||||
protected CallbackContext callbackContext;
|
||||
protected CordovaObservableCallbackContext callbackContext;
|
||||
|
||||
public CordovaHttpBase(String method, String url, String serializer, Object data, JSONObject headers, int timeout,
|
||||
boolean followRedirects, String responseType, TLSConfiguration tlsConfiguration,
|
||||
CallbackContext callbackContext) {
|
||||
CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
@@ -59,7 +57,7 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
}
|
||||
|
||||
public CordovaHttpBase(String method, String url, JSONObject headers, int timeout, boolean followRedirects,
|
||||
String responseType, TLSConfiguration tlsConfiguration, CallbackContext callbackContext) {
|
||||
String responseType, TLSConfiguration tlsConfiguration, CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
@@ -74,30 +72,39 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
CordovaHttpResponse response = new CordovaHttpResponse();
|
||||
HttpRequest request = null;
|
||||
|
||||
try {
|
||||
HttpRequest request = this.createRequest();
|
||||
request = this.createRequest();
|
||||
this.prepareRequest(request);
|
||||
this.sendBody(request);
|
||||
this.processResponse(request, response);
|
||||
request.disconnect();
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof SSLException) {
|
||||
Throwable cause = e.getCause();
|
||||
String message = cause.getMessage();
|
||||
|
||||
if (cause instanceof SSLException) {
|
||||
response.setStatus(-2);
|
||||
response.setErrorMessage("TLS connection could not be established: " + e.getMessage());
|
||||
Log.w(TAG, "TLS connection could not be established", e);
|
||||
} else if (e.getCause() instanceof UnknownHostException) {
|
||||
} else if (cause instanceof UnknownHostException) {
|
||||
response.setStatus(-3);
|
||||
response.setErrorMessage("Host could not be resolved: " + e.getMessage());
|
||||
Log.w(TAG, "Host could not be resolved", e);
|
||||
} else if (e.getCause() instanceof SocketTimeoutException) {
|
||||
} else if (cause instanceof SocketTimeoutException) {
|
||||
response.setStatus(-4);
|
||||
response.setErrorMessage("Request timed out: " + e.getMessage());
|
||||
Log.w(TAG, "Request timed out", e);
|
||||
} else if (cause instanceof InterruptedIOException && "thread interrupted".equals(message.toLowerCase())) {
|
||||
this.setAborted(request, response);
|
||||
} else {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage("There was an error with the request: " + e.getCause().getMessage());
|
||||
response.setErrorMessage("There was an error with the request: " + message);
|
||||
Log.w(TAG, "Generic request error", e);
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
this.setAborted(request, response);
|
||||
} catch (Exception e) {
|
||||
response.setStatus(-1);
|
||||
response.setErrorMessage(e.getMessage());
|
||||
@@ -124,7 +131,6 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
request.readTimeout(this.timeout);
|
||||
request.acceptCharset("UTF-8");
|
||||
request.uncompress(true);
|
||||
HttpRequest.setConnectionFactory(new OkConnectionFactory());
|
||||
|
||||
if (this.tlsConfiguration.getHostnameVerifier() != null) {
|
||||
request.setHostnameVerifier(this.tlsConfiguration.getHostnameVerifier());
|
||||
@@ -143,10 +149,12 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
request.contentType("application/json", "UTF-8");
|
||||
} else if ("utf8".equals(this.serializer)) {
|
||||
request.contentType("text/plain", "UTF-8");
|
||||
} else if ("raw".equals(this.serializer)) {
|
||||
request.contentType("application/octet-stream");
|
||||
} else if ("urlencoded".equals(this.serializer)) {
|
||||
// intentionally left blank, because content type is set in HttpRequest.form()
|
||||
} else if ("multipart".equals(this.serializer)) {
|
||||
request.contentType("multipart/form-data");
|
||||
// intentionally left blank, because content type is set in HttpRequest.part()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,6 +167,8 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
request.send(this.data.toString());
|
||||
} else if ("utf8".equals(this.serializer)) {
|
||||
request.send(((JSONObject) this.data).getString("text"));
|
||||
} else if ("raw".equals(this.serializer)) {
|
||||
request.send(Base64.decode((String)this.data, Base64.DEFAULT));
|
||||
} else if ("urlencoded".equals(this.serializer)) {
|
||||
request.form(JsonUtils.getObjectMap((JSONObject) this.data));
|
||||
} else if ("multipart".equals(this.serializer)) {
|
||||
@@ -177,6 +187,12 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
request.part(name, fileNames.getString(i), types.getString(i), new ByteArrayInputStream(bytes));
|
||||
}
|
||||
}
|
||||
|
||||
// prevent sending malformed empty multipart requests (#372)
|
||||
if (buffers.length() == 0) {
|
||||
request.contentType("multipart/form-data; boundary=00content0boundary00");
|
||||
request.send("\r\n--00content0boundary00--\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +205,7 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
response.setHeaders(request.headers());
|
||||
|
||||
if (request.code() >= 200 && request.code() < 300) {
|
||||
if ("text".equals(this.responseType)) {
|
||||
if ("text".equals(this.responseType) || "json".equals(this.responseType)) {
|
||||
String decoded = HttpBodyDecoder.decodeBody(outputStream.toByteArray(), request.charset());
|
||||
response.setBody(decoded);
|
||||
} else {
|
||||
@@ -199,4 +215,19 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
response.setErrorMessage(HttpBodyDecoder.decodeBody(outputStream.toByteArray(), request.charset()));
|
||||
}
|
||||
}
|
||||
|
||||
protected void setAborted(HttpRequest request, CordovaHttpResponse response) {
|
||||
response.setStatus(-8);
|
||||
response.setErrorMessage("Request was aborted");
|
||||
|
||||
if (request != null) {
|
||||
try {
|
||||
request.disconnect();
|
||||
} catch(Exception any){
|
||||
Log.w(TAG, "Failed to close aborted request", any);
|
||||
}
|
||||
}
|
||||
|
||||
Log.i(TAG, "Request was aborted");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import javax.net.ssl.SSLSocketFactory;
|
||||
import com.silkimen.http.HttpRequest;
|
||||
import com.silkimen.http.TLSConfiguration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.file.FileUtils;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -17,7 +16,7 @@ class CordovaHttpDownload extends CordovaHttpBase {
|
||||
private String filePath;
|
||||
|
||||
public CordovaHttpDownload(String url, JSONObject headers, String filePath, int timeout, boolean followRedirects,
|
||||
TLSConfiguration tlsConfiguration, CallbackContext callbackContext) {
|
||||
TLSConfiguration tlsConfiguration, CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
super("GET", url, headers, timeout, followRedirects, "text", tlsConfiguration, callbackContext);
|
||||
this.filePath = filePath;
|
||||
|
||||
@@ -5,20 +5,19 @@ import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import com.silkimen.http.TLSConfiguration;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONObject;
|
||||
|
||||
class CordovaHttpOperation extends CordovaHttpBase {
|
||||
public CordovaHttpOperation(String method, String url, String serializer, Object data, JSONObject headers,
|
||||
int timeout, boolean followRedirects, String responseType, TLSConfiguration tlsConfiguration,
|
||||
CallbackContext callbackContext) {
|
||||
CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
super(method, url, serializer, data, headers, timeout, followRedirects, responseType, tlsConfiguration,
|
||||
callbackContext);
|
||||
}
|
||||
|
||||
public CordovaHttpOperation(String method, String url, JSONObject headers, int timeout, boolean followRedirects,
|
||||
String responseType, TLSConfiguration tlsConfiguration, CallbackContext callbackContext) {
|
||||
String responseType, TLSConfiguration tlsConfiguration, CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
super(method, url, headers, timeout, followRedirects, responseType, tlsConfiguration, callbackContext);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import java.security.KeyStore;
|
||||
import java.util.HashMap;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import com.silkimen.http.TLSConfiguration;
|
||||
|
||||
@@ -17,17 +21,22 @@ import android.util.Base64;
|
||||
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
|
||||
public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
public class CordovaHttpPlugin extends CordovaPlugin implements Observer {
|
||||
private static final String TAG = "Cordova-Plugin-HTTP";
|
||||
|
||||
private TLSConfiguration tlsConfiguration;
|
||||
|
||||
private HashMap<Integer, Future<?>> reqMap;
|
||||
private final Object reqMapLock = new Object();
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
super.initialize(cordova, webView);
|
||||
|
||||
this.tlsConfiguration = new TLSConfiguration();
|
||||
|
||||
this.reqMap = new HashMap<Integer, Future<?>>();
|
||||
|
||||
try {
|
||||
KeyStore store = KeyStore.getInstance("AndroidCAStore");
|
||||
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
|
||||
@@ -38,6 +47,13 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
|
||||
this.tlsConfiguration.setHostnameVerifier(null);
|
||||
this.tlsConfiguration.setTrustManagers(tmf.getTrustManagers());
|
||||
|
||||
if (this.preferences.contains("androidblacklistsecuresocketprotocols")) {
|
||||
this.tlsConfiguration.setBlacklistedProtocols(
|
||||
this.preferences.getString("androidblacklistsecuresocketprotocols", "").split(",")
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "An error occured while loading system's CA certificates", e);
|
||||
}
|
||||
@@ -57,6 +73,8 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
} else if ("delete".equals(action)) {
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
} else if ("options".equals(action)) {
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
} else if ("post".equals(action)) {
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
} else if ("put".equals(action)) {
|
||||
@@ -71,6 +89,8 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
return this.setServerTrustMode(args, callbackContext);
|
||||
} else if ("setClientAuthMode".equals(action)) {
|
||||
return this.setClientAuthMode(args, callbackContext);
|
||||
} else if ("abort".equals(action)) {
|
||||
return this.abort(args, callbackContext);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -85,10 +105,13 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
boolean followRedirect = args.getBoolean(3);
|
||||
String responseType = args.getString(4);
|
||||
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, headers, timeout, followRedirect,
|
||||
responseType, this.tlsConfiguration, callbackContext);
|
||||
Integer reqId = args.getInt(5);
|
||||
CordovaObservableCallbackContext observableCallbackContext = new CordovaObservableCallbackContext(callbackContext, reqId);
|
||||
|
||||
cordova.getThreadPool().execute(request);
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, headers, timeout, followRedirect,
|
||||
responseType, this.tlsConfiguration, observableCallbackContext);
|
||||
|
||||
startRequest(reqId, observableCallbackContext, request);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -104,10 +127,13 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
boolean followRedirect = args.getBoolean(5);
|
||||
String responseType = args.getString(6);
|
||||
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, serializer, data, headers,
|
||||
timeout, followRedirect, responseType, this.tlsConfiguration, callbackContext);
|
||||
Integer reqId = args.getInt(7);
|
||||
CordovaObservableCallbackContext observableCallbackContext = new CordovaObservableCallbackContext(callbackContext, reqId);
|
||||
|
||||
cordova.getThreadPool().execute(request);
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, serializer, data, headers,
|
||||
timeout, followRedirect, responseType, this.tlsConfiguration, observableCallbackContext);
|
||||
|
||||
startRequest(reqId, observableCallbackContext, request);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -121,10 +147,13 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
boolean followRedirect = args.getBoolean(5);
|
||||
String responseType = args.getString(6);
|
||||
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, headers, filePaths, uploadNames, timeout, followRedirect,
|
||||
responseType, this.tlsConfiguration, this.cordova.getActivity().getApplicationContext(), callbackContext);
|
||||
Integer reqId = args.getInt(7);
|
||||
CordovaObservableCallbackContext observableCallbackContext = new CordovaObservableCallbackContext(callbackContext, reqId);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, headers, filePaths, uploadNames, timeout, followRedirect,
|
||||
responseType, this.tlsConfiguration, this.cordova.getActivity().getApplicationContext(), observableCallbackContext);
|
||||
|
||||
startRequest(reqId, observableCallbackContext, upload);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -136,14 +165,25 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
boolean followRedirect = args.getBoolean(4);
|
||||
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, headers, filePath, timeout, followRedirect,
|
||||
this.tlsConfiguration, callbackContext);
|
||||
Integer reqId = args.getInt(5);
|
||||
CordovaObservableCallbackContext observableCallbackContext = new CordovaObservableCallbackContext(callbackContext, reqId);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, headers, filePath, timeout, followRedirect,
|
||||
this.tlsConfiguration, observableCallbackContext);
|
||||
|
||||
startRequest(reqId, observableCallbackContext, download);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void startRequest(Integer reqId, CordovaObservableCallbackContext observableCallbackContext, CordovaHttpBase request) {
|
||||
synchronized (reqMapLock) {
|
||||
observableCallbackContext.setObserver(this);
|
||||
Future<?> task = cordova.getThreadPool().submit(request);
|
||||
this.addReq(reqId, task, observableCallbackContext);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean setServerTrustMode(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
CordovaServerTrust runnable = new CordovaServerTrust(args.getString(0), this.cordova.getActivity(),
|
||||
this.tlsConfiguration, callbackContext);
|
||||
@@ -164,4 +204,45 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean abort(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
int reqId = args.getInt(0);
|
||||
boolean result = false;
|
||||
// NOTE no synchronized (reqMapLock), since even if the req was already removed from reqMap,
|
||||
// the worst that would happen calling task.cancel(true) is a result of false
|
||||
// (i.e. same result as locking & not finding the req in reqMap)
|
||||
Future<?> task = this.reqMap.get(reqId);
|
||||
|
||||
if (task != null && !task.isDone()) {
|
||||
result = task.cancel(true);
|
||||
}
|
||||
|
||||
callbackContext.success(new JSONObject().put("aborted", result));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addReq(final Integer reqId, final Future<?> task, final CordovaObservableCallbackContext observableCallbackContext) {
|
||||
synchronized (reqMapLock) {
|
||||
if (!task.isDone()){
|
||||
this.reqMap.put(reqId, task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeReq(final Integer reqId) {
|
||||
synchronized (reqMapLock) {
|
||||
this.reqMap.remove(reqId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
synchronized (reqMapLock) {
|
||||
CordovaObservableCallbackContext c = (CordovaObservableCallbackContext) arg;
|
||||
if (c.getCallbackContext().isFinished()) {
|
||||
removeReq(c.getRequestId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import java.net.URI;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -28,7 +27,7 @@ class CordovaHttpUpload extends CordovaHttpBase {
|
||||
|
||||
public CordovaHttpUpload(String url, JSONObject headers, JSONArray filePaths, JSONArray uploadNames, int timeout,
|
||||
boolean followRedirects, String responseType, TLSConfiguration tlsConfiguration,
|
||||
Context applicationContext, CallbackContext callbackContext) {
|
||||
Context applicationContext, CordovaObservableCallbackContext callbackContext) {
|
||||
|
||||
super("POST", url, headers, timeout, followRedirects, responseType, tlsConfiguration, callbackContext);
|
||||
this.filePaths = filePaths;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.silkimen.cordovahttp;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Observer;
|
||||
|
||||
public class CordovaObservableCallbackContext {
|
||||
|
||||
private CallbackContext callbackContext;
|
||||
private Integer requestId;
|
||||
private Observer observer;
|
||||
|
||||
public CordovaObservableCallbackContext(CallbackContext callbackContext, Integer requestId) {
|
||||
this.callbackContext = callbackContext;
|
||||
this.requestId = requestId;
|
||||
}
|
||||
|
||||
public void success(JSONObject message) {
|
||||
this.callbackContext.success(message);
|
||||
this.notifyObserver();
|
||||
}
|
||||
|
||||
public void error(JSONObject message) {
|
||||
this.callbackContext.error(message);
|
||||
this.notifyObserver();
|
||||
}
|
||||
|
||||
public Integer getRequestId() {
|
||||
return this.requestId;
|
||||
}
|
||||
|
||||
public CallbackContext getCallbackContext() {
|
||||
return callbackContext;
|
||||
}
|
||||
|
||||
public Observer getObserver() {
|
||||
return observer;
|
||||
}
|
||||
|
||||
protected void notifyObserver() {
|
||||
if(this.observer != null){
|
||||
this.observer.update(null, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an observer that is notified, when {@link #success(JSONObject)}
|
||||
* or {@link #error(JSONObject)} are called.
|
||||
*
|
||||
* NOTE the observer is notified with
|
||||
* <pre>observer.update(null, cordovaObservableCallbackContext)</pre>
|
||||
* @param observer
|
||||
*/
|
||||
public void setObserver(Observer observer) {
|
||||
this.observer = observer;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.silkimen.http;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.OkUrlFactory;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.Proxy;
|
||||
|
||||
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
|
||||
public HttpURLConnection create(URL url) {
|
||||
OkUrlFactory urlFactory = new OkUrlFactory(this.client);
|
||||
|
||||
return (HttpURLConnection) urlFactory.open(url);
|
||||
}
|
||||
|
||||
public HttpURLConnection create(URL url, Proxy proxy) {
|
||||
OkHttpClient clientWithProxy = new OkHttpClient.Builder().proxy(proxy).build();
|
||||
OkUrlFactory urlFactory = new OkUrlFactory(clientWithProxy);
|
||||
|
||||
return (HttpURLConnection) urlFactory.open(url);
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,10 @@ import javax.net.ssl.TrustManager;
|
||||
import com.silkimen.http.TLSSocketFactory;
|
||||
|
||||
public class TLSConfiguration {
|
||||
private TrustManager[] trustManagers;
|
||||
private KeyManager[] keyManagers;
|
||||
private HostnameVerifier hostnameVerifier;
|
||||
private TrustManager[] trustManagers = null;
|
||||
private KeyManager[] keyManagers = null;
|
||||
private HostnameVerifier hostnameVerifier = null;
|
||||
private String[] blacklistedProtocols = {};
|
||||
|
||||
private SSLSocketFactory socketFactory;
|
||||
|
||||
@@ -33,6 +34,11 @@ public class TLSConfiguration {
|
||||
this.socketFactory = null;
|
||||
}
|
||||
|
||||
public void setBlacklistedProtocols(String[] protocols) {
|
||||
this.blacklistedProtocols = protocols;
|
||||
this.socketFactory = null;
|
||||
}
|
||||
|
||||
public HostnameVerifier getHostnameVerifier() {
|
||||
return this.hostnameVerifier;
|
||||
}
|
||||
@@ -46,12 +52,7 @@ public class TLSConfiguration {
|
||||
SSLContext context = SSLContext.getInstance("TLS");
|
||||
|
||||
context.init(this.keyManagers, this.trustManagers, new SecureRandom());
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT < 20) {
|
||||
this.socketFactory = new TLSSocketFactory(context);
|
||||
} else {
|
||||
this.socketFactory = context.getSocketFactory();
|
||||
}
|
||||
this.socketFactory = new TLSSocketFactory(context, this.blacklistedProtocols);
|
||||
|
||||
return this.socketFactory;
|
||||
} catch (GeneralSecurityException e) {
|
||||
|
||||
@@ -5,6 +5,9 @@ import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
@@ -12,9 +15,11 @@ import javax.net.ssl.SSLSocketFactory;
|
||||
public class TLSSocketFactory extends SSLSocketFactory {
|
||||
|
||||
private SSLSocketFactory delegate;
|
||||
private String[] blacklistedProtocols;
|
||||
|
||||
public TLSSocketFactory(SSLContext context) {
|
||||
delegate = context.getSocketFactory();
|
||||
public TLSSocketFactory(SSLContext context, String[] blacklistedProtocols) {
|
||||
this.delegate = context.getSocketFactory();
|
||||
this.blacklistedProtocols = Arrays.stream(blacklistedProtocols).map(String::trim).toArray(String[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,9 +60,18 @@ public class TLSSocketFactory extends SSLSocketFactory {
|
||||
}
|
||||
|
||||
private Socket enableTLSOnSocket(Socket socket) {
|
||||
if (socket != null && (socket instanceof SSLSocket)) {
|
||||
((SSLSocket) socket).setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" });
|
||||
if (socket == null || !(socket instanceof SSLSocket)) {
|
||||
return socket;
|
||||
}
|
||||
|
||||
String[] supported = ((SSLSocket) socket).getSupportedProtocols();
|
||||
|
||||
String[] filtered = Arrays.stream(supported).filter(
|
||||
val -> Arrays.stream(this.blacklistedProtocols).noneMatch(val::equals)
|
||||
).toArray(String[]::new);
|
||||
|
||||
((SSLSocket) socket).setEnabledProtocols(filtered);
|
||||
|
||||
return socket;
|
||||
}
|
||||
}
|
||||
|
||||
128
src/browser/cordova-http-plugin.js
vendored
128
src/browser/cordova-http-plugin.js
vendored
@@ -3,6 +3,8 @@ var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
|
||||
var cordovaProxy = require('cordova/exec/proxy');
|
||||
var jsUtil = require(pluginId + '.js-util');
|
||||
|
||||
var reqMap = {};
|
||||
|
||||
function serializeJsonData(data) {
|
||||
try {
|
||||
return JSON.stringify(data);
|
||||
@@ -20,7 +22,7 @@ function serializePrimitive(key, value) {
|
||||
}
|
||||
|
||||
function serializeArray(key, values) {
|
||||
return values.map(function(value) {
|
||||
return values.map(function (value) {
|
||||
return encodeURIComponent(key) + '[]=' + encodeURIComponent(value);
|
||||
}).join('&');
|
||||
}
|
||||
@@ -28,7 +30,7 @@ function serializeArray(key, values) {
|
||||
function serializeParams(params) {
|
||||
if (params === null) return '';
|
||||
|
||||
return Object.keys(params).map(function(key) {
|
||||
return Object.keys(params).map(function (key) {
|
||||
if (jsUtil.getTypeOf(params[key]) === 'Array') {
|
||||
return serializeArray(key, params[key]);
|
||||
}
|
||||
@@ -37,6 +39,39 @@ function serializeParams(params) {
|
||||
}).join('&');
|
||||
}
|
||||
|
||||
function decodeB64(dataString) {
|
||||
var binaryString = atob(dataString);
|
||||
var bytes = new Uint8Array(binaryString.length);
|
||||
|
||||
for (var i = 0; i < binaryString.length; ++i) {
|
||||
bytes[i] = binaryString.charCodeAt(i);
|
||||
}
|
||||
|
||||
return bytes.buffer;
|
||||
}
|
||||
|
||||
function processMultipartData(data) {
|
||||
if (!data) return null;
|
||||
|
||||
var fd = new FormData();
|
||||
|
||||
for (var i = 0; i < data.buffers.length; ++i) {
|
||||
var buffer = data.buffers[i];
|
||||
var name = data.names[i];
|
||||
var fileName = data.fileNames[i];
|
||||
var type = data.types[i];
|
||||
|
||||
if (fileName) {
|
||||
fd.append(name, new Blob([decodeB64(buffer)], { type: type }), fileName);
|
||||
} else {
|
||||
// we assume it's plain text if no filename was given
|
||||
fd.append(name, atob(buffer));
|
||||
}
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
function deserializeResponseHeaders(headers) {
|
||||
var headerMap = {};
|
||||
var arr = headers.trim().split(/[\r\n]+/);
|
||||
@@ -82,10 +117,17 @@ function createXhrFailureObject(xhr) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
function injectRequestIdHandler(reqId, cb) {
|
||||
return function (response) {
|
||||
delete reqMap[reqId];
|
||||
cb(response);
|
||||
}
|
||||
}
|
||||
|
||||
function getHeaderValue(headers, headerName) {
|
||||
let result = null;
|
||||
|
||||
Object.keys(headers).forEach(function(key) {
|
||||
Object.keys(headers).forEach(function (key) {
|
||||
if (key.toLowerCase() === headerName.toLowerCase()) {
|
||||
result = headers[key];
|
||||
}
|
||||
@@ -101,7 +143,7 @@ function setDefaultContentType(headers, contentType) {
|
||||
}
|
||||
|
||||
function setHeaders(xhr, headers) {
|
||||
Object.keys(headers).forEach(function(key) {
|
||||
Object.keys(headers).forEach(function (key) {
|
||||
if (key.toLowerCase() === 'cookie') return;
|
||||
|
||||
xhr.setRequestHeader(key, headers[key]);
|
||||
@@ -109,7 +151,7 @@ function setHeaders(xhr, headers) {
|
||||
}
|
||||
|
||||
function sendRequest(method, withData, opts, success, failure) {
|
||||
var data, serializer, headers, timeout, followRedirect, responseType;
|
||||
var data, serializer, headers, timeout, followRedirect, responseType, reqId;
|
||||
var url = opts[0];
|
||||
|
||||
if (withData) {
|
||||
@@ -119,25 +161,31 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
timeout = opts[4];
|
||||
followRedirect = opts[5];
|
||||
responseType = opts[6];
|
||||
reqId = opts[7];
|
||||
} else {
|
||||
headers = opts[1];
|
||||
timeout = opts[2];
|
||||
followRedirect = opts[3];
|
||||
responseType = opts[4];
|
||||
|
||||
reqId = opts[5];
|
||||
}
|
||||
|
||||
var onSuccess = injectRequestIdHandler(reqId, success);
|
||||
var onFail = injectRequestIdHandler(reqId, failure);
|
||||
|
||||
var processedData = null;
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
reqMap[reqId] = xhr;
|
||||
|
||||
xhr.open(method, url);
|
||||
|
||||
if (headers.Cookie && headers.Cookie.length > 0) {
|
||||
return failure('advanced-http: custom cookies not supported on browser platform');
|
||||
return onFail('advanced-http: custom cookies not supported on browser platform');
|
||||
}
|
||||
|
||||
if (!followRedirect) {
|
||||
return failure('advanced-http: disabling follow redirect not supported on browser platform');
|
||||
return onFail('advanced-http: disabling follow redirect not supported on browser platform');
|
||||
}
|
||||
|
||||
switch (serializer) {
|
||||
@@ -146,7 +194,7 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
processedData = serializeJsonData(data);
|
||||
|
||||
if (processedData === null) {
|
||||
return failure('advanced-http: failed serializing data');
|
||||
return onFail('advanced-http: failed serializing data');
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -160,29 +208,78 @@ function sendRequest(method, withData, opts, success, failure) {
|
||||
setDefaultContentType(headers, 'application/x-www-form-urlencoded');
|
||||
processedData = serializeParams(data);
|
||||
break;
|
||||
|
||||
case 'multipart':
|
||||
const contentType = getHeaderValue(headers, 'Content-Type');
|
||||
|
||||
// intentionally don't set a default content type
|
||||
// it's set by the browser together with the content disposition string
|
||||
if (contentType) {
|
||||
headers['Content-Type'] = contentType;
|
||||
}
|
||||
|
||||
processedData = processMultipartData(data);
|
||||
break;
|
||||
|
||||
case 'raw':
|
||||
setDefaultContentType(headers, 'application/octet-stream');
|
||||
processedData = data;
|
||||
break;
|
||||
}
|
||||
|
||||
// requesting text instead of JSON because it's parsed in the response handler
|
||||
xhr.responseType = responseType === 'json' ? 'text' : responseType;
|
||||
xhr.timeout = timeout * 1000;
|
||||
xhr.responseType = responseType;
|
||||
setHeaders(xhr, headers);
|
||||
|
||||
xhr.onerror = xhr.ontimeout = function () {
|
||||
return failure(createXhrFailureObject(xhr));
|
||||
xhr.onerror = function () {
|
||||
return onFail(createXhrFailureObject(xhr));
|
||||
};
|
||||
|
||||
xhr.onabort = function () {
|
||||
return onFail({
|
||||
status: -8,
|
||||
error: 'Request was aborted',
|
||||
url: url,
|
||||
headers: {}
|
||||
});
|
||||
};
|
||||
|
||||
xhr.ontimeout = function () {
|
||||
return onFail({
|
||||
status: -4,
|
||||
error: 'Request timed out',
|
||||
url: url,
|
||||
headers: {}
|
||||
});
|
||||
};
|
||||
|
||||
xhr.onload = function () {
|
||||
if (xhr.readyState !== xhr.DONE) return;
|
||||
|
||||
if (xhr.status < 200 || xhr.status > 299) {
|
||||
return failure(createXhrFailureObject(xhr));
|
||||
return onFail(createXhrFailureObject(xhr));
|
||||
}
|
||||
|
||||
return success(createXhrSuccessObject(xhr));
|
||||
return onSuccess(createXhrSuccessObject(xhr));
|
||||
};
|
||||
|
||||
xhr.send(processedData);
|
||||
}
|
||||
|
||||
function abort(opts, success, failure) {
|
||||
var reqId = opts[0];
|
||||
var result = false;
|
||||
|
||||
var xhr = reqMap[reqId];
|
||||
if(xhr && xhr.readyState !== xhr.DONE){
|
||||
xhr.abort();
|
||||
result = true;
|
||||
}
|
||||
|
||||
success({aborted: result});
|
||||
}
|
||||
|
||||
var browserInterface = {
|
||||
get: function (success, failure, opts) {
|
||||
return sendRequest('get', false, opts, success, failure);
|
||||
@@ -202,6 +299,9 @@ var browserInterface = {
|
||||
patch: function (success, failure, opts) {
|
||||
return sendRequest('patch', true, opts, success, failure);
|
||||
},
|
||||
abort: function (success, failure, opts) {
|
||||
return abort(opts, success, failure);
|
||||
},
|
||||
uploadFile: function (success, failure, opts) {
|
||||
return failure('advanced-http: function "uploadFile" not supported on browser platform');
|
||||
},
|
||||
|
||||
@@ -184,6 +184,8 @@
|
||||
{
|
||||
NSError *serializationError = nil;
|
||||
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
|
||||
if (serializationError) {
|
||||
if (failure) {
|
||||
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
|
||||
@@ -232,6 +234,8 @@
|
||||
{
|
||||
NSError *serializationError = nil;
|
||||
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
|
||||
if (serializationError) {
|
||||
if (failure) {
|
||||
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
|
||||
@@ -293,6 +297,8 @@
|
||||
{
|
||||
NSError *serializationError = nil;
|
||||
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
|
||||
if (serializationError) {
|
||||
if (failure) {
|
||||
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
|
||||
@@ -358,6 +364,8 @@
|
||||
{
|
||||
NSError *serializationError = nil;
|
||||
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
|
||||
[request setHTTPShouldHandleCookies:NO];
|
||||
|
||||
if (serializationError) {
|
||||
if (failure) {
|
||||
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
|
||||
|
||||
8
src/ios/BinaryRequestSerializer.h
Normal file
8
src/ios/BinaryRequestSerializer.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AFURLRequestSerialization.h"
|
||||
|
||||
@interface BinaryRequestSerializer : AFHTTPRequestSerializer
|
||||
|
||||
+ (instancetype)serializer;
|
||||
|
||||
@end
|
||||
53
src/ios/BinaryRequestSerializer.m
Normal file
53
src/ios/BinaryRequestSerializer.m
Normal file
@@ -0,0 +1,53 @@
|
||||
#import "BinaryRequestSerializer.h"
|
||||
|
||||
@implementation BinaryRequestSerializer
|
||||
|
||||
+ (instancetype)serializer
|
||||
{
|
||||
BinaryRequestSerializer *serializer = [[self alloc] init];
|
||||
return serializer;
|
||||
}
|
||||
|
||||
#pragma mark - AFURLRequestSerialization
|
||||
|
||||
- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
|
||||
withParameters:(id)parameters
|
||||
error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
NSParameterAssert(request);
|
||||
|
||||
if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
|
||||
return [super requestBySerializingRequest:request withParameters:parameters error:error];
|
||||
}
|
||||
|
||||
NSMutableURLRequest *mutableRequest = [request mutableCopy];
|
||||
|
||||
[self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
|
||||
if (![request valueForHTTPHeaderField:field]) {
|
||||
[mutableRequest setValue:value forHTTPHeaderField:field];
|
||||
}
|
||||
}];
|
||||
|
||||
if (parameters) {
|
||||
if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
|
||||
[mutableRequest setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
|
||||
}
|
||||
|
||||
[mutableRequest setHTTPBody: parameters];
|
||||
}
|
||||
|
||||
return mutableRequest;
|
||||
}
|
||||
|
||||
#pragma mark - NSSecureCoding
|
||||
|
||||
- (instancetype)initWithCoder:(NSCoder *)decoder {
|
||||
self = [super initWithCoder:decoder];
|
||||
if (!self) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -5,12 +5,16 @@
|
||||
@interface CordovaHttpPlugin : CDVPlugin
|
||||
|
||||
- (void)setServerTrustMode:(CDVInvokedUrlCommand*)command;
|
||||
- (void)setClientAuthMode:(CDVInvokedUrlCommand*)command;
|
||||
- (void)post:(CDVInvokedUrlCommand*)command;
|
||||
- (void)get:(CDVInvokedUrlCommand*)command;
|
||||
- (void)put:(CDVInvokedUrlCommand*)command;
|
||||
- (void)patch:(CDVInvokedUrlCommand*)command;
|
||||
- (void)get:(CDVInvokedUrlCommand*)command;
|
||||
- (void)delete:(CDVInvokedUrlCommand*)command;
|
||||
- (void)head:(CDVInvokedUrlCommand*)command;
|
||||
- (void)options:(CDVInvokedUrlCommand*)command;
|
||||
- (void)uploadFiles:(CDVInvokedUrlCommand*)command;
|
||||
- (void)downloadFile:(CDVInvokedUrlCommand*)command;
|
||||
- (void)abort:(CDVInvokedUrlCommand*)command;
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#import "CordovaHttpPlugin.h"
|
||||
#import "CDVFile.h"
|
||||
#import "BinaryRequestSerializer.h"
|
||||
#import "BinaryResponseSerializer.h"
|
||||
#import "TextResponseSerializer.h"
|
||||
#import "TextRequestSerializer.h"
|
||||
@@ -8,6 +9,8 @@
|
||||
|
||||
@interface CordovaHttpPlugin()
|
||||
|
||||
- (void)addRequest:(NSNumber*)reqId forTask:(NSURLSessionDataTask*)task;
|
||||
- (void)removeRequest:(NSNumber*)reqId;
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager;
|
||||
- (void)handleSuccess:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response andData:(id)data;
|
||||
- (void)handleError:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response error:(NSError*)error;
|
||||
@@ -20,10 +23,21 @@
|
||||
|
||||
@implementation CordovaHttpPlugin {
|
||||
AFSecurityPolicy *securityPolicy;
|
||||
NSURLCredential *x509Credential;
|
||||
NSMutableDictionary *reqDict;
|
||||
}
|
||||
|
||||
- (void)pluginInitialize {
|
||||
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
|
||||
reqDict = [NSMutableDictionary dictionary];
|
||||
}
|
||||
|
||||
- (void)addRequest:(NSNumber*)reqId forTask:(NSURLSessionDataTask*)task {
|
||||
[reqDict setObject:task forKey:reqId];
|
||||
}
|
||||
|
||||
- (void)removeRequest:(NSNumber*)reqId {
|
||||
[reqDict removeObjectForKey:reqId];
|
||||
}
|
||||
|
||||
- (void)setRequestSerializer:(NSString*)serializerName forManager:(AFHTTPSessionManager*)manager {
|
||||
@@ -31,11 +45,40 @@
|
||||
manager.requestSerializer = [AFJSONRequestSerializer serializer];
|
||||
} else if ([serializerName isEqualToString:@"utf8"]) {
|
||||
manager.requestSerializer = [TextRequestSerializer serializer];
|
||||
} else if ([serializerName isEqualToString:@"raw"]) {
|
||||
manager.requestSerializer = [BinaryRequestSerializer serializer];
|
||||
} else {
|
||||
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setupAuthChallengeBlock:(AFHTTPSessionManager*)manager {
|
||||
[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(
|
||||
NSURLSession * _Nonnull session,
|
||||
NSURLAuthenticationChallenge * _Nonnull challenge,
|
||||
NSURLCredential * _Nullable __autoreleasing * _Nullable credential
|
||||
) {
|
||||
if ([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust]) {
|
||||
*credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
|
||||
|
||||
if (![self->securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
|
||||
return NSURLSessionAuthChallengeRejectProtectionSpace;
|
||||
}
|
||||
|
||||
if (credential) {
|
||||
return NSURLSessionAuthChallengeUseCredential;
|
||||
}
|
||||
}
|
||||
|
||||
if ([challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodClientCertificate] && self->x509Credential) {
|
||||
*credential = self->x509Credential;
|
||||
return NSURLSessionAuthChallengeUseCredential;
|
||||
}
|
||||
|
||||
return NSURLSessionAuthChallengePerformDefaultHandling;
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)setRequestHeaders:(NSDictionary*)headers forManager:(AFHTTPSessionManager*)manager {
|
||||
[headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
||||
[manager.requestSerializer setValue:obj forHTTPHeaderField:key];
|
||||
@@ -59,7 +102,7 @@
|
||||
}
|
||||
|
||||
- (void)setResponseSerializer:(NSString*)responseType forManager:(AFHTTPSessionManager*)manager {
|
||||
if ([responseType isEqualToString: @"text"]) {
|
||||
if ([responseType isEqualToString: @"text"] || [responseType isEqualToString: @"json"]) {
|
||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||
} else {
|
||||
manager.responseSerializer = [BinaryResponseSerializer serializer];
|
||||
@@ -80,14 +123,21 @@
|
||||
}
|
||||
|
||||
- (void)handleError:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response error:(NSError*)error {
|
||||
bool aborted = error.code == NSURLErrorCancelled;
|
||||
if(aborted){
|
||||
[dictionary setObject:[NSNumber numberWithInt:-8] forKey:@"status"];
|
||||
[dictionary setObject:@"Request was aborted" forKey:@"error"];
|
||||
}
|
||||
if (response != nil) {
|
||||
[dictionary setValue:response.URL.absoluteString forKey:@"url"];
|
||||
[dictionary setObject:[NSNumber numberWithInt:(int)response.statusCode] forKey:@"status"];
|
||||
[dictionary setObject:[self copyHeaderFields:response.allHeaderFields] forKey:@"headers"];
|
||||
if (error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey]) {
|
||||
[dictionary setObject:error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] forKey:@"error"];
|
||||
if(!aborted){
|
||||
[dictionary setObject:[NSNumber numberWithInt:(int)response.statusCode] forKey:@"status"];
|
||||
if (error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey]) {
|
||||
[dictionary setObject:error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] forKey:@"error"];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if(!aborted) {
|
||||
[dictionary setObject:[self getStatusCode:error] forKey:@"status"];
|
||||
[dictionary setObject:[error localizedDescription] forKey:@"error"];
|
||||
}
|
||||
@@ -144,15 +194,16 @@
|
||||
|
||||
- (void)executeRequestWithoutData:(CDVInvokedUrlCommand*)command withMethod:(NSString*) method {
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.securityPolicy = securityPolicy;
|
||||
|
||||
NSString *url = [command.arguments objectAtIndex:0];
|
||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue];
|
||||
bool followRedirect = [[command.arguments objectAtIndex:3] boolValue];
|
||||
NSString *responseType = [command.arguments objectAtIndex:4];
|
||||
NSNumber *reqId = [command.arguments objectAtIndex:5];
|
||||
|
||||
[self setRequestSerializer: @"default" forManager: manager];
|
||||
[self setupAuthChallengeBlock: manager];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||
[self setRedirect:followRedirect forManager:manager];
|
||||
@@ -163,30 +214,37 @@
|
||||
|
||||
@try {
|
||||
void (^onSuccess)(NSURLSessionTask *, id) = ^(NSURLSessionTask *task, id responseObject) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
|
||||
|
||||
// no 'body' for HEAD request, omitting 'data'
|
||||
if ([method isEqualToString:@"HEAD"]) {
|
||||
[self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil];
|
||||
} else {
|
||||
[self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject];
|
||||
}
|
||||
|
||||
|
||||
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
[manager invalidateSessionCancelingTasks:YES];
|
||||
};
|
||||
|
||||
|
||||
void (^onFailure)(NSURLSessionTask *, NSError *) = ^(NSURLSessionTask *task, NSError *error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
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];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
[manager invalidateSessionCancelingTasks:YES];
|
||||
};
|
||||
|
||||
[manager downloadTaskWithHTTPMethod:method URLString:url parameters:nil progress:nil success:onSuccess failure:onFailure];
|
||||
|
||||
NSURLSessionDataTask *task = [manager downloadTaskWithHTTPMethod:method URLString:url parameters:nil progress:nil success:onSuccess failure:onFailure];
|
||||
[self addRequest:reqId forTask:task];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
@@ -196,7 +254,6 @@
|
||||
|
||||
- (void)executeRequestWithData:(CDVInvokedUrlCommand*)command withMethod:(NSString*)method {
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.securityPolicy = securityPolicy;
|
||||
|
||||
NSString *url = [command.arguments objectAtIndex:0];
|
||||
NSDictionary *data = [command.arguments objectAtIndex:1];
|
||||
@@ -205,8 +262,10 @@
|
||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||
NSNumber *reqId = [command.arguments objectAtIndex:7];
|
||||
|
||||
[self setRequestSerializer: serializerName forManager: manager];
|
||||
[self setupAuthChallengeBlock: manager];
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||
[self setRedirect:followRedirect forManager:manager];
|
||||
@@ -214,30 +273,32 @@
|
||||
|
||||
CordovaHttpPlugin* __weak weakSelf = self;
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||
|
||||
|
||||
@try {
|
||||
void (^constructBody)(id<AFMultipartFormData>) = ^(id<AFMultipartFormData> formData) {
|
||||
NSArray *buffers = [data mutableArrayValueForKey:@"buffers"];
|
||||
NSArray *fileNames = [data mutableArrayValueForKey:@"fileNames"];
|
||||
NSArray *names = [data mutableArrayValueForKey:@"names"];
|
||||
NSArray *types = [data mutableArrayValueForKey:@"types"];
|
||||
|
||||
|
||||
NSError *error;
|
||||
|
||||
|
||||
for (int i = 0; i < [buffers count]; ++i) {
|
||||
NSData *decodedBuffer = [[NSData alloc] initWithBase64EncodedString:[buffers objectAtIndex:i] options:0];
|
||||
NSString *fileName = [fileNames objectAtIndex:i];
|
||||
NSString *partName = [names objectAtIndex:i];
|
||||
NSString *partType = [types objectAtIndex:i];
|
||||
|
||||
|
||||
if (![fileName isEqual:[NSNull null]]) {
|
||||
[formData appendPartWithFileData:decodedBuffer name:partName fileName:fileName mimeType:partType];
|
||||
} else {
|
||||
[formData appendPartWithFormData:decodedBuffer name:[names objectAtIndex:i]];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[dictionary setObject:[NSNumber numberWithInt:400] forKey:@"status"];
|
||||
[dictionary setObject:@"Could not add part to multipart request body." forKey:@"error"];
|
||||
@@ -247,30 +308,38 @@
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void (^onSuccess)(NSURLSessionTask *, id) = ^(NSURLSessionTask *task, id responseObject) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
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];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
[manager invalidateSessionCancelingTasks:YES];
|
||||
};
|
||||
|
||||
|
||||
void (^onFailure)(NSURLSessionTask *, NSError *) = ^(NSURLSessionTask *task, NSError *error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
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];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
[manager invalidateSessionCancelingTasks:YES];
|
||||
};
|
||||
|
||||
|
||||
NSURLSessionDataTask *task;
|
||||
if ([serializerName isEqualToString:@"multipart"]) {
|
||||
[manager uploadTaskWithHTTPMethod:method URLString:url parameters:nil constructingBodyWithBlock:constructBody progress:nil success:onSuccess failure:onFailure];
|
||||
task = [manager uploadTaskWithHTTPMethod:method URLString:url parameters:nil constructingBodyWithBlock:constructBody progress:nil success:onSuccess failure:onFailure];
|
||||
} else {
|
||||
[manager uploadTaskWithHTTPMethod:method URLString:url parameters:data progress:nil success:onSuccess failure:onFailure];
|
||||
task = [manager uploadTaskWithHTTPMethod:method URLString:url parameters:data progress:nil success:onSuccess failure:onFailure];
|
||||
}
|
||||
[self addRequest:reqId forTask:task];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
@@ -299,16 +368,49 @@
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
- (void)get:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"GET"];
|
||||
}
|
||||
- (void)setClientAuthMode:(CDVInvokedUrlCommand*)command {
|
||||
CDVPluginResult* pluginResult;
|
||||
NSString *mode = [command.arguments objectAtIndex:0];
|
||||
|
||||
- (void)delete:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"DELETE"];
|
||||
}
|
||||
if ([mode isEqualToString:@"none"]) {
|
||||
x509Credential = nil;
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
}
|
||||
|
||||
- (void)head:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"HEAD"];
|
||||
if ([mode isEqualToString:@"systemstore"]) {
|
||||
NSString *alias = [command.arguments objectAtIndex:1];
|
||||
|
||||
// TODO
|
||||
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"mode 'systemstore' is not supported on iOS"];
|
||||
}
|
||||
|
||||
if ([mode isEqualToString:@"buffer"]) {
|
||||
CFDataRef container = (__bridge CFDataRef) [command.arguments objectAtIndex:2];
|
||||
CFStringRef password = (__bridge CFStringRef) [command.arguments objectAtIndex:3];
|
||||
|
||||
const void *keys[] = { kSecImportExportPassphrase };
|
||||
const void *values[] = { password };
|
||||
|
||||
CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
|
||||
CFArrayRef items;
|
||||
OSStatus securityError = SecPKCS12Import(container, options, &items);
|
||||
CFRelease(options);
|
||||
|
||||
if (securityError != noErr) {
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
|
||||
} else {
|
||||
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
|
||||
SecIdentityRef identity = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
|
||||
|
||||
self->x509Credential = [NSURLCredential credentialWithIdentity:identity certificates: nil persistence:NSURLCredentialPersistenceForSession];
|
||||
CFRelease(items);
|
||||
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
||||
}
|
||||
}
|
||||
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
- (void)post:(CDVInvokedUrlCommand*)command {
|
||||
@@ -323,9 +425,24 @@
|
||||
[self executeRequestWithData: command withMethod:@"PATCH"];
|
||||
}
|
||||
|
||||
- (void)get:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"GET"];
|
||||
}
|
||||
|
||||
- (void)delete:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"DELETE"];
|
||||
}
|
||||
|
||||
- (void)head:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"HEAD"];
|
||||
}
|
||||
|
||||
- (void)options:(CDVInvokedUrlCommand*)command {
|
||||
[self executeRequestWithoutData: command withMethod:@"OPTIONS"];
|
||||
}
|
||||
|
||||
- (void)uploadFiles:(CDVInvokedUrlCommand*)command {
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.securityPolicy = securityPolicy;
|
||||
|
||||
NSString *url = [command.arguments objectAtIndex:0];
|
||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||
@@ -334,8 +451,10 @@
|
||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||
NSNumber *reqId = [command.arguments objectAtIndex:7];
|
||||
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
[self setupAuthChallengeBlock: manager];
|
||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||
[self setRedirect:followRedirect forManager:manager];
|
||||
[self setResponseSerializer:responseType forManager:manager];
|
||||
@@ -344,7 +463,7 @@
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||
|
||||
@try {
|
||||
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
||||
NSURLSessionDataTask *task = [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
|
||||
NSError *error;
|
||||
for (int i = 0; i < [filePaths count]; i++) {
|
||||
NSString *filePath = (NSString *) [filePaths objectAtIndex:i];
|
||||
@@ -353,6 +472,8 @@
|
||||
[formData appendPartWithFileURL:fileURL name:uploadName error:&error];
|
||||
}
|
||||
if (error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"];
|
||||
[dictionary setObject:@"Could not add file to post body." forKey:@"error"];
|
||||
@@ -362,6 +483,8 @@
|
||||
return;
|
||||
}
|
||||
} progress:nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject];
|
||||
|
||||
@@ -369,6 +492,8 @@
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
|
||||
|
||||
@@ -376,6 +501,7 @@
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
}];
|
||||
[self addRequest:reqId forTask:task];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
@@ -385,7 +511,6 @@
|
||||
|
||||
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.securityPolicy = securityPolicy;
|
||||
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||
|
||||
NSString *url = [command.arguments objectAtIndex:0];
|
||||
@@ -393,8 +518,10 @@
|
||||
NSString *filePath = [command.arguments objectAtIndex: 2];
|
||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:3] doubleValue];
|
||||
bool followRedirect = [[command.arguments objectAtIndex:4] boolValue];
|
||||
NSNumber *reqId = [command.arguments objectAtIndex:5];
|
||||
|
||||
[self setRequestHeaders: headers forManager: manager];
|
||||
[self setupAuthChallengeBlock: manager];
|
||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||
[self setRedirect:followRedirect forManager:manager];
|
||||
|
||||
@@ -406,7 +533,8 @@
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||
|
||||
@try {
|
||||
[manager GET:url parameters:nil progress: nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
NSURLSessionDataTask *task = [manager GET:url parameters:nil progress: nil success:^(NSURLSessionTask *task, id responseObject) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
/*
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
@@ -467,6 +595,7 @@
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
} failure:^(NSURLSessionTask *task, NSError *error) {
|
||||
[weakSelf removeRequest:reqId];
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error];
|
||||
[dictionary setObject:@"There was an error downloading the file" forKey:@"error"];
|
||||
@@ -475,6 +604,7 @@
|
||||
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
}];
|
||||
[self addRequest:reqId forTask:task];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
[[SDNetworkActivityIndicator sharedActivityIndicator] stopActivity];
|
||||
@@ -482,4 +612,32 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)abort:(CDVInvokedUrlCommand*)command {
|
||||
|
||||
NSNumber *reqId = [command.arguments objectAtIndex:0];
|
||||
|
||||
CDVPluginResult *pluginResult;
|
||||
bool removed = false;
|
||||
NSURLSessionDataTask *task = [reqDict objectForKey:reqId];
|
||||
if(task){
|
||||
@try{
|
||||
[task cancel];
|
||||
removed = true;
|
||||
} @catch (NSException *exception) {
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[dictionary setValue:exception.userInfo forKey:@"error"];
|
||||
[dictionary setObject:[NSNumber numberWithInt:-1] forKey:@"status"];
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
|
||||
}
|
||||
}
|
||||
|
||||
if(!pluginResult){
|
||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
||||
[dictionary setObject:[NSNumber numberWithBool:removed] forKey:@"aborted"];
|
||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
|
||||
}
|
||||
|
||||
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -16,15 +16,16 @@
|
||||
<allow-intent href="mailto:*" />
|
||||
<allow-intent href="geo:*" />
|
||||
<platform name="android">
|
||||
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application android:networkSecurityConfig="@xml/network_security_config" />
|
||||
</edit-config>
|
||||
<resource-file src="network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
|
||||
<allow-intent href="market:*" />
|
||||
</platform>
|
||||
<platform name="ios">
|
||||
<allow-intent href="itms:*" />
|
||||
<allow-intent href="itms-apps:*" />
|
||||
</platform>
|
||||
<engine name="android" spec="7.1.0" />
|
||||
<engine name="browser" spec="5.0.0" />
|
||||
<engine name="ios" spec="5.0.1" />
|
||||
<plugin name="cordova-plugin-file" spec="6.0.1" />
|
||||
<preference name="AndroidPersistentFileLocation" value="Internal" />
|
||||
<preference name="AndroidBlacklistSecureSocketProtocols" value="SSLv3,TLSv1" />
|
||||
</widget>
|
||||
|
||||
9
test/e2e-app-template/network_security_config.xml
Normal file
9
test/e2e-app-template/network_security_config.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<network-security-config>
|
||||
<domain-config cleartextTrafficPermitted="true">
|
||||
<domain includeSubdomains="true">localhost</domain>
|
||||
<domain includeSubdomains="true">httpbin.org</domain>
|
||||
<domain includeSubdomains="true">httpbingo.org</domain>
|
||||
<domain includeSubdomains="true">www.columbia.edu</domain>
|
||||
</domain-config>
|
||||
</network-security-config>
|
||||
@@ -1,26 +1,35 @@
|
||||
{
|
||||
"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": "7.1.0",
|
||||
"cordova-browser": "5.0.0",
|
||||
"cordova-ios": "5.0.1"
|
||||
},
|
||||
"cordova": {
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
]
|
||||
},
|
||||
"devDependencies": {}
|
||||
}
|
||||
"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": "10.0.0",
|
||||
"cordova-android": "9.0.0",
|
||||
"cordova-browser": "6.0.0",
|
||||
"cordova-ios": "6.2.0",
|
||||
"cordova-plugin-device": "2.0.3",
|
||||
"cordova-plugin-wkwebview-file-xhr": "3.0.0"
|
||||
},
|
||||
"cordova": {
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios",
|
||||
"browser"
|
||||
],
|
||||
"plugins": {
|
||||
"cordova-plugin-device": {},
|
||||
"cordova-plugin-wkwebview-file-xhr": {}
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"cordova-plugin-device": "2.0.3"
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,12 @@ const app = {
|
||||
|
||||
var onlyFlaggedTests = [];
|
||||
var enabledTests = [];
|
||||
|
||||
|
||||
tests.forEach(function (test) {
|
||||
if (test.only) {
|
||||
onlyFlaggedTests.push(test);
|
||||
}
|
||||
|
||||
|
||||
if (!test.disabled) {
|
||||
enabledTests.push(test);
|
||||
}
|
||||
@@ -50,6 +50,16 @@ const app = {
|
||||
};
|
||||
},
|
||||
|
||||
skip: function (content) {
|
||||
document.getElementById('statusInput').value = 'finished';
|
||||
app.printResult('result - skipped', content);
|
||||
|
||||
app.lastResult = {
|
||||
type: 'skipped',
|
||||
data: content
|
||||
};
|
||||
},
|
||||
|
||||
throw: function (error) {
|
||||
document.getElementById('statusInput').value = 'finished';
|
||||
app.printResult('result - throwed', error.message);
|
||||
@@ -126,7 +136,7 @@ const app = {
|
||||
|
||||
const execTest = function () {
|
||||
try {
|
||||
testDefinition.func(app.resolve, app.reject);
|
||||
testDefinition.func(app.resolve, app.reject, app.skip);
|
||||
} catch (error) {
|
||||
app.throw(error);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ const hooks = {
|
||||
onBeforeEachTest: function (resolve, reject) {
|
||||
cordova.plugin.http.clearCookies();
|
||||
|
||||
helpers.enableFollowingRedirect(function() {
|
||||
// server trust mode is not supported on brpwser platform
|
||||
helpers.enableFollowingRedirect(function () {
|
||||
// server trust mode is not supported on browser platform
|
||||
if (cordova.platformId === 'browser') {
|
||||
return resolve();
|
||||
}
|
||||
@@ -23,7 +23,7 @@ const helpers = {
|
||||
setPinnedServerTrustMode: function (resolve, reject) { cordova.plugin.http.setServerTrustMode('pinned', resolve, reject); },
|
||||
setNoneClientAuthMode: function (resolve, reject) { cordova.plugin.http.setClientAuthMode('none', resolve, reject); },
|
||||
setBufferClientAuthMode: function (resolve, reject) {
|
||||
helpers.getWithXhr(function(pkcs) {
|
||||
helpers.getWithXhr(function (pkcs) {
|
||||
cordova.plugin.http.setClientAuthMode('buffer', {
|
||||
rawPkcs: pkcs,
|
||||
pkcsPassword: 'badssl.com'
|
||||
@@ -34,8 +34,9 @@ const helpers = {
|
||||
setUtf8StringSerializer: function (resolve) { resolve(cordova.plugin.http.setDataSerializer('utf8')); },
|
||||
setUrlEncodedSerializer: function (resolve) { resolve(cordova.plugin.http.setDataSerializer('urlencoded')); },
|
||||
setMultipartSerializer: function (resolve) { resolve(cordova.plugin.http.setDataSerializer('multipart')); },
|
||||
setRawSerializer: function (resolve) { resolve(cordova.plugin.http.setDataSerializer('raw')); },
|
||||
disableFollowingRedirect: function (resolve) { resolve(cordova.plugin.http.setFollowRedirect(false)); },
|
||||
enableFollowingRedirect: function(resolve) { resolve(cordova.plugin.http.setFollowRedirect(true)); },
|
||||
enableFollowingRedirect: function (resolve) { resolve(cordova.plugin.http.setFollowRedirect(true)); },
|
||||
getWithXhr: function (done, url, type) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
@@ -70,7 +71,7 @@ const helpers = {
|
||||
var byteArray = new Uint8Array(buffer);
|
||||
|
||||
for (var i = 0; i < byteArray.length; i++) {
|
||||
hash = ((hash << 5) - hash) + byteArray[i];
|
||||
hash = ((hash << 5) - hash) + byteArray[i];
|
||||
hash |= 0; // Convert to 32bit integer
|
||||
}
|
||||
|
||||
@@ -82,10 +83,34 @@ const helpers = {
|
||||
}
|
||||
|
||||
result.type.should.be.equal(expected);
|
||||
},
|
||||
isAbortSupported: function () {
|
||||
if (window.cordova && window.cordova.platformId === 'android') {
|
||||
var version = device.version; // NOTE will throw error if cordova is present without cordova-plugin-device
|
||||
var major = parseInt(/^(\d+)(\.|$)/.exec(version)[1], 10);
|
||||
return isFinite(major) && major >= 6;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
getAbortDelay: function () { return 0; },
|
||||
getDemoArrayBuffer: function(size) {
|
||||
var demoText = [73, 39, 109, 32, 97, 32, 100, 117, 109, 109, 121, 32, 102, 105, 108, 101, 33, 32, 73, 39, 109, 32, 117, 115, 101, 100, 32, 102, 111, 114, 32, 116, 101, 115, 116, 105, 110, 103, 32, 112, 117, 114, 112, 111, 115, 101, 115, 46, 32, 82, 97, 110, 100, 111, 109, 32, 100, 97, 116, 97, 32, 105, 115, 32, 102, 111, 108, 108, 111, 119, 105, 110, 103, 58, 32];
|
||||
var buffer = new ArrayBuffer(size);
|
||||
var view = new Uint8Array(buffer);
|
||||
|
||||
for (var i = 0; i < size; ++i) {
|
||||
view[i] = demoText[i];
|
||||
}
|
||||
|
||||
return buffer;
|
||||
},
|
||||
isTlsBlacklistSupported: function () {
|
||||
return window.cordova && window.cordova.platformId === 'android';
|
||||
}
|
||||
};
|
||||
|
||||
const messageFactory = {
|
||||
handshakeFailed: function() { return 'TLS connection could not be established: javax.net.ssl.SSLHandshakeException: Handshake failed' },
|
||||
sslTrustAnchor: function () { return 'TLS connection could not be established: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.' },
|
||||
invalidCertificate: function (domain) { return 'The certificate for this server is invalid. You might be connecting to a server that is pretending to be “' + domain + '” which could put your confidential information at risk.' }
|
||||
}
|
||||
@@ -290,7 +315,7 @@ const tests = [
|
||||
{
|
||||
description: 'should resolve correct URL after redirect (GET) #33',
|
||||
expected: 'resolved: {"status": 200, url: "http://httpbin.org/anything", ...',
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('http://httpbin.org/redirect-to?url=http://httpbin.org/anything', {}, {}, resolve, reject); },
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('http://httpbingo.org/redirect-to?url=http://httpbin.org/anything', {}, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
result.data.url.should.be.equal('http://httpbin.org/anything');
|
||||
@@ -299,8 +324,8 @@ const tests = [
|
||||
{
|
||||
description: 'should not follow 302 redirect when following redirects is disabled',
|
||||
expected: 'rejected: {"status": 302, ...',
|
||||
before: function(resolve, reject) { cordova.plugin.http.disableRedirect(true, resolve, reject)},
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('http://httpbin.org/redirect-to?url=http://httpbin.org/anything', {}, {}, resolve, reject); },
|
||||
before: function (resolve, reject) { cordova.plugin.http.setFollowRedirect(false); resolve(); },
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('http://httpbingo.org/redirect-to?url=http://httpbin.org/anything', {}, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('rejected');
|
||||
result.data.status.should.be.equal(302);
|
||||
@@ -373,7 +398,7 @@ const tests = [
|
||||
var targetUrl = 'http://httpbin.org/post';
|
||||
|
||||
helpers.writeToFile(function () {
|
||||
helpers.writeToFile(function() {
|
||||
helpers.writeToFile(function () {
|
||||
cordova.plugin.http.uploadFile(targetUrl, {}, {}, [sourcePath, sourcePath2], [fileName, fileName2], resolve, reject);
|
||||
}, fileName2, fileContent2);
|
||||
}, fileName, fileContent);
|
||||
@@ -463,7 +488,7 @@ const tests = [
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should not send any cookies after running "clearCookies" (GET) #59',
|
||||
description: 'should not send programmatically set 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');
|
||||
@@ -786,7 +811,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should decode error body even if response type is "arraybuffer"',
|
||||
expected: 'rejected: {"status": 418, ...',
|
||||
expected: 'rejected: {"status":418, ...',
|
||||
func: function (resolve, reject) {
|
||||
var url = 'https://httpbin.org/status/418';
|
||||
var options = { method: 'get', responseType: 'arraybuffer' };
|
||||
@@ -800,7 +825,7 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should serialize FormData instance correctly when it contains string value',
|
||||
expected: 'resolved: {"status": 200, ...',
|
||||
expected: 'resolved: {"status":200, ...',
|
||||
before: helpers.setMultipartSerializer,
|
||||
func: function (resolve, reject) {
|
||||
var ponyfills = cordova.plugin.http.ponyfills;
|
||||
@@ -819,11 +844,11 @@ const tests = [
|
||||
},
|
||||
{
|
||||
description: 'should serialize FormData instance correctly when it contains blob value',
|
||||
expected: 'resolved: {"status": 200, ...',
|
||||
expected: 'resolved: {"status":200, ...',
|
||||
before: helpers.setMultipartSerializer,
|
||||
func: function (resolve, reject) {
|
||||
var ponyfills = cordova.plugin.http.ponyfills;
|
||||
helpers.getWithXhr(function(blob) {
|
||||
helpers.getWithXhr(function (blob) {
|
||||
var formData = new ponyfills.FormData();
|
||||
formData.append('CordovaLogo', blob);
|
||||
|
||||
@@ -843,19 +868,301 @@ const tests = [
|
||||
const b64Logo = rawLogo.toString('base64');
|
||||
JSON.parse(result.data.data).files.CordovaLogo.should.be.equal('data:image/png;base64,' + b64Logo);
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should send raw byte array correctly (POST) #291',
|
||||
expected: 'resolved: {"status":200,"data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUg ...',
|
||||
before: helpers.setRawSerializer,
|
||||
func: function (resolve, reject) {
|
||||
helpers.getWithXhr(function (buffer) {
|
||||
cordova.plugin.http.post('http://httpbin.org/anything', buffer, {}, resolve, reject);
|
||||
}, './res/cordova_logo.png', 'arraybuffer');
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'resolved');
|
||||
result.data.status.should.be.equal(200);
|
||||
|
||||
// TODO: not ready yet
|
||||
// {
|
||||
// description: 'should authenticate correctly when client cert auth is configured with a PKCS12 container',
|
||||
// expected: 'resolved: {"status": 200, ...',
|
||||
// before: helpers.setBufferClientAuthMode,
|
||||
// func: function (resolve, reject) { cordova.plugin.http.get('https://client.badssl.com/', {}, {}, resolve, reject); },
|
||||
// validationFunc: function (driver, result) {
|
||||
// result.type.should.be.equal('resolved');
|
||||
// result.data.data.should.include('TLS handshake');
|
||||
// }
|
||||
// }
|
||||
// httpbin.org encodes posted binaries in base64 and echoes them back
|
||||
// therefore we need to check for base64 string with mime type prefix
|
||||
const fs = require('fs');
|
||||
const rawLogo = fs.readFileSync('./test/e2e-app-template/www/res/cordova_logo.png');
|
||||
const b64Logo = rawLogo.toString('base64');
|
||||
const parsed = JSON.parse(result.data.data);
|
||||
|
||||
parsed.headers['Content-Type'].should.be.equal('application/octet-stream');
|
||||
parsed.data.should.be.equal('data:application/octet-stream;base64,' + b64Logo);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should perform an OPTIONS request correctly #155',
|
||||
expected: 'resolved: {"status":200,"headers":{"allow":"GET, PUT, DELETE, HEAD, PATCH, TRACE, POST, OPTIONS" ...',
|
||||
func: function (resolve, reject) { cordova.plugin.http.options('http://httpbin.org/anything', {}, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
result.data.status.should.be.equal(200);
|
||||
|
||||
result.data.headers.should.be.an('object');
|
||||
result.data.headers.allow.should.include('GET');
|
||||
result.data.headers.allow.should.include('PUT');
|
||||
result.data.headers.allow.should.include('DELETE');
|
||||
result.data.headers.allow.should.include('HEAD');
|
||||
result.data.headers.allow.should.include('PATCH');
|
||||
result.data.headers.allow.should.include('TRACE');
|
||||
result.data.headers.allow.should.include('POST');
|
||||
result.data.headers.allow.should.include('OPTIONS');
|
||||
|
||||
result.data.headers['access-control-allow-origin'].should.be.equal('*');
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should allow empty response body even though responseType is set #334',
|
||||
expected: 'resolved: {"status":200, ...',
|
||||
func: function (resolve, reject) {
|
||||
var url = 'https://httpbin.org/status/200';
|
||||
var options = { method: 'get', responseType: 'json' };
|
||||
cordova.plugin.http.sendRequest(url, options, resolve, reject);
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
should.equal(true, result.data.data === null || result.data.data === undefined);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should decode JSON data correctly when response type is "json" #301',
|
||||
expected: 'resolved: {"status":200,"data":{"slideshow": ... ',
|
||||
func: function (resolve, reject) {
|
||||
var url = 'https://httpbin.org/json';
|
||||
var options = { method: 'get', responseType: 'json' };
|
||||
cordova.plugin.http.sendRequest(url, options, resolve, reject);
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
result.data.status.should.be.equal(200);
|
||||
result.data.data.should.be.an('object');
|
||||
result.data.data.slideshow.should.be.eql({
|
||||
author: 'Yours Truly',
|
||||
date: 'date of publication',
|
||||
slides: [
|
||||
{
|
||||
title: 'Wake up to WonderWidgets!',
|
||||
type: 'all'
|
||||
},
|
||||
{
|
||||
items: [
|
||||
'Why <em>WonderWidgets</em> are great',
|
||||
'Who <em>buys</em> WonderWidgets'
|
||||
],
|
||||
title: 'Overview',
|
||||
type: 'all'
|
||||
}
|
||||
],
|
||||
title: 'Sample Slide Show'
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should serialize FormData instance correctly when it contains null or undefined value #300',
|
||||
expected: 'resolved: {"status":200, ...',
|
||||
before: helpers.setMultipartSerializer,
|
||||
func: function (resolve, reject) {
|
||||
var ponyfills = cordova.plugin.http.ponyfills;
|
||||
var formData = new ponyfills.FormData();
|
||||
formData.append('myNullValue', null);
|
||||
formData.append('myUndefinedValue', undefined);
|
||||
|
||||
var url = 'https://httpbin.org/anything';
|
||||
var options = { method: 'post', data: formData };
|
||||
cordova.plugin.http.sendRequest(url, options, resolve, reject);
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'resolved');
|
||||
result.data.status.should.be.equal(200);
|
||||
JSON.parse(result.data.data).form.should.be.eql({
|
||||
myNullValue: 'null',
|
||||
myUndefinedValue: 'undefined'
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should authenticate correctly when client cert auth is configured with a PKCS12 container',
|
||||
expected: 'resolved: {"status": 200, ...',
|
||||
before: helpers.setBufferClientAuthMode,
|
||||
func: function (resolve, reject) { cordova.plugin.http.get('https://client.badssl.com/', {}, {}, resolve, reject); },
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
result.data.data.should.include('TLS handshake');
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should not send any cookies after running "clearCookies" (GET) #248',
|
||||
expected: 'resolved: {"status": 200, "data": "{\"cookies\":{}} ...',
|
||||
before: helpers.disableFollowingRedirect,
|
||||
func: function (resolve, reject) {
|
||||
cordova.plugin.http.get('https://httpbin.org/cookies/set?myCookieKey=myCookieValue', {}, {}, function () {
|
||||
cordova.plugin.http.clearCookies();
|
||||
cordova.plugin.http.get('https://httpbin.org/cookies', {}, {}, resolve, reject);
|
||||
}, function () {
|
||||
cordova.plugin.http.clearCookies();
|
||||
cordova.plugin.http.get('https://httpbin.org/cookies', {}, {}, resolve, reject);
|
||||
});
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('resolved');
|
||||
result.data.status.should.be.equal(200);
|
||||
JSON.parse(result.data.data).cookies.should.be.eql({});
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should be able to abort (POST)',
|
||||
expected: 'rejected: {"status":-8, "error": "Request ...}',
|
||||
before: helpers.setRawSerializer,
|
||||
func: function (resolve, reject, skip) {
|
||||
if (!helpers.isAbortSupported()) {
|
||||
return skip();
|
||||
}
|
||||
|
||||
var targetUrl = 'http://httpbin.org/post';
|
||||
var fileContent = helpers.getDemoArrayBuffer(10000);
|
||||
var reqId = cordova.plugin.http.post(targetUrl, fileContent, {}, resolve, reject);
|
||||
|
||||
setTimeout(function () {
|
||||
cordova.plugin.http.abort(reqId);
|
||||
}, helpers.getAbortDelay());
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'rejected');
|
||||
result.data.status.should.be.equal(-8);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should be able to abort (GET)',
|
||||
expected: 'rejected: {"status":-8, "error": "Request ...}',
|
||||
func: function (resolve, reject, skip) {
|
||||
if (!helpers.isAbortSupported()) {
|
||||
return skip();
|
||||
}
|
||||
var url = 'https://httpbin.org/drip?duration=2&numbytes=10&code=200';
|
||||
var options = { method: 'get', responseType: 'blob' };
|
||||
var success = function (response) {
|
||||
resolve({
|
||||
isBlob: response.data.constructor === Blob,
|
||||
type: response.data.type,
|
||||
byteLength: response.data.size
|
||||
});
|
||||
};
|
||||
|
||||
var reqId = cordova.plugin.http.sendRequest(url, options, success, reject);
|
||||
setTimeout(function () {
|
||||
cordova.plugin.http.abort(reqId);
|
||||
}, helpers.getAbortDelay());
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'rejected');
|
||||
result.data.status.should.be.equal(-8);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should be able to abort downloading a file',
|
||||
expected: 'rejected: {"status":-8, "error": "Request ...}',
|
||||
func: function (resolve, reject, skip) {
|
||||
if (!helpers.isAbortSupported()) {
|
||||
return skip();
|
||||
}
|
||||
var sourceUrl = 'http://httpbin.org/xml';
|
||||
var targetPath = cordova.file.cacheDirectory + 'test.xml';
|
||||
|
||||
var reqId = 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);
|
||||
|
||||
setTimeout(function () {
|
||||
cordova.plugin.http.abort(reqId);
|
||||
}, helpers.getAbortDelay());
|
||||
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'rejected');
|
||||
result.data.status.should.be.equal(-8);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should be able to abort uploading a file',
|
||||
expected: 'rejected: {"status":-8, "error": "Request ...}',
|
||||
func: function (resolve, reject, skip) {
|
||||
if (!helpers.isAbortSupported()) {
|
||||
return skip();
|
||||
}
|
||||
|
||||
|
||||
var fileName = 'test-file.txt';
|
||||
var fileContent = helpers.getDemoArrayBuffer(10000);
|
||||
var sourcePath = cordova.file.cacheDirectory + fileName;
|
||||
var targetUrl = 'http://httpbin.org/post';
|
||||
|
||||
helpers.writeToFile(function () {
|
||||
|
||||
var reqId = cordova.plugin.http.uploadFile(targetUrl, {}, {}, sourcePath, fileName, resolve, reject);
|
||||
|
||||
setTimeout(function () {
|
||||
cordova.plugin.http.abort(reqId);
|
||||
}, helpers.getAbortDelay());
|
||||
|
||||
}, fileName, fileContent);
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
helpers.checkResult(result, 'rejected');
|
||||
result.data.status.should.be.equal(-8);
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should not send malformed request when FormData is empty #372',
|
||||
expected: 'resolved: {"status":200, ...',
|
||||
before: helpers.setMultipartSerializer,
|
||||
func: function (resolve, reject) {
|
||||
var ponyfills = cordova.plugin.http.ponyfills;
|
||||
var formData = new ponyfills.FormData();
|
||||
|
||||
var url = 'http://httpbin.org/anything';
|
||||
var options = { method: 'post', data: formData };
|
||||
cordova.plugin.http.sendRequest(url, options, resolve, reject);
|
||||
},
|
||||
validationFunc: function (driver, result, targetInfo) {
|
||||
helpers.checkResult(result, 'resolved');
|
||||
|
||||
var parsed = JSON.parse(result.data.data);
|
||||
|
||||
if (targetInfo.isAndroid) {
|
||||
// boundary should be sent correctly on Android
|
||||
parsed.headers['Content-Type'].should.be.equal('multipart/form-data; boundary=00content0boundary00');
|
||||
} else {
|
||||
// falling back to empty url encoded request on iOS
|
||||
parsed.headers['Content-Type'].should.be.equal('application/x-www-form-urlencoded');
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
description: 'should reject connecting to server with blacklisted SSL version #420',
|
||||
expected: 'rejected: {"status":-2, ...',
|
||||
func: function (resolve, reject, skip) {
|
||||
if (!helpers.isTlsBlacklistSupported()) {
|
||||
return skip();
|
||||
}
|
||||
|
||||
cordova.plugin.http.get('https://tls-v1-0.badssl.com:1010/', {}, {}, resolve, reject);
|
||||
},
|
||||
validationFunc: function (driver, result) {
|
||||
result.type.should.be.equal('rejected');
|
||||
result.data.should.be.eql({ status: -2, error: messageFactory.handshakeFailed() });
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
|
||||
@@ -14,7 +14,7 @@ const configs = {
|
||||
},
|
||||
localIosEmulator: {
|
||||
platformName: 'iOS',
|
||||
platformVersion: '13.2',
|
||||
platformVersion: '14.3',
|
||||
automationName: 'XCUITest',
|
||||
deviceName: 'iPhone 8',
|
||||
autoWebview: true,
|
||||
@@ -22,7 +22,7 @@ const configs = {
|
||||
},
|
||||
localAndroidEmulator: {
|
||||
platformName: 'Android',
|
||||
platformVersion: '5',
|
||||
platformVersion: '9',
|
||||
deviceName: 'Android Emulator',
|
||||
autoWebview: true,
|
||||
fullReset: true,
|
||||
@@ -32,30 +32,48 @@ const configs = {
|
||||
// testing on SauceLabs
|
||||
saucelabsIosDevice: {
|
||||
browserName: '',
|
||||
'appium-version': '1.9.1',
|
||||
'appium-version': '1.20.1',
|
||||
platformName: 'iOS',
|
||||
platformVersion: '10.3',
|
||||
platformVersion: '14.3',
|
||||
deviceName: 'iPhone 6',
|
||||
autoWebview: true,
|
||||
app: 'sauce-storage:HttpDemo.app.zip'
|
||||
},
|
||||
saucelabsIosEmulator: {
|
||||
browserName: '',
|
||||
'appium-version': '1.9.1',
|
||||
'appium-version': '1.20.1',
|
||||
platformName: 'iOS',
|
||||
platformVersion: '10.3',
|
||||
platformVersion: '14.3',
|
||||
deviceName: 'iPhone Simulator',
|
||||
autoWebview: true,
|
||||
app: 'sauce-storage:HttpDemo.app.zip'
|
||||
},
|
||||
saucelabsAndroidEmulator: {
|
||||
browserName: '',
|
||||
'appium-version': '1.9.1',
|
||||
'appium-version': '1.20.1',
|
||||
platformName: 'Android',
|
||||
platformVersion: '5.1',
|
||||
platformVersion: '8.0',
|
||||
deviceName: 'Android Emulator',
|
||||
autoWebview: true,
|
||||
app: 'sauce-storage:HttpDemo.apk'
|
||||
},
|
||||
|
||||
// testing on BrowserStack
|
||||
browserstackIosDevice: {
|
||||
device: 'iPhone 12',
|
||||
os_version: '14',
|
||||
project: 'HTTP Test App',
|
||||
autoWebview: true,
|
||||
app: 'HttpTestAppAndroid',
|
||||
'browserstack.networkLogs': true
|
||||
},
|
||||
browserstackAndroidDevice: {
|
||||
device: 'Google Nexus 6',
|
||||
os_version: '6.0',
|
||||
project: 'HTTP Test App',
|
||||
autoWebview: true,
|
||||
app: 'HttpTestAppAndroid',
|
||||
'browserstack.networkLogs': true
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
223
test/e2e-tooling/reporter.js
Normal file
223
test/e2e-tooling/reporter.js
Normal file
@@ -0,0 +1,223 @@
|
||||
'use strict';
|
||||
|
||||
const Mocha = require('mocha');
|
||||
const milliseconds = require('ms');
|
||||
const Base = Mocha.reporters.Base;
|
||||
const color = Base.color;
|
||||
|
||||
const {
|
||||
isString,
|
||||
stringify,
|
||||
inherits
|
||||
} = Mocha.utils;
|
||||
|
||||
const {
|
||||
EVENT_RUN_BEGIN,
|
||||
EVENT_RUN_END,
|
||||
EVENT_TEST_FAIL,
|
||||
EVENT_TEST_PASS,
|
||||
EVENT_TEST_PENDING,
|
||||
EVENT_SUITE_BEGIN,
|
||||
EVENT_SUITE_END
|
||||
} = Mocha.Runner.constants;
|
||||
|
||||
function Spec(runner, options) {
|
||||
Base.call(this, runner, options);
|
||||
|
||||
var self = this;
|
||||
var indents = 0;
|
||||
var n = 0;
|
||||
|
||||
function indent() {
|
||||
return Array(indents).join(' ');
|
||||
}
|
||||
|
||||
runner.on(EVENT_RUN_BEGIN, function() {
|
||||
Base.consoleLog();
|
||||
});
|
||||
|
||||
runner.on(EVENT_SUITE_BEGIN, function(suite) {
|
||||
++indents;
|
||||
Base.consoleLog(color('suite', '%s%s'), indent(), suite.title);
|
||||
});
|
||||
|
||||
runner.on(EVENT_SUITE_END, function() {
|
||||
--indents;
|
||||
if (indents === 1) {
|
||||
Base.consoleLog();
|
||||
}
|
||||
});
|
||||
|
||||
runner.on(EVENT_TEST_PENDING, function(test) {
|
||||
var fmt = indent() + color('pending', ' - %s');
|
||||
Base.consoleLog(fmt, test.title);
|
||||
});
|
||||
|
||||
runner.on(EVENT_TEST_PASS, function(test) {
|
||||
var fmt;
|
||||
if (test.speed === 'fast') {
|
||||
fmt =
|
||||
indent() +
|
||||
color('checkmark', ' ' + Base.symbols.ok) +
|
||||
color('pass', ' %s');
|
||||
Base.consoleLog(fmt, test.title);
|
||||
} else {
|
||||
fmt =
|
||||
indent() +
|
||||
color('checkmark', ' ' + Base.symbols.ok) +
|
||||
color('pass', ' %s') +
|
||||
color(test.speed, ' (%dms)');
|
||||
Base.consoleLog(fmt, test.title, test.duration);
|
||||
}
|
||||
});
|
||||
|
||||
runner.on(EVENT_TEST_FAIL, function(test) {
|
||||
Base.consoleLog(indent() + color('fail', ' %d) %s'), ++n, test.title);
|
||||
});
|
||||
|
||||
runner.once(EVENT_RUN_END, self.epilogue.bind(self));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inherit from `Base.prototype`.
|
||||
*/
|
||||
inherits(Spec, Base);
|
||||
|
||||
Spec.description = 'custom reporter for HTTP plugin testing';
|
||||
|
||||
Spec.prototype.epilogue = function() {
|
||||
var stats = this.stats;
|
||||
var fmt;
|
||||
|
||||
Base.consoleLog();
|
||||
|
||||
// passes
|
||||
fmt =
|
||||
color('bright pass', ' ') +
|
||||
color('green', ' %d passing') +
|
||||
color('light', ' (%s)');
|
||||
|
||||
Base.consoleLog(fmt, stats.passes || 0, milliseconds(stats.duration));
|
||||
|
||||
// pending
|
||||
if (stats.pending) {
|
||||
fmt = color('pending', ' ') + color('pending', ' %d pending');
|
||||
|
||||
Base.consoleLog(fmt, stats.pending);
|
||||
}
|
||||
|
||||
// failures
|
||||
if (stats.failures) {
|
||||
fmt = color('fail', ' %d failing');
|
||||
|
||||
Base.consoleLog(fmt, stats.failures);
|
||||
|
||||
this.showList(this.failures);
|
||||
Base.consoleLog();
|
||||
}
|
||||
|
||||
Base.consoleLog();
|
||||
};
|
||||
|
||||
Spec.prototype.showList = function(failures) {
|
||||
var multipleErr, multipleTest;
|
||||
var self = this;
|
||||
|
||||
Base.consoleLog();
|
||||
failures.forEach(function(test, i) {
|
||||
// format
|
||||
var fmt =
|
||||
color('error title', ' %s) %s:\n') +
|
||||
color('error message', ' %s') +
|
||||
color('error stack', '\n%s\n');
|
||||
|
||||
// msg
|
||||
var msg;
|
||||
var err;
|
||||
if (test.err && test.err.multiple) {
|
||||
if (multipleTest !== test) {
|
||||
multipleTest = test;
|
||||
multipleErr = [test.err].concat(test.err.multiple);
|
||||
}
|
||||
err = multipleErr.shift();
|
||||
} else {
|
||||
err = test.err;
|
||||
}
|
||||
var message;
|
||||
if (err.message && typeof err.message.toString === 'function') {
|
||||
message = err.message + '';
|
||||
} else if (typeof err.inspect === 'function') {
|
||||
message = err.inspect() + '';
|
||||
} else {
|
||||
message = '';
|
||||
}
|
||||
var stack = err.stack || message;
|
||||
var index = message ? stack.indexOf(message) : -1;
|
||||
|
||||
if (index === -1) {
|
||||
msg = message;
|
||||
} else {
|
||||
index += message.length;
|
||||
msg = stack.slice(0, index);
|
||||
// remove msg from stack
|
||||
stack = stack.slice(index + 1);
|
||||
}
|
||||
|
||||
// uncaught
|
||||
if (err.uncaught) {
|
||||
msg = 'Uncaught ' + msg;
|
||||
}
|
||||
// explicitly show diff
|
||||
if (Base.showDiff(err)) {
|
||||
self.stringifyDiffObjs(err);
|
||||
fmt =
|
||||
color('error title', ' %s) %s:\n%s') + color('error stack', '\n%s\n');
|
||||
var match = message.match(/^([^:]+): expected/);
|
||||
msg = '\n ' + color('error message', match ? match[1] : msg);
|
||||
|
||||
msg += Base.generateDiff(err.actual, err.expected);
|
||||
}
|
||||
|
||||
// indent stack trace
|
||||
stack = stack.replace(/^/gm, ' ');
|
||||
|
||||
// indented test title
|
||||
var testTitle = '';
|
||||
test.titlePath().forEach(function(str, index) {
|
||||
if (index !== 0) {
|
||||
testTitle += '\n ';
|
||||
}
|
||||
for (var i = 0; i < index; i++) {
|
||||
testTitle += ' ';
|
||||
}
|
||||
testTitle += str;
|
||||
});
|
||||
|
||||
Base.consoleLog(fmt, i + 1, testTitle, msg, stack);
|
||||
self.showDetails(err);
|
||||
});
|
||||
};
|
||||
|
||||
Spec.prototype.stringifyDiffObjs = function(err) {
|
||||
if (!isString(err.actual) || !isString(err.expected)) {
|
||||
err.actual = stringify(err.actual);
|
||||
err.expected = stringify(err.expected);
|
||||
}
|
||||
}
|
||||
|
||||
Spec.prototype.showDetails = function(err) {
|
||||
if (!err.details) {
|
||||
return;
|
||||
}
|
||||
|
||||
const details = JSON
|
||||
.stringify(err.details, null, 2)
|
||||
.replace(/^/gm, ' ');
|
||||
|
||||
Base.consoleLog(
|
||||
color('error stack', '\n Details:\n%s'),
|
||||
details
|
||||
);
|
||||
}
|
||||
|
||||
exports = module.exports = Spec;
|
||||
@@ -9,6 +9,11 @@ const configs = {
|
||||
host: 'ondemand.saucelabs.com',
|
||||
port: 80,
|
||||
auth: process.env.SAUCE_USERNAME + ":" + process.env.SAUCE_ACCESS_KEY
|
||||
},
|
||||
browserstack: {
|
||||
host: 'hub-cloud.browserstack.com',
|
||||
port: 80,
|
||||
auth: process.env.BROWSERSTACK_USERNAME + ":" + process.env.BROWSERSTACK_ACCESS_KEY
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
const wd = require('wd');
|
||||
const chai = require('chai');
|
||||
const chaiAsPromised = require('chai-as-promised');
|
||||
const logging = require('./logging');
|
||||
const capsConfig = require('./caps');
|
||||
const serverConfig = require('./server');
|
||||
const testDefinitions = require('../e2e-specs');
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
chaiAsPromised.transferPromiseness = wd.transferPromiseness;
|
||||
global.should = chai.should();
|
||||
|
||||
let driver;
|
||||
let allPassed = true;
|
||||
|
||||
describe('Advanced HTTP e2e test suite', function () {
|
||||
const isSauceLabs = !!process.env.SAUCE_USERNAME;
|
||||
const isBrowserStack = !!process.env.BROWSERSTACK_USERNAME;
|
||||
const isVerbose = process.argv.includes('--verbose');
|
||||
const isDevice = process.argv.includes('--device');
|
||||
const isAndroid = process.argv.includes('--android');
|
||||
|
||||
const targetInfo = { isSauceLabs, isDevice, isAndroid };
|
||||
const environment = isSauceLabs ? 'saucelabs' : 'local';
|
||||
|
||||
let driver;
|
||||
let allPassed = true;
|
||||
const targetInfo = { isSauceLabs, isBrowserStack, isDevice, isAndroid };
|
||||
const environment = isSauceLabs ? 'saucelabs' : isBrowserStack ? 'browserstack' : 'local';
|
||||
|
||||
this.timeout(15000);
|
||||
this.slow(4000);
|
||||
@@ -51,12 +49,19 @@ describe('Advanced HTTP e2e test suite', function () {
|
||||
});
|
||||
|
||||
const defineTestForMocha = (test, index) => {
|
||||
it(index + ': ' + test.description, async () => {
|
||||
it(index + ': ' + test.description, async function () {
|
||||
await clickNext(driver);
|
||||
await validateTestIndex(driver, index);
|
||||
await validateTestTitle(driver, test.description);
|
||||
await waitToBeFinished(driver, test.timeout || 10000);
|
||||
await validateResult(driver, test.validationFunc, targetInfo);
|
||||
|
||||
const skipped = await checkSkipped(driver);
|
||||
|
||||
if (skipped) {
|
||||
this.skip();
|
||||
} else {
|
||||
await validateResult(driver, test.validationFunc, targetInfo);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -116,7 +121,20 @@ async function waitToBeFinished(driver, timeout) {
|
||||
|
||||
async function validateResult(driver, validationFunc, targetInfo) {
|
||||
const result = await driver.safeExecute('app.lastResult');
|
||||
validationFunc(driver, result, targetInfo);
|
||||
|
||||
try {
|
||||
validationFunc(driver, result, targetInfo);
|
||||
} catch (error) {
|
||||
allPassed = false;
|
||||
error.details = result;
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSkipped(driver) {
|
||||
const result = await driver.safeExecute('app.lastResult');
|
||||
return result.type === 'skipped';
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
|
||||
114
test/js-specs.js
114
test/js-specs.js
@@ -1,5 +1,4 @@
|
||||
const chai = require('chai');
|
||||
const mock = require('mock-require');
|
||||
const util = require('util');
|
||||
const should = chai.should();
|
||||
|
||||
@@ -50,6 +49,13 @@ describe('Advanced HTTP public interface', function () {
|
||||
http.getHeaders('*').myKey.should.equal('myValue');
|
||||
});
|
||||
|
||||
it('clears global headers correctly when value is undefined', () => {
|
||||
http.setHeader('*', 'myKey', 'myValue');
|
||||
http.setHeader('*', 'myKey', null);
|
||||
should.equal(undefined, http.getHeaders('*').myKey);
|
||||
Object.keys(http.getHeaders('*')).length.should.be.equal(0);
|
||||
});
|
||||
|
||||
it('sets host headers correctly #24', () => {
|
||||
http.setHeader('www.google.de', 'myKey', 'myValue');
|
||||
http.getHeaders('www.google.de').myKey.should.equal('myValue');
|
||||
@@ -419,7 +425,7 @@ describe('Common helpers', function () {
|
||||
response => response.data.should.be.equal('fakeData')
|
||||
);
|
||||
|
||||
handler({ data: 'fakeData' });
|
||||
handler({ data: 'fakeData' });
|
||||
});
|
||||
|
||||
it('does not change response data if response type is "text"', () => {
|
||||
@@ -444,6 +450,17 @@ describe('Common helpers', function () {
|
||||
handler({ data: JSON.stringify(fakeData) });
|
||||
});
|
||||
|
||||
it('handles empty "json" response correctly', () => {
|
||||
const emptyData = "";
|
||||
const helpers = require('../www/helpers')(null, jsUtil, null, messages, null, errorCodes);
|
||||
const handler = helpers.injectRawResponseHandler(
|
||||
'json',
|
||||
response => should.equal(undefined, response.data)
|
||||
);
|
||||
|
||||
handler({ data: emptyData });
|
||||
});
|
||||
|
||||
it('handles response type "arraybuffer" correctly', () => {
|
||||
const helpers = require('../www/helpers')(null, jsUtil, null, messages, fakeBase64, errorCodes);
|
||||
const handler = helpers.injectRawResponseHandler(
|
||||
@@ -454,6 +471,16 @@ describe('Common helpers', function () {
|
||||
handler({ data: 'myString' });
|
||||
});
|
||||
|
||||
it('handles empty "arraybuffer" response correctly', () => {
|
||||
const helpers = require('../www/helpers')(null, jsUtil, null, messages, fakeBase64, errorCodes);
|
||||
const handler = helpers.injectRawResponseHandler(
|
||||
'arraybuffer',
|
||||
response => should.equal(null, response.data)
|
||||
);
|
||||
|
||||
handler({ data: '' });
|
||||
});
|
||||
|
||||
it('handles response type "blob" correctly', () => {
|
||||
const helpers = require('../www/helpers')(null, jsUtil, null, messages, fakeBase64, errorCodes);
|
||||
const handler = helpers.injectRawResponseHandler(
|
||||
@@ -465,7 +492,19 @@ describe('Common helpers', function () {
|
||||
}
|
||||
);
|
||||
|
||||
handler({ data: 'myString', headers: { 'content-type': 'fakeType'} });
|
||||
handler({ data: 'myString', headers: { 'content-type': 'fakeType' } });
|
||||
});
|
||||
|
||||
it('handles empty "blob" response correctly', () => {
|
||||
const helpers = require('../www/helpers')(null, jsUtil, null, messages, fakeBase64, errorCodes);
|
||||
const handler = helpers.injectRawResponseHandler(
|
||||
'blob',
|
||||
(response) => {
|
||||
should.equal(null, response.data)
|
||||
}
|
||||
);
|
||||
|
||||
handler({ data: '', headers: { 'content-type': 'fakeType' } });
|
||||
});
|
||||
|
||||
it('calls failure callback when post-processing fails', () => {
|
||||
@@ -546,7 +585,7 @@ describe('Common helpers', function () {
|
||||
|
||||
it('processes data correctly when serializer "utf8" is configured', (cb) => {
|
||||
helpers.processData('myString', 'utf8', (data) => {
|
||||
data.should.be.eql({text: 'myString'});
|
||||
data.should.be.eql({ text: 'myString' });
|
||||
cb();
|
||||
})
|
||||
});
|
||||
@@ -589,6 +628,67 @@ describe('Common helpers', function () {
|
||||
cb();
|
||||
});
|
||||
});
|
||||
|
||||
it('processes data correctly when serializer "multipart" is configured and form data contains file value (filename set)', (cb) => {
|
||||
const formData = new FormDataMock();
|
||||
formData.append('myFile', new BlobMock([testString], { type: 'application/octet-stream' }), 'file.name');
|
||||
|
||||
helpers.processData(formData, 'multipart', (data) => {
|
||||
data.buffers.length.should.be.equal(1);
|
||||
data.names.length.should.be.equal(1);
|
||||
data.fileNames.length.should.be.equal(1);
|
||||
data.types.length.should.be.equal(1);
|
||||
|
||||
data.buffers[0].should.be.eql(testStringBase64);
|
||||
data.names[0].should.be.equal('myFile');
|
||||
data.fileNames[0].should.be.equal('file.name');
|
||||
data.types[0].should.be.equal('application/octet-stream');
|
||||
|
||||
cb();
|
||||
});
|
||||
});
|
||||
|
||||
it('processes data correctly when serializer "multipart" is configured and form data contains file value (filename empty)', (cb) => {
|
||||
const formData = new FormDataMock();
|
||||
formData.append('myFile', new BlobMock([testString], { type: 'application/octet-stream' }), '');
|
||||
|
||||
helpers.processData(formData, 'multipart', (data) => {
|
||||
data.buffers.length.should.be.equal(1);
|
||||
data.names.length.should.be.equal(1);
|
||||
data.fileNames.length.should.be.equal(1);
|
||||
data.types.length.should.be.equal(1);
|
||||
|
||||
data.buffers[0].should.be.eql(testStringBase64);
|
||||
data.names[0].should.be.equal('myFile');
|
||||
data.fileNames[0].should.be.equal('');
|
||||
data.types[0].should.be.equal('application/octet-stream');
|
||||
|
||||
cb();
|
||||
});
|
||||
});
|
||||
|
||||
it('processes data correctly when serializer "raw" is configured', (cb) => {
|
||||
const byteArray = new Uint8Array([1, 2, 3]);
|
||||
helpers.processData(byteArray, 'raw', (data) => {
|
||||
data.should.be.a('ArrayBuffer');
|
||||
data.should.be.equal(byteArray.buffer);
|
||||
cb();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe('nextRequestId()', function () {
|
||||
const helpers = require('../www/helpers')(null, null, null, null, null, null);
|
||||
|
||||
it('returns number requestIds', () => {
|
||||
helpers.nextRequestId().should.be.a('number');
|
||||
});
|
||||
|
||||
it('returns unique requestIds', () => {
|
||||
const ids = [helpers.nextRequestId(), helpers.nextRequestId(), helpers.nextRequestId()];
|
||||
const set = new Set(ids);
|
||||
ids.should.to.deep.equal(Array.from(set));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -638,7 +738,7 @@ describe('Dependency Validator', function () {
|
||||
describe('checkFormDataInstance()', function () {
|
||||
it('throws an error if FormData.entries() is not supported on given instance', function () {
|
||||
const console = new ConsoleMock();
|
||||
const validator = require('../www/dependency-validator')({ FormData: {}}, console, messages);
|
||||
const validator = require('../www/dependency-validator')({ FormData: {} }, console, messages);
|
||||
|
||||
(() => validator.checkFormDataInstance({})).should.throw(messages.MISSING_FORMDATA_ENTRIES_API);
|
||||
});
|
||||
@@ -675,12 +775,12 @@ describe('Ponyfills', function () {
|
||||
const iterator = new ponyfills.Iterator([]);
|
||||
iterator.next().should.be.eql({ done: true, value: undefined });
|
||||
});
|
||||
|
||||
|
||||
it('returns iteration object correctly when end posititon of list is not reached yet', () => {
|
||||
const iterator = new ponyfills.Iterator([['first', 'this is the first item']]);
|
||||
iterator.next().should.be.eql({ done: false, value: ['first', 'this is the first item'] });
|
||||
});
|
||||
|
||||
|
||||
it('returns iteration object correctly when end posititon of list is already reached', () => {
|
||||
const iterator = new ponyfills.Iterator([['first', 'this is the first item']]);
|
||||
iterator.next();
|
||||
|
||||
@@ -3,7 +3,7 @@ const BlobMock = require('./Blob.mock');
|
||||
module.exports = class FileMock extends BlobMock {
|
||||
constructor(blob, fileName) {
|
||||
super(blob, { type: blob.type });
|
||||
this._fileName = fileName || '';
|
||||
this._fileName = fileName !== undefined ? fileName : 'blob';
|
||||
this.__lastModifiedDate = new Date();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,4 +6,5 @@ module.exports = {
|
||||
UNSUPPORTED_URL: -5,
|
||||
NOT_CONNECTED: -6,
|
||||
POST_PROCESSING_FAILED: -7,
|
||||
ABORTED: -8,
|
||||
};
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
module.exports = function init(global, jsUtil, cookieHandler, messages, base64, errorCodes, dependencyValidator, ponyfills) {
|
||||
var validSerializers = ['urlencoded', 'json', 'utf8', 'multipart'];
|
||||
var validSerializers = ['urlencoded', 'json', 'utf8', 'raw', 'multipart'];
|
||||
var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
|
||||
var validClientAuthModes = ['none', 'systemstore', 'buffer'];
|
||||
var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download'];
|
||||
var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'options', 'upload', 'download'];
|
||||
var validResponseTypes = ['text', 'json', 'arraybuffer', 'blob'];
|
||||
|
||||
var nextRequestId = (function(){
|
||||
var currReqId = 0;
|
||||
return function nextRequestId() {
|
||||
return ++currReqId;
|
||||
}
|
||||
})();
|
||||
|
||||
var interface = {
|
||||
b64EncodeUnicode: b64EncodeUnicode,
|
||||
checkClientAuthMode: checkClientAuthMode,
|
||||
@@ -24,6 +31,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
injectCookieHandler: injectCookieHandler,
|
||||
injectFileEntryHandler: injectFileEntryHandler,
|
||||
injectRawResponseHandler: injectRawResponseHandler,
|
||||
nextRequestId: nextRequestId,
|
||||
};
|
||||
|
||||
// expose all functions for testing purposes
|
||||
@@ -187,7 +195,9 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
}
|
||||
|
||||
function checkForInvalidHeaderValue(value) {
|
||||
if (jsUtil.getTypeOf(value) !== 'String') {
|
||||
var type = jsUtil.getTypeOf(value);
|
||||
|
||||
if (type !== 'String' && type !== 'Null') {
|
||||
throw new Error(messages.INVALID_HEADER_VALUE);
|
||||
}
|
||||
|
||||
@@ -295,20 +305,28 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
try {
|
||||
// json
|
||||
if (responseType === validResponseTypes[1]) {
|
||||
response.data = JSON.parse(response.data);
|
||||
response.data = response.data === ''
|
||||
? undefined
|
||||
: JSON.parse(response.data);
|
||||
}
|
||||
|
||||
// arraybuffer
|
||||
else if (responseType === validResponseTypes[2]) {
|
||||
response.data = base64.toArrayBuffer(response.data);
|
||||
response.data = response.data === ''
|
||||
? null
|
||||
: base64.toArrayBuffer(response.data);
|
||||
}
|
||||
|
||||
// blob
|
||||
else if (responseType === validResponseTypes[3]) {
|
||||
var buffer = base64.toArrayBuffer(response.data);
|
||||
var type = response.headers['content-type'] || '';
|
||||
var blob = new Blob([ buffer ], { type: type });
|
||||
response.data = blob;
|
||||
if (response.data === '') {
|
||||
response.data = null;
|
||||
} else {
|
||||
var buffer = base64.toArrayBuffer(response.data);
|
||||
var type = response.headers['content-type'] || '';
|
||||
var blob = new Blob([buffer], { type: type });
|
||||
response.data = blob;
|
||||
}
|
||||
}
|
||||
|
||||
success(response);
|
||||
@@ -365,6 +383,8 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
return ['Object'];
|
||||
case 'json':
|
||||
return ['Array', 'Object'];
|
||||
case 'raw':
|
||||
return ['Uint8Array', 'ArrayBuffer'];
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
@@ -382,7 +402,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
if (allowedInstanceTypes) {
|
||||
var isCorrectInstanceType = false;
|
||||
|
||||
allowedInstanceTypes.forEach(function(type) {
|
||||
allowedInstanceTypes.forEach(function (type) {
|
||||
if ((global[type] && data instanceof global[type]) || (ponyfills[type] && data instanceof ponyfills[type])) {
|
||||
isCorrectInstanceType = true;
|
||||
}
|
||||
@@ -400,6 +420,8 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
switch (dataSerializer) {
|
||||
case 'utf8':
|
||||
return cb({ text: data });
|
||||
case 'raw':
|
||||
return cb(currentDataType === 'Uint8Array' ? data.buffer : data);
|
||||
case 'multipart':
|
||||
return processFormData(data, cb);
|
||||
default:
|
||||
@@ -436,10 +458,10 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64,
|
||||
if (entry.value[1] instanceof global.Blob || entry.value[1] instanceof global.File) {
|
||||
var reader = new global.FileReader();
|
||||
|
||||
reader.onload = function() {
|
||||
reader.onload = function () {
|
||||
result.buffers.push(base64.fromArrayBuffer(reader.result));
|
||||
result.names.push(entry.value[0]);
|
||||
result.fileNames.push(entry.value[1].name || 'blob');
|
||||
result.fileNames.push(entry.value[1].name !== undefined ? entry.value[1].name : 'blob');
|
||||
result.types.push(entry.value[1].type || '');
|
||||
processFormDataIterator(iterator, textEncoder, result, onFinished);
|
||||
};
|
||||
|
||||
@@ -6,6 +6,8 @@ module.exports = {
|
||||
return 'Array';
|
||||
case '[object Blob]':
|
||||
return 'Blob';
|
||||
case '[object Uint8Array]':
|
||||
return 'Uint8Array';
|
||||
case '[object ArrayBuffer]':
|
||||
return 'ArrayBuffer';
|
||||
case '[object Boolean]':
|
||||
|
||||
@@ -146,7 +146,7 @@ module.exports = function init(ToughCookie, _) {
|
||||
|
||||
Object.keys(store).forEach(function (domain) {
|
||||
Object.keys(store[domain]).forEach(function (path) {
|
||||
Array.protype.push.apply(cookies, _.values(store[domain][path]));
|
||||
Array.prototype.push.apply(cookies, _.values(store[domain][path]));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ module.exports = {
|
||||
INVALID_DATA_SERIALIZER: 'advanced-http: invalid serializer, supported serializers are:',
|
||||
INVALID_DOWNLOAD_FILE_PATH: 'advanced-http: invalid "filePath" value, needs to be a string, <filePath: string>',
|
||||
INVALID_FOLLOW_REDIRECT_VALUE: 'advanced-http: invalid follow redirect value, needs to be a boolean value, <followRedirect: boolean>',
|
||||
INVALID_HEADER_VALUE: 'advanced-http: invalid header value, needs to be a string, <header: string>',
|
||||
INVALID_HEADER_VALUE: 'advanced-http: invalid header value, needs to be a string or null, <header: string | null>',
|
||||
INVALID_HTTP_METHOD: 'advanced-http: invalid HTTP method, supported methods are:',
|
||||
INVALID_RESPONSE_TYPE: 'advanced-http: invalid response type, supported types are:',
|
||||
INVALID_SSL_CERT_MODE: 'advanced-http: invalid SSL cert mode, supported modes are:',
|
||||
|
||||
@@ -16,9 +16,9 @@ module.exports = function init(global) {
|
||||
} else if (global.Blob && value instanceof global.Blob) {
|
||||
// mimic File instance by adding missing properties
|
||||
value.lastModifiedDate = new Date();
|
||||
value.name = filename || '';
|
||||
value.name = filename !== undefined ? filename : 'blob';
|
||||
} else {
|
||||
value = value.toString ? value.toString() : value;
|
||||
value = String(value);
|
||||
}
|
||||
|
||||
this.__items.push([ name, value ]);
|
||||
@@ -44,4 +44,4 @@ module.exports = function init(global) {
|
||||
}
|
||||
|
||||
return interface;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -14,21 +14,19 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
setRequestTimeout: setRequestTimeout,
|
||||
getFollowRedirect: getFollowRedirect,
|
||||
setFollowRedirect: setFollowRedirect,
|
||||
// @DEPRECATED
|
||||
disableRedirect: disableRedirect,
|
||||
// @DEPRECATED
|
||||
setSSLCertMode: setServerTrustMode,
|
||||
setServerTrustMode: setServerTrustMode,
|
||||
setClientAuthMode: setClientAuthMode,
|
||||
sendRequest: sendRequest,
|
||||
post: post,
|
||||
get: get,
|
||||
put: put,
|
||||
patch: patch,
|
||||
get: get,
|
||||
delete: del,
|
||||
head: head,
|
||||
options: options,
|
||||
uploadFile: uploadFile,
|
||||
downloadFile: downloadFile,
|
||||
abort: abort,
|
||||
ErrorCode: errorCodes,
|
||||
ponyfills: ponyfills
|
||||
};
|
||||
@@ -61,7 +59,12 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
helpers.checkForInvalidHeaderValue(value);
|
||||
|
||||
globalConfigs.headers[host] = globalConfigs.headers[host] || {};
|
||||
globalConfigs.headers[host][header] = value;
|
||||
|
||||
if (value === null) {
|
||||
delete globalConfigs.headers[host][header];
|
||||
} else {
|
||||
globalConfigs.headers[host][header] = value;
|
||||
}
|
||||
}
|
||||
|
||||
function getDataSerializer() {
|
||||
@@ -104,14 +107,6 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
globalConfigs.followRedirect = helpers.checkFollowRedirectValue(follow);
|
||||
}
|
||||
|
||||
// @DEPRECATED
|
||||
function disableRedirect(disable, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
setFollowRedirect(!disable);
|
||||
success();
|
||||
}
|
||||
|
||||
function setServerTrustMode(mode, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
@@ -149,33 +144,37 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
var onFail = helpers.injectCookieHandler(url, failure);
|
||||
var onSuccess = helpers.injectCookieHandler(url, helpers.injectRawResponseHandler(options.responseType, success, failure));
|
||||
|
||||
var reqId = helpers.nextRequestId();
|
||||
|
||||
switch (options.method) {
|
||||
case 'post':
|
||||
case 'put':
|
||||
case 'patch':
|
||||
return helpers.processData(options.data, options.serializer, function(data) {
|
||||
exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType]);
|
||||
helpers.processData(options.data, options.serializer, function (data) {
|
||||
exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType, reqId]);
|
||||
});
|
||||
break;
|
||||
case 'upload':
|
||||
var fileOptions = helpers.checkUploadFileOptions(options.filePath, options.name);
|
||||
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType]);
|
||||
exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType, reqId]);
|
||||
break;
|
||||
case 'download':
|
||||
var filePath = helpers.checkDownloadFilePath(options.filePath);
|
||||
var onDownloadSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
|
||||
return exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect]);
|
||||
exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect, reqId]);
|
||||
break;
|
||||
default:
|
||||
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType]);
|
||||
exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType, reqId]);
|
||||
break;
|
||||
}
|
||||
|
||||
return reqId;
|
||||
}
|
||||
|
||||
function post(url, data, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'post', data: data, headers: headers }, success, failure);
|
||||
};
|
||||
|
||||
function get(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
|
||||
};
|
||||
|
||||
function put(url, data, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'put', data: data, headers: headers }, success, failure);
|
||||
}
|
||||
@@ -184,6 +183,10 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
return publicInterface.sendRequest(url, { method: 'patch', data: data, headers: headers }, success, failure);
|
||||
}
|
||||
|
||||
function get(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
|
||||
};
|
||||
|
||||
function del(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'delete', params: params, headers: headers }, success, failure);
|
||||
}
|
||||
@@ -192,6 +195,10 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
return publicInterface.sendRequest(url, { method: 'head', params: params, headers: headers }, success, failure);
|
||||
}
|
||||
|
||||
function options(url, params, headers, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'options', params: params, headers: headers }, success, failure);
|
||||
};
|
||||
|
||||
function uploadFile(url, params, headers, filePath, name, success, failure) {
|
||||
return publicInterface.sendRequest(url, { method: 'upload', params: params, headers: headers, filePath: filePath, name: name }, success, failure);
|
||||
}
|
||||
@@ -200,5 +207,9 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf
|
||||
return publicInterface.sendRequest(url, { method: 'download', params: params, headers: headers, filePath: filePath }, success, failure);
|
||||
}
|
||||
|
||||
function abort(requestId , success, failure) {
|
||||
return exec(success, failure, 'CordovaHttpPlugin', 'abort', [requestId]);
|
||||
}
|
||||
|
||||
return publicInterface;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user