From d61333015f6695d6a9fbe883b500cc9221c6e161 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Wed, 10 Dec 2014 23:04:42 +0500 Subject: [PATCH] Migration from mocha to tape --- package.json | 5 +- test.js | 187 ----------------------------------------- test/server.js | 42 +++++++++ test/ssl/ca/ca.cnf | 20 +++++ test/ssl/ca/ca.crl | 0 test/ssl/ca/ca.crt | 17 ++++ test/ssl/ca/ca.csr | 13 +++ test/ssl/ca/ca.key | 18 ++++ test/ssl/ca/ca.srl | 1 + test/ssl/ca/server.cnf | 19 +++++ test/ssl/ca/server.crt | 16 ++++ test/ssl/ca/server.csr | 11 +++ test/ssl/ca/server.key | 9 ++ test/ssl/npm-ca.crt | 16 ++++ test/ssl/test.crt | 15 ++++ test/ssl/test.key | 15 ++++ test/test-gzip.js | 42 +++++++++ test/test-http.js | 88 +++++++++++++++++++ test/test-https.js | 42 +++++++++ test/test-post.js | 50 +++++++++++ test/test-redirects.js | 52 ++++++++++++ 21 files changed, 489 insertions(+), 189 deletions(-) delete mode 100644 test.js create mode 100644 test/server.js create mode 100644 test/ssl/ca/ca.cnf create mode 100644 test/ssl/ca/ca.crl create mode 100644 test/ssl/ca/ca.crt create mode 100644 test/ssl/ca/ca.csr create mode 100644 test/ssl/ca/ca.key create mode 100644 test/ssl/ca/ca.srl create mode 100644 test/ssl/ca/server.cnf create mode 100644 test/ssl/ca/server.crt create mode 100644 test/ssl/ca/server.csr create mode 100644 test/ssl/ca/server.key create mode 100644 test/ssl/npm-ca.crt create mode 100644 test/ssl/test.crt create mode 100644 test/ssl/test.key create mode 100644 test/test-gzip.js create mode 100644 test/test-http.js create mode 100644 test/test-https.js create mode 100644 test/test-post.js create mode 100644 test/test-redirects.js diff --git a/package.json b/package.json index 3f688d9..6943205 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=0.10.0" }, "scripts": { - "test": "mocha --timeout 50000" + "test": "taper test/test-*.js" }, "files": [ "index.js" @@ -37,6 +37,7 @@ "timed-out": "^2.0.0" }, "devDependencies": { - "mocha": "*" + "tape": "^3.0.3", + "taper": "^0.3.0" } } diff --git a/test.js b/test.js deleted file mode 100644 index d82e4b4..0000000 --- a/test.js +++ /dev/null @@ -1,187 +0,0 @@ -/* global describe, it, before, after */ -'use strict'; -var assert = require('assert'); -var http = require('http'); -var got = require('./'); - -it('should do HTTP request', function (done) { - got('http://google.com', function (err, data) { - if (err) { - console.error(err); - assert(false); - return; - } - - assert(/google/.test(data)); - done(); - }); -}); - -it('should do HTTPS request', function (done) { - got('https://google.com', function (err, data) { - if (err) { - console.error(err); - assert(false); - return; - } - - assert(/google/.test(data)); - done(); - }); -}); - -it('should should return status code as error code and response object when not 200', function (done) { - got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data, res) { - assert.ok(res.headers); - assert.strictEqual(err.code, 404); - assert.ok(//.test(data)); - done(); - }); -}); - -it('should support optional options', function (done) { - got('http://sindresorhus.com', {method: 'HEAD'}, function (err, data) { - assert(!err, err); - assert(!data, data); - done(); - }); -}); - -it('should get headers only with HEAD method', function (done) { - got('http://google.com', {method: 'HEAD'}, function (err, data, res) { - assert(!data, data); - assert.ok(res.headers); - done(); - }); -}); - -it('should support gzip', function (done) { - got('http://sindresorhus.com', function (err, data) { - assert(!err, err); - assert(/^/.test(data)); - done(); - }); -}); - -it('should return a buffer if encoding is set to null', function (done) { - got('http://google.com', {encoding: null}, function (err, data) { - assert(!err, err); - assert.ok(Buffer.isBuffer(data)); - done(); - }); -}); - -it('should return a readable stream without a callback', function (done) { - var stream = got('http://google.com'); - - var data = ''; - stream.on('data', function (chunk) { - data += chunk; - }); - stream.on('end', function () { - assert.ok(/google/.test(data)); - done(); - }); -}); - -it('should proxy errors to the stream', function (done) { - var stream = got('http://sindresorhus.com/sfsadfasdfadsga'); - - stream.on('error', function (error) { - assert.strictEqual(error.code, 404); - done(); - }); -}); - -it('should support timeout option', function (done) { - var stream = got('http://sindresorhus.com/', {timeout: 1}); - - stream.on('error', function (error) { - assert.strictEqual(error.code, 'ETIMEDOUT'); - done(); - }); -}); - -describe('POST', function () { - var server; - - before(function (done) { - server = http.createServer(function (req, res) { - req.pipe(res); - }); - server.listen(8081, done); - }); - - after(function (done) { - server.close(done); - }); - - it('should support string as body option', function (done) { - got('http://0.0.0.0:8081', {body: 'string'}, function (err, data) { - assert.ifError(err); - assert.equal(data, 'string'); - done(); - }); - }); - - it('should support Buffer as body option', function (done) { - got('http://0.0.0.0:8081', {body: new Buffer('string')}, function (err, data) { - assert.ifError(err); - assert.equal(data, 'string'); - done(); - }); - }); - - it('should take data from options.body in stream mode', function (done) { - got.post('http://0.0.0.0:8081', {body: 'Hello'}) - .on('error', done) - .on('data', function (chunk) { - assert.equal(chunk, 'Hello'); - done(); - }); - }); - - it('should throw an error on options.body and write operations', function (done) { - assert.throws(function () { - got.post('http://0.0.0.0:8081', {body: 'Hello'}) - .end('Hello'); - }, 'got\'s stream is not writable when options.body is used'); - - assert.throws(function () { - got.post('http://0.0.0.0:8081', {body: 'Hello'}) - .write('Hello'); - }, 'got\'s stream is not writable when options.body is used'); - - done(); - }); - - it('should be a writeable stream on POST', function (done) { - got.post('http://0.0.0.0:8081') - .on('error', done) - .on('data', function (chunk) { - assert.equal(chunk, 'Hello'); - done(); - }) - .end('Hello'); - }); - - it('should be a writeable stream on PUT', function (done) { - got.put('http://0.0.0.0:8081') - .on('error', done) - .on('data', function (chunk) { - assert.equal(chunk, 'Hello'); - done(); - }) - .end('Hello'); - }); - - it('should be a writeable stream on PATCH', function (done) { - got.patch('http://0.0.0.0:8081') - .on('error', done) - .on('data', function (chunk) { - assert.equal(chunk, 'Hello'); - done(); - }) - .end('Hello'); - }); -}); diff --git a/test/server.js b/test/server.js new file mode 100644 index 0000000..0ea69fb --- /dev/null +++ b/test/server.js @@ -0,0 +1,42 @@ +'use strict'; + +var http = require('http'); +var https = require('https'); +var path = require('path'); +var fs = require('fs'); +var assign = require('object-assign'); + +exports.port = 6767; +exports.portSSL = 16167; + +exports.createServer = function (port) { + port = port || exports.port; + var s = http.createServer(function (req, resp) { + s.emit(req.url, req, resp); + }); + s.port = port; + s.url = 'http://localhost:' + port; + s.protocol = 'http'; + return s; +}; + +exports.createSSLServer = function (port, opts) { + port = port || exports.portSSL; + + var options = assign({ + 'key' : path.join(__dirname, 'ssl', 'test.key'), + 'cert': path.join(__dirname, 'ssl', 'test.crt') + }, opts); + + for (var i in options) { + options[i] = fs.readFileSync(options[i]); + } + + var s = https.createServer(options, function (req, resp) { + s.emit(req.url, req, resp); + }); + s.port = port; + s.url = 'https://localhost:' + port; + s.protocol = 'https'; + return s; +}; diff --git a/test/ssl/ca/ca.cnf b/test/ssl/ca/ca.cnf new file mode 100644 index 0000000..425a889 --- /dev/null +++ b/test/ssl/ca/ca.cnf @@ -0,0 +1,20 @@ +[ req ] +default_bits = 1024 +days = 3650 +distinguished_name = req_distinguished_name +attributes = req_attributes +prompt = no +output_password = password + +[ req_distinguished_name ] +C = US +ST = CA +L = Oakland +O = request +OU = request Certificate Authority +CN = requestCA +emailAddress = mikeal@mikealrogers.com + +[ req_attributes ] +challengePassword = password challenge + diff --git a/test/ssl/ca/ca.crl b/test/ssl/ca/ca.crl new file mode 100644 index 0000000..e69de29 diff --git a/test/ssl/ca/ca.crt b/test/ssl/ca/ca.crt new file mode 100644 index 0000000..b4524e4 --- /dev/null +++ b/test/ssl/ca/ca.crt @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICvTCCAiYCCQDn+P/MSbDsWjANBgkqhkiG9w0BAQUFADCBojELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMRAwDgYDVQQKEwdyZXF1 +ZXN0MSYwJAYDVQQLEx1yZXF1ZXN0IENlcnRpZmljYXRlIEF1dGhvcml0eTESMBAG +A1UEAxMJcmVxdWVzdENBMSYwJAYJKoZIhvcNAQkBFhdtaWtlYWxAbWlrZWFscm9n +ZXJzLmNvbTAeFw0xMjAzMDEyMjUwNTZaFw0yMjAyMjcyMjUwNTZaMIGiMQswCQYD +VQQGEwJVUzELMAkGA1UECBMCQ0ExEDAOBgNVBAcTB09ha2xhbmQxEDAOBgNVBAoT +B3JlcXVlc3QxJjAkBgNVBAsTHXJlcXVlc3QgQ2VydGlmaWNhdGUgQXV0aG9yaXR5 +MRIwEAYDVQQDEwlyZXF1ZXN0Q0ExJjAkBgkqhkiG9w0BCQEWF21pa2VhbEBtaWtl +YWxyb2dlcnMuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7t9pQUAK4 +5XJYTI6NrF0n3G2HZsfN+rPYSVzzL8SuVyb1tHXos+vbPm3NKI4E8X1yVAXU8CjJ +5SqXnp4DAypAhaseho81cbhk7LXUhFz78OvAa+OD+xTAEAnNQ8tGUr4VGyplEjfD +xsBVuqV2j8GPNTftr+drOCFlqfAgMrBn4wIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +ADVdTlVAL45R+PACNS7Gs4o81CwSclukBu4FJbxrkd4xGQmurgfRrYYKjtqiopQm +D7ysRamS3HMN9/VKq2T7r3z1PMHPAy7zM4uoXbbaTKwlnX4j/8pGPn8Ca3qHXYlo +88L/OOPc6Di7i7qckS3HFbXQCTiULtxWmy97oEuTwrAj +-----END CERTIFICATE----- diff --git a/test/ssl/ca/ca.csr b/test/ssl/ca/ca.csr new file mode 100644 index 0000000..e48c56e --- /dev/null +++ b/test/ssl/ca/ca.csr @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICBjCCAW8CAQAwgaIxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEQMA4GA1UE +BxMHT2FrbGFuZDEQMA4GA1UEChMHcmVxdWVzdDEmMCQGA1UECxMdcmVxdWVzdCBD +ZXJ0aWZpY2F0ZSBBdXRob3JpdHkxEjAQBgNVBAMTCXJlcXVlc3RDQTEmMCQGCSqG +SIb3DQEJARYXbWlrZWFsQG1pa2VhbHJvZ2Vycy5jb20wgZ8wDQYJKoZIhvcNAQEB +BQADgY0AMIGJAoGBALu32lBQArjlclhMjo2sXSfcbYdmx836s9hJXPMvxK5XJvW0 +deiz69s+bc0ojgTxfXJUBdTwKMnlKpeengMDKkCFqx6GjzVxuGTstdSEXPvw68Br +44P7FMAQCc1Dy0ZSvhUbKmUSN8PGwFW6pXaPwY81N+2v52s4IWWp8CAysGfjAgMB +AAGgIzAhBgkqhkiG9w0BCQcxFBMScGFzc3dvcmQgY2hhbGxlbmdlMA0GCSqGSIb3 +DQEBBQUAA4GBAGJO7grHeVHXetjHEK8urIxdnvfB2qeZeObz4GPKIkqUurjr0rfj +bA3EK1kDMR5aeQWR8RunixdM16Q6Ry0lEdLVWkdSwRN9dmirIHT9cypqnD/FYOia +SdezZ0lUzXgmJIwRYRwB1KSMMocIf52ll/xC2bEGg7/ZAEuAyAgcZV3X +-----END CERTIFICATE REQUEST----- diff --git a/test/ssl/ca/ca.key b/test/ssl/ca/ca.key new file mode 100644 index 0000000..a53e7f7 --- /dev/null +++ b/test/ssl/ca/ca.key @@ -0,0 +1,18 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,C8B5887048377F02 + +nyD5ZH0Wup2uWsDvurq5mKDaDrf8lvNn9w0SH/ZkVnfR1/bkwqrFriqJWvZNUG+q +nS0iBYczsWLJnbub9a1zLOTENWUKVD5uqbC3aGHhnoUTNSa27DONgP8gHOn6JgR+ +GAKo01HCSTiVT4LjkwN337QKHnMP2fTzg+IoC/CigvMcq09hRLwU1/guq0GJKGwH +gTxYNuYmQC4Tjh8vdS4liF+Ve/P3qPR2CehZrIOkDT8PHJBGQJRo4xGUIB7Tpk38 +VCk+UZ0JCS2coY8VkY/9tqFJp/ZnnQQVmaNbdRqg7ECKL+bXnNo7yjzmazPZmPe3 +/ShbE0+CTt7LrjCaQAxWbeDzqfo1lQfgN1LulTm8MCXpQaJpv7v1VhIhQ7afjMYb +4thW/ypHPiYS2YJCAkAVlua9Oxzzh1qJoh8Df19iHtpd79Q77X/qf+1JvITlMu0U +gi7yEatmQcmYNws1mtTC1q2DXrO90c+NZ0LK/Alse6NRL/xiUdjug2iHeTf/idOR +Gg/5dSZbnnlj1E5zjSMDkzg6EHAFmHV4jYGSAFLEQgp4V3ZhMVoWZrvvSHgKV/Qh +FqrAK4INr1G2+/QTd09AIRzfy3/j6yD4A9iNaOsEf9Ua7Qh6RcALRCAZTWR5QtEf +dX+iSNJ4E85qXs0PqwkMDkoaxIJ+tmIRJY7y8oeylV8cfGAi8Soubt/i3SlR8IHC +uDMas/2OnwafK3N7ODeE1i7r7wkzQkSHaEz0TrF8XRnP25jAICCSLiMdAAjKfxVb +EvzsFSuAy3Jt6bU3hSLY9o4YVYKE+68ITMv9yNjvTsEiW+T+IbN34w== +-----END RSA PRIVATE KEY----- diff --git a/test/ssl/ca/ca.srl b/test/ssl/ca/ca.srl new file mode 100644 index 0000000..17128db --- /dev/null +++ b/test/ssl/ca/ca.srl @@ -0,0 +1 @@ +ADF62016AA40C9C3 diff --git a/test/ssl/ca/server.cnf b/test/ssl/ca/server.cnf new file mode 100644 index 0000000..cd1fd1e --- /dev/null +++ b/test/ssl/ca/server.cnf @@ -0,0 +1,19 @@ +[ req ] +default_bits = 1024 +days = 3650 +distinguished_name = req_distinguished_name +attributes = req_attributes +prompt = no + +[ req_distinguished_name ] +C = US +ST = CA +L = Oakland +O = request +OU = testing +CN = testing.request.mikealrogers.com +emailAddress = mikeal@mikealrogers.com + +[ req_attributes ] +challengePassword = password challenge + diff --git a/test/ssl/ca/server.crt b/test/ssl/ca/server.crt new file mode 100644 index 0000000..efe96ce --- /dev/null +++ b/test/ssl/ca/server.crt @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE----- +MIICejCCAeMCCQCt9iAWqkDJwzANBgkqhkiG9w0BAQUFADCBojELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMRAwDgYDVQQKEwdyZXF1 +ZXN0MSYwJAYDVQQLEx1yZXF1ZXN0IENlcnRpZmljYXRlIEF1dGhvcml0eTESMBAG +A1UEAxMJcmVxdWVzdENBMSYwJAYJKoZIhvcNAQkBFhdtaWtlYWxAbWlrZWFscm9n +ZXJzLmNvbTAeFw0xMjAzMDEyMjUwNTZaFw0yMjAyMjcyMjUwNTZaMIGjMQswCQYD +VQQGEwJVUzELMAkGA1UECBMCQ0ExEDAOBgNVBAcTB09ha2xhbmQxEDAOBgNVBAoT +B3JlcXVlc3QxEDAOBgNVBAsTB3Rlc3RpbmcxKTAnBgNVBAMTIHRlc3RpbmcucmVx +dWVzdC5taWtlYWxyb2dlcnMuY29tMSYwJAYJKoZIhvcNAQkBFhdtaWtlYWxAbWlr +ZWFscm9nZXJzLmNvbTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDgVl0jMumvOpmM +20W5v9yhGgZj8hPhEQF/N7yCBVBn/rWGYm70IHC8T/pR5c0LkWc5gdnCJEvKWQjh +DBKxZD8FAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEABShRkNgFbgs4vUWW9R9deNJj +7HJoiTmvkmoOC7QzcYkjdgHbOxsSq3rBnwxsVjY9PAtPwBn0GRspOeG7KzKRgySB +kb22LyrCFKbEOfKO/+CJc80ioK9zEPVjGsFMyAB+ftYRqM+s/4cQlTg/m89l01wC +yapjN3RxZbInGhWR+jA= +-----END CERTIFICATE----- diff --git a/test/ssl/ca/server.csr b/test/ssl/ca/server.csr new file mode 100644 index 0000000..a8e7595 --- /dev/null +++ b/test/ssl/ca/server.csr @@ -0,0 +1,11 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIBgjCCASwCAQAwgaMxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEQMA4GA1UE +BxMHT2FrbGFuZDEQMA4GA1UEChMHcmVxdWVzdDEQMA4GA1UECxMHdGVzdGluZzEp +MCcGA1UEAxMgdGVzdGluZy5yZXF1ZXN0Lm1pa2VhbHJvZ2Vycy5jb20xJjAkBgkq +hkiG9w0BCQEWF21pa2VhbEBtaWtlYWxyb2dlcnMuY29tMFwwDQYJKoZIhvcNAQEB +BQADSwAwSAJBAOBWXSMy6a86mYzbRbm/3KEaBmPyE+ERAX83vIIFUGf+tYZibvQg +cLxP+lHlzQuRZzmB2cIkS8pZCOEMErFkPwUCAwEAAaAjMCEGCSqGSIb3DQEJBzEU +ExJwYXNzd29yZCBjaGFsbGVuZ2UwDQYJKoZIhvcNAQEFBQADQQBD3E5WekQzCEJw +7yOcqvtPYIxGaX8gRKkYfLPoj3pm3GF5SGqtJKhylKfi89szHXgktnQgzff9FN+A +HidVJ/3u +-----END CERTIFICATE REQUEST----- diff --git a/test/ssl/ca/server.key b/test/ssl/ca/server.key new file mode 100644 index 0000000..72d8698 --- /dev/null +++ b/test/ssl/ca/server.key @@ -0,0 +1,9 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIBOwIBAAJBAOBWXSMy6a86mYzbRbm/3KEaBmPyE+ERAX83vIIFUGf+tYZibvQg +cLxP+lHlzQuRZzmB2cIkS8pZCOEMErFkPwUCAwEAAQJAK+r8ZM2sze8s7FRo/ApB +iRBtO9fCaIdJwbwJnXKo4RKwZDt1l2mm+fzZ+/QaQNjY1oTROkIIXmnwRvZWfYlW +gQIhAPKYsG+YSBN9o8Sdp1DMyZ/rUifKX3OE6q9tINkgajDVAiEA7Ltqh01+cnt0 +JEnud/8HHcuehUBLMofeg0G+gCnSbXECIQCqDvkXsWNNLnS/3lgsnvH0Baz4sbeJ +rjIpuVEeg8eM5QIgbu0+9JmOV6ybdmmiMV4yAncoF35R/iKGVHDZCAsQzDECIQDZ +0jGz22tlo5YMcYSqrdD3U4sds1pwiAaWFRbCunoUJw== +-----END RSA PRIVATE KEY----- diff --git a/test/ssl/npm-ca.crt b/test/ssl/npm-ca.crt new file mode 100644 index 0000000..fde2fe9 --- /dev/null +++ b/test/ssl/npm-ca.crt @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE----- +MIIChzCCAfACCQDauvz/KHp8ejANBgkqhkiG9w0BAQUFADCBhzELMAkGA1UEBhMC +VVMxCzAJBgNVBAgTAkNBMRAwDgYDVQQHEwdPYWtsYW5kMQwwCgYDVQQKEwNucG0x +IjAgBgNVBAsTGW5wbSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkxDjAMBgNVBAMTBW5w +bUNBMRcwFQYJKoZIhvcNAQkBFghpQGl6cy5tZTAeFw0xMTA5MDUwMTQ3MTdaFw0y +MTA5MDIwMTQ3MTdaMIGHMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEDAOBgNV +BAcTB09ha2xhbmQxDDAKBgNVBAoTA25wbTEiMCAGA1UECxMZbnBtIENlcnRpZmlj +YXRlIEF1dGhvcml0eTEOMAwGA1UEAxMFbnBtQ0ExFzAVBgkqhkiG9w0BCQEWCGlA +aXpzLm1lMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLI4tIqPpRW+ACw9GE +OgBlJZwK5f8nnKCLK629Pv5yJpQKs3DENExAyOgDcyaF0HD0zk8zTp+ZsLaNdKOz +Gn2U181KGprGKAXP6DU6ByOJDWmTlY6+Ad1laYT0m64fERSpHw/hjD3D+iX4aMOl +y0HdbT5m1ZGh6SJz3ZqxavhHLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAC4ySDbC +l7W1WpLmtLGEQ/yuMLUf6Jy/vr+CRp4h+UzL+IQpCv8FfxsYE7dhf/bmWTEupBkv +yNL18lipt2jSvR3v6oAHAReotvdjqhxddpe5Holns6EQd1/xEZ7sB1YhQKJtvUrl +ZNufy1Jf1r0ldEGeA+0ISck7s+xSh9rQD2Op +-----END CERTIFICATE----- diff --git a/test/ssl/test.crt b/test/ssl/test.crt new file mode 100644 index 0000000..b357f86 --- /dev/null +++ b/test/ssl/test.crt @@ -0,0 +1,15 @@ +-----BEGIN CERTIFICATE----- +MIICQzCCAawCCQCO/XWtRFck1jANBgkqhkiG9w0BAQUFADBmMQswCQYDVQQGEwJU +SDEQMA4GA1UECBMHQmFuZ2tvazEOMAwGA1UEBxMFU2lsb20xGzAZBgNVBAoTElRo +ZSBSZXF1ZXN0IE1vZHVsZTEYMBYGA1UEAxMPcmVxdWVzdC5leGFtcGxlMB4XDTEx +MTIwMzAyMjkyM1oXDTIxMTEzMDAyMjkyM1owZjELMAkGA1UEBhMCVEgxEDAOBgNV +BAgTB0Jhbmdrb2sxDjAMBgNVBAcTBVNpbG9tMRswGQYDVQQKExJUaGUgUmVxdWVz +dCBNb2R1bGUxGDAWBgNVBAMTD3JlcXVlc3QuZXhhbXBsZTCBnzANBgkqhkiG9w0B +AQEFAAOBjQAwgYkCgYEAwmctddZqlA48+NXs0yOy92DijcQV1jf87zMiYAIlNUto +wghVbTWgJU5r0pdKrD16AptnWJTzKanhItEX8XCCPgsNkq1afgTtJP7rNkwu3xcj +eIMkhJg/ay4ZnkbnhYdsii5VTU5prix6AqWRAhbkBgoA+iVyHyof8wvZyKBoFTMC +AwEAATANBgkqhkiG9w0BAQUFAAOBgQB6BybMJbpeiABgihDfEVBcAjDoQ8gUMgwV +l4NulugfKTDmArqnR9aPd4ET5jX5dkMP4bwCHYsvrcYDeWEQy7x5WWuylOdKhua4 +L4cEi2uDCjqEErIG3cc1MCOk6Cl6Ld6tkIzQSf953qfdEACRytOeUqLNQcrXrqeE +c7U8F6MWLQ== +-----END CERTIFICATE----- diff --git a/test/ssl/test.key b/test/ssl/test.key new file mode 100644 index 0000000..b85810d --- /dev/null +++ b/test/ssl/test.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXgIBAAKBgQDCZy111mqUDjz41ezTI7L3YOKNxBXWN/zvMyJgAiU1S2jCCFVt +NaAlTmvSl0qsPXoCm2dYlPMpqeEi0RfxcII+Cw2SrVp+BO0k/us2TC7fFyN4gySE +mD9rLhmeRueFh2yKLlVNTmmuLHoCpZECFuQGCgD6JXIfKh/zC9nIoGgVMwIDAQAB +AoGBALXFwfUf8vHTSmGlrdZS2AGFPvEtuvldyoxi9K5u8xmdFCvxnOcLsF2RsTHt +Mu5QYWhUpNJoG+IGLTPf7RJdj/kNtEs7xXqWy4jR36kt5z5MJzqiK+QIgiO9UFWZ +fjUb6oeDnTIJA9YFBdYi97MDuL89iU/UK3LkJN3hd4rciSbpAkEA+MCkowF5kSFb +rkOTBYBXZfiAG78itDXN6DXmqb9XYY+YBh3BiQM28oxCeQYyFy6pk/nstnd4TXk6 +V/ryA2g5NwJBAMgRKTY9KvxJWbESeMEFe2iBIV0c26/72Amgi7ZKUCLukLfD4tLF ++WSZdmTbbqI1079YtwaiOVfiLm45Q/3B0eUCQAaQ/0eWSGE+Yi8tdXoVszjr4GXb +G81qBi91DMu6U1It+jNfIba+MPsiHLcZJMVb4/oWBNukN7bD1nhwFWdlnu0CQQCf +Is9WHkdvz2RxbZDxb8verz/7kXXJQJhx5+rZf7jIYFxqX3yvTNv3wf2jcctJaWlZ +fVZwB193YSivcgt778xlAkEAprYUz3jczjF5r2hrgbizPzPDR94tM5BTO3ki2v3w +kbf+j2g7FNAx6kZiVN8XwfLc8xEeUGiPKwtq3ddPDFh17w== +-----END RSA PRIVATE KEY----- diff --git a/test/test-gzip.js b/test/test-gzip.js new file mode 100644 index 0000000..abf7098 --- /dev/null +++ b/test/test-gzip.js @@ -0,0 +1,42 @@ +'use strict'; + +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); +var zlib = require('zlib'); + +var s = server.createServer(); +var testContent = 'Compressible response content.\n'; + +s.on('/', function (req, res) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + + if (/\bgzip\b/i.test(req.headers['accept-encoding'])) { + res.setHeader('Content-Encoding', 'gzip'); + zlib.gzip(testContent, function (err, data) { + res.end(data); + }); + } else { + res.end(testContent); + } +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('ungzip content', function (t) { + got(s.url, function (err, data) { + t.error(err); + t.equal(data, testContent); + t.end(); + }); +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +}); diff --git a/test/test-http.js b/test/test-http.js new file mode 100644 index 0000000..d3ad7ec --- /dev/null +++ b/test/test-http.js @@ -0,0 +1,88 @@ +'use strict'; + +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); + +var s = server.createServer(); + +s.on('/', function (req, res) { + res.end('ok'); +}); + +s.on('/empty', function (req, res) { + res.end(); +}); + +s.on('/404', function (req, res) { + res.statusCode = 404; + res.end('not'); +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('callback mode', function (t) { + got(s.url, function (err, data) { + t.error(err); + t.equal(data, 'ok'); + t.end(); + }); +}); + +tape('empty response', function (t) { + got(s.url + '/empty', function (err, data) { + t.error(err); + t.equal(data, ''); + t.end(); + }); +}); + +tape('error with code', function (t) { + got(s.url + '/404', function (err, data) { + t.ok(err); + t.equal(err.code, 404); + t.equal(data, 'not'); + t.end(); + }); +}); + +tape('buffer on encoding === null', function (t) { + got(s.url, {encoding: null}, function (err, data) { + t.error(err); + t.ok(Buffer.isBuffer(data)); + t.end(); + }); +}); + +tape('stream mode', function (t) { + got(s.url) + .on('data', function (data) { + t.equal(data.toString(), 'ok'); + t.end(); + }); +}); + +tape('proxy errors to the stream', function (t) { + got(s.url + '/404') + .on('error', function (err) { + t.equal(err.code, 404); + t.end(); + }); +}); + +tape('timeout option', function (t) { + got(s.url + '/404', {timeout: 1}) + .on('error', function (err) { + t.equal(err.code, 'ETIMEDOUT'); + t.end(); + }); +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +}); diff --git a/test/test-https.js b/test/test-https.js new file mode 100644 index 0000000..bcdbe00 --- /dev/null +++ b/test/test-https.js @@ -0,0 +1,42 @@ +'use strict'; + +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); + +var fs = require('fs'); +var path = require('path'); + +var s = server.createSSLServer(server.portSSL + 1, { + key: path.resolve(__dirname, 'ssl/ca/server.key'), + cert: path.resolve(__dirname, 'ssl/ca/server.crt') +}); +var caFile = path.resolve(__dirname, 'ssl/ca/ca.crt'); +var ca = fs.readFileSync(caFile); + +s.on('/', function (req, res) { + res.end('ok'); +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('make request to https server', function (t) { + got(s.url, { + strictSSL: true, + ca: ca, + headers: { host: 'testing.request.mikealrogers.com' } + }, function (err, data) { + t.error(err); + t.equal(data, 'ok'); + t.end(); + }); +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +}); diff --git a/test/test-post.js b/test/test-post.js new file mode 100644 index 0000000..76a3a2d --- /dev/null +++ b/test/test-post.js @@ -0,0 +1,50 @@ +'use strict'; + +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); + +var s = server.createServer(); + +s.on('/', function (req, res) { + req.pipe(res); +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('send data from options with post request', function (t) { + t.plan(2); + + got(s.url, {body: 'wow'}, function (err, data) { + t.equal(data, 'wow'); + }); + + got(s.url, {body: new Buffer('wow')}, function (err, data) { + t.equal(data, 'wow'); + }); +}); + +tape('return writeable stream', function (t) { + got.post(s.url) + .on('data', function (data) { + t.equal(data.toString(), 'wow'); + t.end(); + }) + .end('wow'); +}); + +tape('throws on write to stream with body specified', function (t) { + t.throws(function () { + got(s.url, {body: 'wow'}).write('wow'); + }); + setTimeout(t.end.bind(t), 10); // Wait for request to end +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +}); diff --git a/test/test-redirects.js b/test/test-redirects.js new file mode 100644 index 0000000..c49b522 --- /dev/null +++ b/test/test-redirects.js @@ -0,0 +1,52 @@ +'use strict'; + +var tape = require('tape'); +var got = require('../'); +var server = require('./server.js'); + +var s = server.createServer(); + +s.on('/finite', function (req, res) { + res.writeHead(302, { + location : s.url + '/' + }); + res.end(); +}); + +s.on('/endless', function (req, res) { + res.writeHead(302, { + location : s.url + '/endless' + }); + res.end(); +}); + +s.on('/', function (req, res) { + res.end('reached'); +}); + +tape('setup', function (t) { + s.listen(s.port, function () { + t.end(); + }); +}); + +tape('follows redirect', function (t) { + got(s.url + '/finite', {agent: false}, function (err, data) { + t.error(err); + t.equal(data, 'reached'); + t.end(); + }); +}); + +tape('throws on endless redirect', function (t) { + got(s.url + '/endless', {agent: false}, function (err) { + t.ok(err, 'should get error'); + t.equal(err.message, 'Redirected 10 times. Aborting.'); + t.end(); + }); +}); + +tape('cleanup', function (t) { + s.close(); + t.end(); +});