Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict'
// **Github:** https://github.com/fidm/x509
//
// **License:** MIT
import fs from 'fs'
import { strictEqual, ok, throws } from 'assert'
import { suite, it } from 'tman'
import { PEM } from '../src/pem'
suite('PEM', function () {
it('should work', function () {
const crtData = fs.readFileSync('./test/cert/github.crt')
const pems = PEM.parse(crtData)
strictEqual(pems.length, 1)
strictEqual(pems[0].type, 'CERTIFICATE')
strictEqual(pems[0].procType, '')
strictEqual(pems[0].getHeader('DEK-Info'), '')
strictEqual(pems[0].toString(), crtData.toString())
ok(pems[0].body instanceof Buffer)
})
it('should throw error if no block', function () {
throws(() => PEM.parse(Buffer.alloc(0)))
})
})
function test (merge2) {
tman.suite('merge2', function () {
tman.it('merge2(read1, read2, through3)', function (done) {
const options = { objectMode: true }
const result = []
const read1 = fakeReadStream(options)
const read2 = fakeReadStream(options)
const through3 = through.obj()
const mergeStream = merge2(read1, read2, through3)
read1.push(1)
thunk.delay(100)(function () {
read1.push(2)
read1.push(null)
})
read2.push(3)
thunk.delay(10)(function () {
tman.suite('ctx.acceptsCharsets()', function () {
tman.suite('with no arguments', function () {
tman.suite('when Accept-Charset is populated', function () {
tman.it('should return accepted types', function () {
let ctx = context()
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'
assert.deepStrictEqual(ctx.acceptsCharsets(), ['utf-8', 'utf-7', 'iso-8859-1'])
})
})
})
tman.suite('with multiple arguments', function () {
tman.suite('when Accept-Charset is populated', function () {
tman.suite('if any types match', function () {
tman.it('should return the best fit', function () {
let ctx = context()
ctx.req.headers['accept-charset'] = 'utf-8, iso-8859-1;q=0.2, utf-7;q=0.5'
assert.strictEqual(ctx.acceptsCharsets('utf-7', 'utf-8'), 'utf-8')
tman.suite('ctx.acceptsLanguages(langs)', function () {
tman.suite('with no arguments', function () {
tman.suite('when Accept-Language is populated', function () {
tman.it('should return accepted types', function () {
let ctx = context()
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'
assert.deepStrictEqual(ctx.acceptsLanguages(), ['es', 'pt', 'en'])
})
})
})
tman.suite('with multiple arguments', function () {
tman.suite('when Accept-Language is populated', function () {
tman.suite('if any types types match', function () {
tman.it('should return the best fit', function () {
let ctx = context()
ctx.req.headers['accept-language'] = 'en;q=0.8, es, pt'
assert.strictEqual(ctx.acceptsLanguages('es', 'en'), 'es')
tman.suite('ctx.acceptsEncodings()', function () {
tman.suite('with no arguments', function () {
tman.suite('when Accept-Encoding is populated', function () {
tman.it('should return accepted types', function () {
let ctx = context()
ctx.req.headers['accept-encoding'] = 'gzip, compress;q=0.2'
assert.deepStrictEqual(ctx.acceptsEncodings(), ['gzip', 'compress', 'identity'])
assert.strictEqual(ctx.acceptsEncodings('gzip', 'compress'), 'gzip')
})
})
tman.suite('when Accept-Encoding is not populated', function () {
tman.it('should return identity', function () {
let ctx = context()
assert.deepStrictEqual(ctx.acceptsEncodings(), ['identity'])
assert.strictEqual(ctx.acceptsEncodings('gzip', 'deflate', 'identity'), 'identity')
})
})
tman.suite('ctx.accepts(types)', function () {
tman.suite('with no arguments', function () {
tman.suite('when Accept is populated', function () {
tman.it('should return all accepted types', function () {
let ctx = context()
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'
assert.deepStrictEqual(ctx.accepts(), ['text/html', 'text/plain', 'image/jpeg', 'application/*'])
})
})
})
tman.suite('with no valid types', function () {
tman.suite('when Accept is populated', function () {
tman.it('should return false', function () {
let ctx = context()
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain'
assert.strictEqual(ctx.accepts('image/png', 'image/tiff'), false)
})
}, 100)
},
function (callback) {
setTimeout(function () {
callback(null, 'b')
}, 10)
}
])
})(function (error, value) {
should(error).be.equal(null)
should(value).be.equal('b')
})(done)
})
})
tman.suite('thunk.thunkify()', function () {
function test (a, b, c, callback) {
callback(a, b, c, this)
}
tman.it('thunk.thunkify()', function (done) {
var thunk = thunks()
var thunkTest = thunk.thunkify(test)
thunkTest(1, 2, 3)(function (error, value) {
should(error).be.equal(1)
should(value).be.equal(undefined)
return thunkTest(null, 2, x)
})(function (error, value) {
should(error).be.equal(null)
should(value[0]).be.equal(2)
should(value[1]).be.equal(x)
should(value[2]).be.equal(this)
assert.strictEqual(mejs.render('index'), '0')
mejs = tplStr2Mejs('<%- 0 %>')
assert.strictEqual(mejs.render('index'), '0')
})
tman.it('false renders something escaped', function () {
let mejs = tplStr2Mejs('<%= false %>')
assert.strictEqual(mejs.render('index'), 'false')
mejs = tplStr2Mejs('<%- false %>')
assert.strictEqual(mejs.render('index'), 'false')
})
})
tman.suite('<%', function () {
tman.it('without semicolons', function () {
let mejs = fixtureMejs('no.semicolons.ejs')
assert.strictEqual(mejs.render('no.semicolons'), fixtureFile('no.semicolons.html'))
})
})
tman.suite('<%=', function () {
tman.it('escape &
let mejs = tplStr2Mejs('<%= false %>')
assert.strictEqual(mejs.render('index'), 'false')
mejs = tplStr2Mejs('<%- false %>')
assert.strictEqual(mejs.render('index'), 'false')
})
})
tman.suite('<%', function () {
tman.it('without semicolons', function () {
let mejs = fixtureMejs('no.semicolons.ejs')
assert.strictEqual(mejs.render('no.semicolons'), fixtureFile('no.semicolons.html'))
})
})
tman.suite('<%=', function () {
tman.it('escape &
assert.equal(mejs.render('index', {users: users}), expectedResult)
})
tman.it('works with windows style', function () {
let mejs = tplStr2Mejs('<ul><% -%>\r\n' +
'<% it.users.forEach(function(user){ -%>\r\n' +
'<li><%= user.name -%></li>\r\n' +
'<% }) -%>\r\n' +
'</ul><% -%>\r\n')
let expectedResult = '<ul><li>geddy</li>\r\n<li>neil</li>\r\n<li>alex</li>\r\n</ul>'
assert.equal(mejs.render('index', {users: users}), expectedResult)
})
})
tman.suite('<%%', function () {
tman.it('produce literals', function () {
let mejs = tplStr2Mejs('<%%- "foo" %>')
assert.strictEqual(mejs.render('index'), '<%- "foo" %>')
})
tman.it('work without an end tag', function () {
let mejs = tplStr2Mejs('<%%')
assert.strictEqual(mejs.render('index'), '<%')
mejs = fixtureMejs('literal.ejs', {delimiter: ' '})
assert.strictEqual(mejs.render('literal'), fixtureFile('literal.html'))
})
})
tman.suite('%%>', function () {
tman.it('produce literal', function () {