|
| 1 | +/**! |
| 2 | + * urllib - openssl.test.js |
| 3 | + * |
| 4 | + * Copyright(c) node-modules and other contributors. |
| 5 | + * MIT Licensed |
| 6 | + * |
| 7 | + * Authors: |
| 8 | + * P.S.V.R <pmq2001@gmail.com> (http://www.ofpsvr.com) |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +/** |
| 14 | + * This is a test for the bug of unwanted openSSL errors. |
| 15 | + */ |
| 16 | + |
| 17 | +var should = require('should'); |
| 18 | +var urllib = require('../'); |
| 19 | +var crypto = require('crypto'); |
| 20 | +var fs = require('fs'); |
| 21 | +var path = require('path'); |
| 22 | +var Agent = require('agentkeepalive'); |
| 23 | +var HttpsAgent = require('agentkeepalive').HttpsAgent; |
| 24 | + |
| 25 | +var bad_sign = function () { |
| 26 | + // This triggers an implicit error |
| 27 | + // that is recorded onto OpenSSL's error stack |
| 28 | + var privateKey = fs.readFileSync(path.join(__dirname, './fixtures/bad_rsa_privkey.pem'), 'utf8'); |
| 29 | + var s = crypto.createSign('sha1'); |
| 30 | + var sign = s.sign(privateKey); |
| 31 | +}; |
| 32 | + |
| 33 | +describe('httpclient.test.js', function () { |
| 34 | + it('should requestThunk()', function (done) { |
| 35 | + var conf = { |
| 36 | + "keepAlive": true, |
| 37 | + "keepAliveTimeout": 300000, |
| 38 | + "timeout": 300000, |
| 39 | + "maxSockets": null, |
| 40 | + "maxFreeSockets": 10, |
| 41 | + "enableStatusLog": false |
| 42 | + }; |
| 43 | + |
| 44 | + var httpAgent = new Agent(conf); |
| 45 | + var httpsAgent = new HttpsAgent(conf); |
| 46 | + |
| 47 | + var client = urllib.create({ |
| 48 | + agent: httpAgent, |
| 49 | + httpsAgent: httpsAgent |
| 50 | + }); |
| 51 | + |
| 52 | + bad_sign(); |
| 53 | + // 1st time when we establish a https connection, |
| 54 | + // ~ClearErrorOnReturn is called so the errors of bad_sign() won't be thrown |
| 55 | + client.requestThunk('https://www.alipay.com')(function (err, result) { |
| 56 | + should.not.exist(err); |
| 57 | + result.data.should.be.a.Buffer; |
| 58 | + result.status.should.equal(200); |
| 59 | + |
| 60 | + setImmediate(function () { |
| 61 | + // 2nd time when we reuse an existing connection, |
| 62 | + // ~ClearErrorOnReturn will not be called, |
| 63 | + // making the errors of bad_sign() be thrown |
| 64 | + // A fix of this bug should prevent this from happening |
| 65 | + bad_sign(); |
| 66 | + client.requestThunk('https://www.alipay.com')(function (err, result) { |
| 67 | + should.not.exist(err); |
| 68 | + result.data.should.be.a.Buffer; |
| 69 | + result.status.should.equal(200); |
| 70 | + done(); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments