├── .editorconfig ├── .eslintrc.json ├── .gitignore ├── .npmignore ├── .publishrc ├── .travis.yml ├── LICENSE ├── README.md ├── errors.js ├── gulpfile.js ├── lib └── rp.js ├── package.json └── test ├── .eslintrc.json ├── fixtures ├── require │ ├── afterwards.js │ ├── beforehand.js │ └── beforehandAndAfterwards.js └── server.js └── spec └── request-test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | 9 | [*.js] 10 | indent_style = space 11 | indent_size = 4 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | [*.{json,xml}] 16 | indent_style = space 17 | indent_size = 2 18 | trim_trailing_whitespace = true 19 | insert_final_newline = true 20 | 21 | [*.{md,txt}] 22 | indent_style = space 23 | indent_size = 4 24 | trim_trailing_whitespace = false 25 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "parser": "espree", 4 | "parserOptions": { 5 | "ecmaVersion": 5, 6 | "sourceType": "script", 7 | "ecmaFeatures": {} 8 | }, 9 | "plugins": [], 10 | "env": { 11 | "node": true 12 | }, 13 | "globals": { 14 | "Promise": false 15 | }, 16 | "rules": { 17 | 18 | // Possible Errors (fully reviewed 2016-07-05) 19 | 20 | "no-cond-assign": 2, 21 | "no-console": 0, 22 | "no-constant-condition": 2, 23 | "no-control-regex": 2, 24 | "no-debugger": 2, 25 | "no-dupe-args": 2, 26 | "no-dupe-keys": 2, 27 | "no-duplicate-case": 2, 28 | "no-empty": [2, { "allowEmptyCatch": false }], 29 | "no-empty-character-class": 2, 30 | "no-ex-assign": 2, 31 | "no-extra-boolean-cast": 2, 32 | "no-extra-parens": [2, "all"], 33 | "no-extra-semi": 2, 34 | "no-func-assign": 2, 35 | "no-inner-declarations": [2, "functions"], 36 | "no-invalid-regexp": 2, 37 | "no-irregular-whitespace": [2, { "skipComments": false }], 38 | "no-negated-in-lhs": 2, 39 | "no-obj-calls": 2, 40 | "no-prototype-builtins": 2, 41 | "no-regex-spaces": 2, 42 | "no-sparse-arrays": 2, 43 | "no-unexpected-multiline": 2, 44 | "no-unreachable": 2, 45 | "no-unsafe-finally": 2, 46 | "use-isnan": 2, 47 | "valid-jsdoc": 0, 48 | "valid-typeof": 2, 49 | 50 | // Best Practices 51 | 52 | "accessor-pairs": 2, 53 | "curly": [2, "multi-line"], 54 | "dot-location": [2, "property"], 55 | "eqeqeq": 2, 56 | "no-caller": 2, 57 | "no-empty-pattern": 0, // for ES6 destructuring 58 | "no-eval": 2, 59 | "no-extend-native": 2, 60 | "no-extra-bind": 2, 61 | "no-fallthrough": 2, 62 | "no-floating-decimal": 2, 63 | "no-implied-eval": 2, 64 | "no-iterator": 2, 65 | "no-labels": 2, 66 | "no-lone-blocks": 2, 67 | "no-magic-numbers": 0, 68 | "no-multi-spaces": 2, 69 | "no-multi-str": 2, 70 | "no-native-reassign": 2, 71 | "no-new": 2, 72 | "no-new-func": 2, 73 | "no-new-wrappers": 2, 74 | "no-octal": 2, 75 | "no-octal-escape": 2, 76 | "no-proto": 2, 77 | "no-redeclare": 2, 78 | "no-return-assign": [2, "except-parens"], 79 | "no-self-assign": 2, 80 | "no-self-compare": 2, 81 | "no-sequences": 2, 82 | "no-throw-literal": 2, 83 | "no-unmodified-loop-condition": 2, 84 | "no-useless-call": 2, 85 | "no-useless-escape": 2, 86 | "no-with": 2, 87 | "wrap-iife": [2, "inside"], 88 | "yoda": 2, 89 | 90 | // Strict Mode (fully reviewed 2016-07-05) 91 | 92 | "strict": [2, "safe"], 93 | 94 | // Variables (fully reviewed 2016-07-05) 95 | 96 | "init-declarations": [2, "always"], 97 | "no-catch-shadow": 0, 98 | "no-delete-var": 2, 99 | "no-label-var": 2, 100 | "no-restricted-globals": 0, 101 | "no-shadow": [2, { "builtinGlobals": false, "hoist": "all", "allow": [] }], 102 | "no-shadow-restricted-names": 2, 103 | "no-undef": [2, { "typeof": true }], 104 | "no-undef-init": 2, 105 | "no-undefined": 0, 106 | "no-unused-vars": [2, { "vars": "local", "args": "none", "caughtErrors": "none" }], 107 | "no-use-before-define": [2, { "functions": false, "classes": true }], 108 | 109 | // Node.js and CommonJS (fully reviewed 2016-07-05) 110 | 111 | "callback-return": 0, 112 | "global-require": 0, 113 | "handle-callback-err": [2, "^(err|error)$" ], 114 | "no-mixed-requires": 0, 115 | "no-new-require": 2, 116 | "no-path-concat": 2, 117 | "no-process-env": 2, 118 | "no-process-exit": 2, 119 | "no-restricted-modules": 0, 120 | "no-sync": 2, 121 | 122 | // Stylistic Issues 123 | 124 | "block-spacing": 2, 125 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 126 | "camelcase": [2, { "properties": "never" }], 127 | "comma-dangle": [2, "never"], 128 | "comma-spacing": 2, 129 | "comma-style": 2, 130 | "eol-last": 2, 131 | "indent": [2, 4, { "SwitchCase": 1 }], 132 | "jsx-quotes": 0, 133 | "key-spacing": 2, 134 | "keyword-spacing": 2, 135 | "new-cap": 0, 136 | "new-parens": 2, 137 | "no-array-constructor": 2, 138 | "no-mixed-spaces-and-tabs": 2, 139 | "no-multiple-empty-lines": [2, { "max": 2, "maxBOF": 0, "maxEOF": 1 }], 140 | "no-new-object": 2, 141 | "no-plusplus": [2, { "allowForLoopAfterthoughts": false }], 142 | "no-spaced-func": 2, 143 | "no-trailing-spaces": 2, 144 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 145 | "no-whitespace-before-property": 2, 146 | "one-var": 0, 147 | "operator-linebreak": [2, "after", { "overrides": { "?": "before", ":": "before" } }], 148 | "padded-blocks": 0, 149 | "quotes": [2, "single", "avoid-escape"], 150 | "semi": [2, "always"], 151 | "semi-spacing": 2, 152 | "space-before-blocks": 2, 153 | "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], 154 | "space-in-parens": 0, 155 | "space-infix-ops": 0, 156 | "space-unary-ops": 2, 157 | "spaced-comment": 0 158 | 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /coverage/ 3 | /node_modules/ 4 | 5 | .DS_Store 6 | npm-debug.log -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /.idea/ 2 | /coverage/ 3 | /test/ 4 | 5 | /.editorconfig 6 | /.eslintrc.json 7 | /.gitignore 8 | /.publishrc 9 | /.travis.yml 10 | /gulpfile.js 11 | 12 | .DS_Store 13 | npm-debug.log -------------------------------------------------------------------------------- /.publishrc: -------------------------------------------------------------------------------- 1 | { 2 | "validations": { 3 | "vulnerableDependencies": false, 4 | "uncommittedChanges": true, 5 | "untrackedFiles": true, 6 | "sensitiveData": true, 7 | "branch": "master", 8 | "gitTag": true 9 | }, 10 | "confirm": true, 11 | "publishTag": "latest", 12 | "prePublishScript": "npm run test-publish" 13 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | 4 | matrix: 5 | include: 6 | - node_js: "0.12" 7 | env: V_REQUEST=2.76.0 8 | - node_js: "iojs" 9 | env: V_REQUEST=2.76.0 10 | - node_js: "4" 11 | env: V_REQUEST=latest 12 | - node_js: "6" 13 | env: V_REQUEST=latest 14 | - node_js: "8" 15 | env: V_REQUEST=latest 16 | - node_js: "10" 17 | env: V_REQUEST=latest 18 | 19 | before_install: 20 | - npm install tough-cookie 21 | - npm install request@$V_REQUEST 22 | 23 | install: npm install 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2020, Nicolai Kamenzky and contributors 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Promises/A+ logo 3 | 4 | 5 | # Request-Promise-Native 6 | 7 | [![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square&maxAge=2592000)](https://gitter.im/request/request-promise?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 8 | [![Build Status](https://img.shields.io/travis/request/request-promise-native/master.svg?style=flat-square&maxAge=2592000)](https://travis-ci.org/request/request-promise-native) 9 | [![Coverage Status](https://img.shields.io/coveralls/request/request-promise-native.svg?style=flat-square&maxAge=2592000)](https://coveralls.io/r/request/request-promise-native) 10 | [![Dependency Status](https://img.shields.io/david/request/request-promise-native.svg?style=flat-square&maxAge=2592000)](https://david-dm.org/request/request-promise-native) 11 | [![Known Vulnerabilities](https://snyk.io/test/npm/request-promise-native/badge.svg?style=flat-square&maxAge=2592000)](https://snyk.io/test/npm/request-promise-native) 12 | 13 | # Deprecated! 14 | 15 | As of Feb 11th 2020, [`request`](https://github.com/request/request) is fully deprecated. No new changes are expected to land. In fact, none have landed for some time. This package is also deprecated because it depends on `request`. 16 | 17 | Fyi, here is the [reasoning of `request`'s deprecation](https://github.com/request/request/issues/3142) and a [list of alternative libraries](https://github.com/request/request/issues/3143). 18 | 19 | --- 20 | 21 | This package is similar to [`request-promise`](https://www.npmjs.com/package/request-promise) but uses native ES6+ promises. 22 | 23 | Please refer to the [`request-promise` documentation](https://www.npmjs.com/package/request-promise). Everything applies to `request-promise-native` except the following: 24 | - Instead of using Bluebird promises this library uses native ES6+ promises. 25 | - Native ES6+ promises may have fewer features than Bluebird promises do. In particular, the `.finally(...)` method was not included until Node v10. 26 | 27 | ## Installation 28 | 29 | This module is installed via npm: 30 | 31 | ``` 32 | npm install --save request 33 | npm install --save request-promise-native 34 | ``` 35 | 36 | `request` is defined as a peer-dependency and thus has to be installed separately. 37 | 38 | ## Migration from `request-promise` to `request-promise-native` 39 | 40 | 1. Go through the [migration instructions](https://github.com/request/request-promise#migration-from-v3-to-v4) to upgrade to `request-promise` v4. 41 | 2. Ensure that you don't use Bluebird-specific features on the promise returned by your request calls. In particular, you can't use `.finally(...)` anymore. 42 | 3. You are done. 43 | 44 | ## Contributing 45 | 46 | To set up your development environment: 47 | 48 | 1. clone the repo to your desktop, 49 | 2. in the shell `cd` to the main folder, 50 | 3. hit `npm install`, 51 | 4. hit `npm install gulp -g` if you haven't installed gulp globally yet, and 52 | 5. run `gulp dev`. (Or run `node ./node_modules/.bin/gulp dev` if you don't want to install gulp globally.) 53 | 54 | `gulp dev` watches all source files and if you save some changes it will lint the code and execute all tests. The test coverage report can be viewed from `./coverage/lcov-report/index.html`. 55 | 56 | If you want to debug a test you should use `gulp test-without-coverage` to run all tests without obscuring the code by the test coverage instrumentation. 57 | 58 | ## Change History 59 | 60 | - v1.0.9 (2020-07-21) 61 | - Security fix: bumped `request-promise-core` which bumps `lodash` to `^4.17.19` following [this advisory](https://www.npmjs.com/advisories/1523). 62 | - v1.0.8 (2019-11-03) 63 | - Security fix: bumped `request-promise-core` which bumps `lodash` to `^4.17.15`. See [vulnerabilty reports](https://snyk.io/vuln/search?q=lodash&type=npm). 64 | *(Thanks to @aw-davidson for reporting this in issue [#49](https://github.com/request/request-promise-native/issues/49).)* 65 | - v1.0.7 (2019-02-14) 66 | - Corrected mistakenly set `tough-cookie` version, now `^2.3.3` 67 | *(Thanks to @evocateur for pointing this out.)* 68 | - If you installed `request-promise-native@1.0.6` please make sure after the upgrade that `request` and `request-promise-native` use the same physical copy of `tough-cookie`. 69 | - v1.0.6 (2019-02-14) 70 | - Using stricter `tough-cookie@~2.3.3` to avoid installing `tough-cookie@3` which introduces breaking changes 71 | *(Thanks to @jasonmit for pull request [#33](https://github.com/request/request-promise-native/pull/33/))* 72 | - Security fix: bumped `lodash` to `^4.17.11`, see [vulnerabilty reports](https://snyk.io/vuln/search?q=lodash&type=npm) 73 | - v1.0.5 (2017-09-22) 74 | - Upgraded `tough-cookie` to a version without regex DoS vulnerability 75 | *(Thanks to @sophieklm for [pull request #13](https://github.com/request/request-promise-native/pull/13))* 76 | - v1.0.4 (2017-05-07) 77 | - Fix that allows to use `tough-cookie` for [cookie creation](https://github.com/request/request-promise#include-a-cookie) 78 | - v1.0.3 (2016-08-08) 79 | - Renamed internally used package `@request/promise-core` to `request-promise-core` because there where [too](https://github.com/request/request-promise/issues/137) [many](https://github.com/request/request-promise/issues/141) issues with the scoped package name 80 | - v1.0.2 (2016-07-18) 81 | - Fix for using with module bundlers like Webpack and Browserify 82 | - v1.0.1 (2016-07-17) 83 | - Fixed `@request/promise-core` version for safer versioning 84 | - v1.0.0 (2016-07-15) 85 | - Initial version similar to [`request-promise`](https://www.npmjs.com/package/request-promise) v4 86 | 87 | ## License (ISC) 88 | 89 | In case you never heard about the [ISC license](http://en.wikipedia.org/wiki/ISC_license) it is functionally equivalent to the MIT license. 90 | 91 | See the [LICENSE file](LICENSE) for details. 92 | -------------------------------------------------------------------------------- /errors.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = require('request-promise-core/errors'); 4 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var runSequence = require('run-sequence'); 5 | var istanbul = require('gulp-istanbul'); 6 | var mocha = require('gulp-mocha'); 7 | var chalk = require('chalk'); 8 | var rimraf = require('rimraf'); 9 | var coveralls = require('gulp-coveralls'); 10 | var eslint = require('gulp-eslint'); 11 | var _ = require('lodash'); 12 | 13 | var chai = require('chai'); 14 | global.expect = chai.expect; 15 | 16 | 17 | var paths = { 18 | libJsFiles: ['./lib/**/*.js', './errors.js'], 19 | specFiles: './test/spec/**/*.js', 20 | fixtureFiles: './test/fixtures/**/*.js', 21 | gulpfile: './gulpfile.js', 22 | eslintrc: './.eslintrc.json' 23 | }; 24 | 25 | 26 | gulp.task('dev', ['watch', 'validate']); 27 | 28 | gulp.task('watch', function () { 29 | 30 | gulp.watch(_.flatten([ 31 | paths.libJsFiles, 32 | paths.specFiles, 33 | paths.fixtureFiles, 34 | paths.gulpfile 35 | ]), [ 36 | 'validate' 37 | ]); 38 | 39 | gulp.watch(_.flatten([ 40 | paths.eslintrc 41 | ]), [ 42 | 'lint' 43 | ]); 44 | 45 | }); 46 | 47 | gulp.task('validate', function (done) { 48 | runSequence('lint', 'test', done); 49 | }); 50 | 51 | gulp.task('lint', function () { 52 | 53 | return gulp.src(_.flatten([ 54 | paths.libJsFiles, 55 | paths.gulpfile, 56 | paths.specFiles, 57 | paths.fixtureFiles, 58 | paths.gulpfile 59 | ])) 60 | .pipe(eslint()) 61 | .pipe(eslint.format()) 62 | .pipe(eslint.failAfterError()); 63 | 64 | }); 65 | 66 | gulp.task('test', ['clean'], function (done) { 67 | 68 | var coverageVariable = '$$cov_' + new Date().getTime() + '$$'; 69 | 70 | gulp.src(paths.libJsFiles) 71 | .pipe(istanbul({ 72 | coverageVariable: coverageVariable 73 | })) 74 | .pipe(istanbul.hookRequire()) 75 | .on('finish', function () { 76 | 77 | gulp.src(paths.specFiles) 78 | .pipe(mocha()) 79 | .on('error', function (err) { 80 | console.error(String(err)); 81 | console.error(chalk.bold.bgRed(' TESTS FAILED ')); 82 | done(new Error(' TESTS FAILED ')); 83 | }) 84 | .pipe(istanbul.writeReports({ 85 | reporters: ['lcov'], 86 | coverageVariable: coverageVariable 87 | })) 88 | .on('end', done); 89 | 90 | }); 91 | 92 | }); 93 | 94 | gulp.task('test-without-coverage', function () { 95 | 96 | return gulp.src(paths.specFiles) 97 | .pipe(mocha()) 98 | .on('error', function () { 99 | console.log(chalk.bold.bgRed(' TESTS FAILED ')); 100 | }); 101 | 102 | }); 103 | 104 | gulp.task('clean', ['clean-coverage']); 105 | 106 | gulp.task('clean-coverage', function (done) { 107 | rimraf('./coverage', done); 108 | }); 109 | 110 | gulp.task('ci', function (done) { 111 | runSequence('validate', 'coveralls', 'test-without-coverage', done); 112 | }); 113 | 114 | gulp.task('ci-no-cov', function (done) { 115 | runSequence('validate', 'test-without-coverage', done); 116 | }); 117 | 118 | gulp.task('coveralls', function () { 119 | return gulp.src('coverage/**/lcov.info') 120 | .pipe(coveralls()); 121 | }); 122 | -------------------------------------------------------------------------------- /lib/rp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var configure = require('request-promise-core/configure/request2'), 4 | stealthyRequire = require('stealthy-require'); 5 | 6 | // Load Request freshly - so that users can require an unaltered request instance! 7 | var request = stealthyRequire(require.cache, function () { 8 | return require('request'); 9 | }, 10 | function () { 11 | require('tough-cookie'); 12 | }, module); 13 | 14 | 15 | configure({ 16 | request: request, 17 | PromiseImpl: Promise, 18 | expose: [ 19 | 'then', 20 | 'catch', 21 | 'promise' 22 | ] 23 | }); 24 | 25 | 26 | module.exports = request; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "request-promise-native", 3 | "version": "1.0.9", 4 | "description": "The simplified HTTP request client 'request' with Promise support. Powered by native ES6 promises.", 5 | "keywords": [ 6 | "xhr", 7 | "http", 8 | "https", 9 | "promise", 10 | "request", 11 | "then", 12 | "thenable", 13 | "native" 14 | ], 15 | "main": "./lib/rp.js", 16 | "scripts": { 17 | "test": "./node_modules/.bin/gulp ci", 18 | "test-publish": "./node_modules/.bin/gulp ci-no-cov", 19 | "publish-please": "publish-please", 20 | "prepublish": "publish-please guard" 21 | }, 22 | "repository": { 23 | "type": "git", 24 | "url": "git+https://github.com/request/request-promise-native.git" 25 | }, 26 | "author": "Nicolai Kamenzky (https://github.com/analog-nico)", 27 | "license": "ISC", 28 | "bugs": { 29 | "url": "https://github.com/request/request-promise-native/issues" 30 | }, 31 | "homepage": "https://github.com/request/request-promise-native#readme", 32 | "engines": { 33 | "node": ">=0.12.0" 34 | }, 35 | "dependencies": { 36 | "request-promise-core": "1.1.4", 37 | "stealthy-require": "^1.1.1", 38 | "tough-cookie": "^2.3.3" 39 | }, 40 | "peerDependencies": { 41 | "request": "^2.34" 42 | }, 43 | "devDependencies": { 44 | "body-parser": "~1.15.2", 45 | "chai": "~3.5.0", 46 | "chalk": "~1.1.3", 47 | "gulp": "~3.9.1", 48 | "gulp-coveralls": "~0.1.4", 49 | "gulp-eslint": "~2.1.0", 50 | "gulp-istanbul": "~1.0.0", 51 | "gulp-mocha": "~2.2.0", 52 | "lodash": "~4.13.1", 53 | "publish-please": "~2.1.4", 54 | "request": "^2.34.0", 55 | "rimraf": "~2.5.3", 56 | "run-sequence": "~1.2.2" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "mocha": true 4 | }, 5 | "globals": { 6 | "expect": true 7 | }, 8 | "rules": { 9 | 10 | // Node.js and CommonJS 11 | 12 | "no-process-env": 0, 13 | "no-sync": 0 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/fixtures/require/afterwards.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var rp = require('../../../'); 4 | var request = require('request'); 5 | 6 | var rpHasThen = rp('http://localhost:4000/200').then !== undefined; 7 | var requestHasNoThen = request('http://localhost:4000/200').then === undefined; 8 | 9 | console.log('rp: ' + rpHasThen + ', request: ' + requestHasNoThen); 10 | -------------------------------------------------------------------------------- /test/fixtures/require/beforehand.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var request = require('request'); 4 | var rp = require('../../../'); 5 | 6 | var requestHasNoThen = request('http://localhost:4000/200').then === undefined; 7 | var rpHasThen = rp('http://localhost:4000/200').then !== undefined; 8 | 9 | console.log('request: ' + requestHasNoThen + ', rp: ' + rpHasThen); 10 | -------------------------------------------------------------------------------- /test/fixtures/require/beforehandAndAfterwards.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var request1 = require('request'); 4 | var rp = require('../../../'); 5 | var request2 = require('request'); 6 | 7 | var request1HasNoThen = request1('http://localhost:4000/200').then === undefined; 8 | var rpHasThen = rp('http://localhost:4000/200').then !== undefined; 9 | var request2IsIdenticalToRequest1 = request2 === request1; 10 | 11 | console.log('request1: ' + request1HasNoThen + ', rp: ' + rpHasThen + ', request2: ' + request2IsIdenticalToRequest1); 12 | -------------------------------------------------------------------------------- /test/fixtures/server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var bodyParser = require('body-parser'); 4 | var http = require('http'); 5 | var url = require('url'); 6 | 7 | 8 | module.exports = function startServer(port, cb) { 9 | 10 | var server = http.createServer(function (req, res) { 11 | 12 | bodyParser.json()(req, res, function () { 13 | 14 | var path = url.parse(req.url).pathname; 15 | var status = Number(path.split('?')[0].split('/')[1]); 16 | 17 | switch (status) { 18 | case 301: 19 | res.writeHead(301, { Location: '/200' }); 20 | res.end(); 21 | break; 22 | default: 23 | res.writeHead(status, {'Content-Type': 'text/plain'}); 24 | var body = req.method === 'POST' ? ' - ' + JSON.stringify(req.body) : ''; 25 | res.end(req.method + ' ' + path + body); 26 | } 27 | 28 | }); 29 | 30 | }); 31 | 32 | server.listen(port, function () { 33 | 34 | cb(function stopServer(done) { 35 | // Wait for all requests to finish since they may produce unhandled errors for tests at the end that don't wait themselves. 36 | setTimeout(function () { 37 | server.close(); 38 | done(); 39 | }, 20); 40 | }); 41 | 42 | }); 43 | 44 | }; 45 | -------------------------------------------------------------------------------- /test/spec/request-test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var childProcess = require('child_process'), 4 | errors = require('../../errors'), 5 | path = require('path'), 6 | rp = require('../../'), 7 | tough = require('tough-cookie'), 8 | startServer = require('../fixtures/server.js'); 9 | 10 | 11 | describe('Request-Promise-Native', function () { 12 | 13 | var stopServer = null; 14 | 15 | before(function (done) { 16 | 17 | startServer(4000, function (stop) { 18 | stopServer = stop; 19 | done(); 20 | }); 21 | 22 | }); 23 | 24 | after(function (done) { 25 | 26 | stopServer(done); 27 | 28 | }); 29 | 30 | describe('should expose', function () { 31 | 32 | it('.then(...)', function (done) { 33 | 34 | rp('http://localhost:4000/200') 35 | .then(function (body) { 36 | expect(body).to.eql('GET /200'); 37 | done(); 38 | }) 39 | .catch(function (err) { 40 | done(err); 41 | }); 42 | 43 | }); 44 | 45 | it('.catch(...) and the error types', function (done) { 46 | 47 | rp('http://localhost:4000/404') 48 | .catch(function (err) { 49 | expect(err instanceof errors.StatusCodeError).to.eql(true); 50 | return 'catch called'; 51 | }) 52 | .then(function (info) { 53 | expect(info).to.eql('catch called'); 54 | done(); 55 | }) 56 | .catch(function (err) { 57 | done(err); 58 | }); 59 | 60 | }); 61 | 62 | it('.promise() returning a native ES6 promise', function () { 63 | 64 | var p = rp('http://localhost:4000/200').promise(); 65 | 66 | expect(p instanceof Promise).to.eql(true); 67 | 68 | }); 69 | 70 | }); 71 | 72 | describe('should still allow to require Request independently', function () { 73 | 74 | it('by not interfering with Request required afterwards', function (done) { 75 | 76 | childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/afterwards.js'), function (err, stdout, stderr) { 77 | 78 | if (err) { 79 | done(err); 80 | return; 81 | } 82 | 83 | try { 84 | expect(stdout, 'Actual stdout: ' + stdout).to.contain('rp: true, request: true'); 85 | done(); 86 | } catch (e) { 87 | done(e); 88 | } 89 | 90 | }); 91 | 92 | }); 93 | 94 | it('by not interfering with Request required beforehand', function (done) { 95 | 96 | childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/beforehand.js'), function (err, stdout, stderr) { 97 | 98 | if (err) { 99 | done(err); 100 | return; 101 | } 102 | 103 | try { 104 | expect(stdout, 'Actual stdout: ' + stdout).to.contain('request: true, rp: true'); 105 | done(); 106 | } catch (e) { 107 | done(e); 108 | } 109 | 110 | }); 111 | 112 | }); 113 | 114 | it('by not interfering with Request required beforehand and afterwards being identical', function (done) { 115 | 116 | childProcess.exec('node ' + path.join(__dirname, '../fixtures/require/beforehandAndAfterwards.js'), function (err, stdout, stderr) { 117 | 118 | if (err) { 119 | done(err); 120 | return; 121 | } 122 | 123 | try { 124 | expect(stdout, 'Actual stdout: ' + stdout).to.contain('request1: true, rp: true, request2: true'); 125 | done(); 126 | } catch (e) { 127 | done(e); 128 | } 129 | 130 | }); 131 | 132 | }); 133 | 134 | }); 135 | 136 | it('should allow the use of tough-cookie - issue request-promise#183', function () { 137 | 138 | var sessionCookie = new tough.Cookie({ 139 | key: 'some_key', 140 | value: 'some_value', 141 | domain: 'api.mydomain.com', 142 | httpOnly: true, 143 | maxAge: 31536000 144 | }); 145 | 146 | var cookiejar = rp.jar(); 147 | 148 | expect(function () { 149 | cookiejar.setCookie(sessionCookie.toString(), 'https://api.mydomain.com'); 150 | }).to.not.throw(); 151 | 152 | }); 153 | 154 | }); 155 | --------------------------------------------------------------------------------