Skip to content

Commit e329663

Browse files
committed
Fix using special characters in format
1 parent eb1968a commit e329663

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix using special characters in format
45
* deps: depd@~1.1.2
56
- perf: remove argument reassignment
67

index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ function compile (format) {
375375
throw new TypeError('argument format must be a string')
376376
}
377377

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

@@ -385,7 +385,7 @@ function compile (format) {
385385
}
386386

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

390390
// eslint-disable-next-line no-new-func
391391
return new Function('tokens, req, res', js)

test/morgan.js

+51
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,57 @@ describe('morgan()', function () {
925925
})
926926
})
927927

928+
describe('a string', function () {
929+
it('should accept format as format string of tokens', function (done) {
930+
var cb = after(2, function (err, res, line) {
931+
if (err) return done(err)
932+
assert.strictEqual(line, 'GET /')
933+
done()
934+
})
935+
936+
var stream = createLineStream(function (line) {
937+
cb(null, null, line)
938+
})
939+
940+
request(createServer(':method :url', { stream: stream }))
941+
.get('/')
942+
.expect(200, cb)
943+
})
944+
945+
it('should accept text mixed with tokens', function (done) {
946+
var cb = after(2, function (err, res, line) {
947+
if (err) return done(err)
948+
assert.strictEqual(line, 'method=GET url=/')
949+
done()
950+
})
951+
952+
var stream = createLineStream(function (line) {
953+
cb(null, null, line)
954+
})
955+
956+
request(createServer('method=:method url=:url', { stream: stream }))
957+
.get('/')
958+
.expect(200, cb)
959+
})
960+
961+
it('should accept special characters', function (done) {
962+
var cb = after(2, function (err, res, line) {
963+
if (err) return done(err)
964+
assert.strictEqual(line, 'LOCAL\\tobi "GET /" 200')
965+
done()
966+
})
967+
968+
var stream = createLineStream(function (line) {
969+
cb(null, null, line)
970+
})
971+
972+
request(createServer('LOCAL\\:remote-user ":method :url" :status', { stream: stream }))
973+
.get('/')
974+
.set('Authorization', 'Basic dG9iaTpsb2tp')
975+
.expect(200, cb)
976+
})
977+
})
978+
928979
describe('combined', function () {
929980
it('should match expectations', function (done) {
930981
var cb = after(2, function (err, res, line) {

0 commit comments

Comments
 (0)