Skip to content

Commit

Permalink
Fix using special characters in format
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 11, 2018
1 parent eb1968a commit e329663
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix using special characters in format
* deps: depd@~1.1.2
- perf: remove argument reassignment

Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ function compile (format) {
throw new TypeError('argument format must be a string')
}

var fmt = format.replace(/"/g, '\\"')
var js = ' "use strict"\n return "' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function (_, name, arg) {
var fmt = String(JSON.stringify(format))
var js = ' "use strict"\n return ' + fmt.replace(/:([-\w]{2,})(?:\[([^\]]+)\])?/g, function (_, name, arg) {
var tokenArguments = 'req, res'
var tokenFunction = 'tokens[' + String(JSON.stringify(name)) + ']'

Expand All @@ -385,7 +385,7 @@ function compile (format) {
}

return '" +\n (' + tokenFunction + '(' + tokenArguments + ') || "-") + "'
}) + '"'
})

// eslint-disable-next-line no-new-func
return new Function('tokens, req, res', js)
Expand Down
51 changes: 51 additions & 0 deletions test/morgan.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,57 @@ describe('morgan()', function () {
})
})

describe('a string', function () {
it('should accept format as format string of tokens', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert.strictEqual(line, 'GET /')
done()
})

var stream = createLineStream(function (line) {
cb(null, null, line)
})

request(createServer(':method :url', { stream: stream }))
.get('/')
.expect(200, cb)
})

it('should accept text mixed with tokens', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert.strictEqual(line, 'method=GET url=/')
done()
})

var stream = createLineStream(function (line) {
cb(null, null, line)
})

request(createServer('method=:method url=:url', { stream: stream }))
.get('/')
.expect(200, cb)
})

it('should accept special characters', function (done) {
var cb = after(2, function (err, res, line) {
if (err) return done(err)
assert.strictEqual(line, 'LOCAL\\tobi "GET /" 200')
done()
})

var stream = createLineStream(function (line) {
cb(null, null, line)
})

request(createServer('LOCAL\\:remote-user ":method :url" :status', { stream: stream }))
.get('/')
.set('Authorization', 'Basic dG9iaTpsb2tp')
.expect(200, cb)
})
})

describe('combined', function () {
it('should match expectations', function (done) {
var cb = after(2, function (err, res, line) {
Expand Down

0 comments on commit e329663

Please sign in to comment.