Skip to content

Commit

Permalink
Add special case to support multiple unsubscribes. Fixes #203
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed May 1, 2018
1 parent 4a17211 commit f2db0e4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 49 deletions.
12 changes: 11 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function isSpecialParam (paramKey) {
return ((key === 'vars' || key === 'members' || key === 'recipient-variables') || (key.indexOf('v:') === 0))
}

function isMultiUnsubsribe (path, data) {
return path.indexOf('/unsubscribes') && data && Array.isArray(data)
}

function prepareData (data) {
const params = {}

Expand Down Expand Up @@ -77,14 +81,20 @@ class Request {
} else {
if (isMIME) {
this.headers['Content-Type'] = 'multipart/form-data'
} else if (method === 'POST' && isMultiUnsubsribe(path, data)) {
this.headers['Content-Type'] = 'application/json'
} else {
this.headers['Content-Type'] = 'application/x-www-form-urlencoded'
}

if (params && (params.attachment || params.inline || (isMIME && params.message))) {
this.prepareFormData(params)
} else {
this.payload = qs.stringify(params)
if (method === 'POST' && isMultiUnsubsribe(path, data)) {
this.payload = JSON.stringify(data)
} else {
this.payload = qs.stringify(params)
}

if (this.payload) {
this.headers['Content-Length'] = Buffer.byteLength(this.payload)
Expand Down
11 changes: 1 addition & 10 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,7 @@ module.exports = {
'description': 'Adds address to unsubscribed table.',
'href': '/unsubscribes',
'method': 'POST',
'title': 'create',
'properties': {
'address': {
'type': 'string'
},
'tag': {
'type': 'string'
}
},
'required': ['address', 'tag']
'title': 'create'
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions test/data/fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"address": "[email protected]",
"tag": "*"
},
"unsubscribe2": {
"address": "[email protected]",
"tag": ["some_tag"]
},
"bounce": {
"address": "[email protected]"
},
Expand Down
44 changes: 6 additions & 38 deletions test/unsubscribes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,16 @@ describe('Unsubscribes', () => {
setTimeout(done, 500)
})

it('test unsubscribes().create() missing address', (done) => {
mailgun.unsubscribes().create({}, (err) => {
assert.ok(err)
assert(/Missing parameter 'address'/.test(err.message))
done()
})
})

it('test unsubscribes().create() missing tag', (done) => {
mailgun.unsubscribes().create({
'address': fixture.unsubscribe.address
}, (err) => {
assert.ok(err)
assert(/Missing parameter 'tag'/.test(err.message))
done()
})
})

it('test unsubscribes().create() invalid address type', (done) => {
mailgun.unsubscribes().create({
'address': 123,
'tag': fixture.unsubscribe.tag
}, (err) => {
assert.ok(err)
assert(/Invalid parameter type./.test(err.message))
done()
})
})

it('test unsubscribes().create() invalid tag type', (done) => {
mailgun.unsubscribes().create({
'address': fixture.unsubscribe.address,
'tag': 10
}, (err) => {
assert.ok(err)
assert(/Invalid parameter type./.test(err.message))
it('test unsubscribes().create()', (done) => {
mailgun.unsubscribes().create(fixture.unsubscribe, (err, body) => {
assert.ifError(err)
assert.ok(body)
done()
})
})

it('test unsubscribes().create()', (done) => {
mailgun.unsubscribes().create(fixture.unsubscribe, (err, body) => {
it('test unsubscribes().create() multi', (done) => {
mailgun.unsubscribes().create([fixture.unsubscribe, fixture.unsubscribe2], (err, body) => {
assert.ifError(err)
assert.ok(body)
done()
Expand Down

0 comments on commit f2db0e4

Please sign in to comment.