body-fingerprint
Advanced tools
Comparing version 1.3.2 to 1.3.3
14
index.js
@@ -75,7 +75,13 @@ const multipartFingerprint = (req, _, next) => { | ||
const order = []; | ||
JSON.parse(req.json.raw.body, (key) => { | ||
if (key) order.push(key); | ||
}); | ||
try { | ||
JSON.parse(req.json.raw.body, (key) => { | ||
if (key) order.push(key); | ||
}); | ||
} catch (error) { | ||
req.json.error = error; | ||
} | ||
req.json.order = order; | ||
req.json.fingerprint = order.join(","); | ||
req.json.fingerprint = order.join(); | ||
@@ -82,0 +88,0 @@ const spaces = []; |
{ | ||
"name": "body-fingerprint", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Tracks consumers by POST body", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
115
test.js
@@ -575,2 +575,117 @@ const assert = require("node:assert"); | ||
}); | ||
it("can parse spaces inner objects", () => { | ||
const exampleJsonString = ` { | ||
"a": 5, | ||
"b": { | ||
"c": "a\\n b" | ||
} | ||
}`; | ||
const expected = [ | ||
" ", | ||
"\n", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
"\n", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
"\n", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
"\n", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
"\n", | ||
" ", | ||
" ", | ||
" ", | ||
" ", | ||
]; | ||
const req = new (class extends EventEmitter { | ||
get headers() { | ||
return { | ||
"content-type": "application/json", | ||
}; | ||
} | ||
setEncoding() {} | ||
})(); | ||
jsonFingerprint(req, res, next); | ||
req.emit("data", exampleJsonString); | ||
req.emit("end"); | ||
const { | ||
json: { spaces: actual }, | ||
} = req; | ||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
it("should silently fail on bad body", () => { | ||
// no double quotes between property key "a" | ||
const exampleJsonString = ' {a: "who\\r \\n ops" \r\n} \r'; | ||
const expected = [" ", " ", " ", " ", "\r", "\n", " ", "\r"]; | ||
const req = new (class extends EventEmitter { | ||
get headers() { | ||
return { | ||
"content-type": "application/json", | ||
}; | ||
} | ||
setEncoding() {} | ||
})(); | ||
jsonFingerprint(req, res, next); | ||
req.emit("data", exampleJsonString); | ||
req.emit("end"); | ||
const { | ||
json: { spaces: actual }, | ||
} = req; | ||
assert.deepStrictEqual(actual, expected); | ||
}); | ||
it("should have error on fail parse", () => { | ||
// no double quotes between property key "a" | ||
const exampleJsonString = ' {a: "who\\r \\n ops" \r\n} \r'; | ||
const expected = "Unexpected token a in JSON at position 2"; | ||
const req = new (class extends EventEmitter { | ||
get headers() { | ||
return { | ||
"content-type": "application/json", | ||
}; | ||
} | ||
setEncoding() {} | ||
})(); | ||
jsonFingerprint(req, res, next); | ||
req.emit("data", exampleJsonString); | ||
req.emit("end"); | ||
const actual = req.json.error.message; | ||
assert.equal(actual, expected); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20902
684