Skip to content

Commit

Permalink
Merge pull request #52 from multiformats/fix/tighten-addr-validation
Browse files Browse the repository at this point in the history
Tighten validation on addr when creating from existing address
  • Loading branch information
vmx authored Feb 27, 2018
2 parents 3bad57b + 0097036 commit 1a81317
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ function Multiaddr (addr) {
}

// default
addr = addr || ''
if (addr == null) {
addr = ''
}

if (addr instanceof Buffer) {
/**
Expand Down
21 changes: 19 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,25 @@ describe('construction', () => {
expect(multiaddr('').toString()).to.equal('/')
})

it('throws on non string or buffer', () => {
expect(() => multiaddr({})).to.throw(/addr must be a string/)
it('null/undefined construct still works', () => {
expect(multiaddr().toString()).to.equal('/')
expect(multiaddr(null).toString()).to.equal('/')
expect(multiaddr(undefined).toString()).to.equal('/')
})

it('throws on truthy non string or buffer', () => {
const errRegex = /addr must be a string/
expect(() => multiaddr({})).to.throw(errRegex)
expect(() => multiaddr([])).to.throw(errRegex)
expect(() => multiaddr(138)).to.throw(errRegex)
expect(() => multiaddr(true)).to.throw(errRegex)
})

it('throws on falsy non string or buffer', () => {
const errRegex = /addr must be a string/
expect(() => multiaddr(NaN)).to.throw(errRegex)
expect(() => multiaddr(false)).to.throw(errRegex)
expect(() => multiaddr(0)).to.throw(errRegex)
})
})

Expand Down

0 comments on commit 1a81317

Please sign in to comment.