You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var test = require("tape")
  2. var extend = require("./")
  3. var mutableExtend = require("./mutable")
  4. test("merge", function(assert) {
  5. var a = { a: "foo" }
  6. var b = { b: "bar" }
  7. assert.deepEqual(extend(a, b), { a: "foo", b: "bar" })
  8. assert.end()
  9. })
  10. test("replace", function(assert) {
  11. var a = { a: "foo" }
  12. var b = { a: "bar" }
  13. assert.deepEqual(extend(a, b), { a: "bar" })
  14. assert.end()
  15. })
  16. test("undefined", function(assert) {
  17. var a = { a: undefined }
  18. var b = { b: "foo" }
  19. assert.deepEqual(extend(a, b), { a: undefined, b: "foo" })
  20. assert.deepEqual(extend(b, a), { a: undefined, b: "foo" })
  21. assert.end()
  22. })
  23. test("handle 0", function(assert) {
  24. var a = { a: "default" }
  25. var b = { a: 0 }
  26. assert.deepEqual(extend(a, b), { a: 0 })
  27. assert.deepEqual(extend(b, a), { a: "default" })
  28. assert.end()
  29. })
  30. test("is immutable", function (assert) {
  31. var record = {}
  32. extend(record, { foo: "bar" })
  33. assert.equal(record.foo, undefined)
  34. assert.end()
  35. })
  36. test("null as argument", function (assert) {
  37. var a = { foo: "bar" }
  38. var b = null
  39. var c = void 0
  40. assert.deepEqual(extend(b, a, c), { foo: "bar" })
  41. assert.end()
  42. })
  43. test("mutable", function (assert) {
  44. var a = { foo: "bar" }
  45. mutableExtend(a, { bar: "baz" })
  46. assert.equal(a.bar, "baz")
  47. assert.end()
  48. })
  49. test("null prototype", function(assert) {
  50. var a = { a: "foo" }
  51. var b = Object.create(null)
  52. b.b = "bar";
  53. assert.deepEqual(extend(a, b), { a: "foo", b: "bar" })
  54. assert.end()
  55. })
  56. test("null prototype mutable", function (assert) {
  57. var a = { foo: "bar" }
  58. var b = Object.create(null)
  59. b.bar = "baz";
  60. mutableExtend(a, b)
  61. assert.equal(a.bar, "baz")
  62. assert.end()
  63. })
  64. test("prototype pollution", function (assert) {
  65. var a = {}
  66. var maliciousPayload = '{"__proto__":{"oops":"It works!"}}'
  67. assert.strictEqual(a.oops, undefined)
  68. extend({}, maliciousPayload)
  69. assert.strictEqual(a.oops, undefined)
  70. assert.end()
  71. })
  72. test("prototype pollution mutable", function (assert) {
  73. var a = {}
  74. var maliciousPayload = '{"__proto__":{"oops":"It works!"}}'
  75. assert.strictEqual(a.oops, undefined)
  76. mutableExtend({}, maliciousPayload)
  77. assert.strictEqual(a.oops, undefined)
  78. assert.end()
  79. })