-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
303 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## Debug logging | ||
|
||
[debug](https://npmjs.com/package/debug) package is used for debug logging. | ||
|
||
```sh | ||
DEBUG=mailgun-js node app.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## Test mode | ||
|
||
Test mode can be turned on using `testMode` option. When on, no requests are actually sent to Mailgun, rather we log the request options and applicable payload and form data. By default we log to `console.log`, unless `DEBUG` is turned on, in which case we use debug logging. | ||
|
||
```js | ||
mailgun = require('../')({ apiKey: api_key, domain: domain, testMode: true }) | ||
|
||
const data = { | ||
from: 'mailgunjs+test1@gmail.com', | ||
to: 'mailgunjstest+recv1@gmail.com', | ||
subject: 'Test email subject', | ||
text: 'Test email text' | ||
}; | ||
|
||
mailgun.messages().send(data, function (error, body) { | ||
console.log(body); | ||
}); | ||
``` | ||
|
||
``` | ||
options: { hostname: 'api.mailgun.net', | ||
port: 443, | ||
protocol: 'https:', | ||
path: '/v3/sandbox12345.mailgun.org/messages', | ||
method: 'POST', | ||
headers: | ||
{ 'Content-Type': 'application/x-www-form-urlencoded', | ||
'Content-Length': 127 }, | ||
auth: 'api:key-0e8pwgtt5ylx0m94xwuzqys2-o0x4-77', | ||
agent: false, | ||
timeout: undefined } | ||
payload: 'to=mailgunjs%2Btest1%40gmail.com&from=mailgunjstest%2Brecv1%40gmail.com&subject=Test%20email%20subject&text=Test%20email%20text' | ||
form: undefined | ||
undefined | ||
``` | ||
|
||
Note that in test mode no error or body are returned as a result. | ||
|
||
The logging can be customized using `testModeLogger` option which is a function to perform custom logging. | ||
|
||
```js | ||
const logger = (httpOptions, payload, form) => { | ||
const { method, path } = httpOptions | ||
const hasPayload = !!payload | ||
const hasForm = !!form | ||
|
||
console.log(`%s %s payload: %s form: %s`, method, path, hasPayload, hasForm) | ||
} | ||
|
||
mailgun = require('../')({ apiKey: api_key, domain: domain, testMode: true, testModeLogger: logger }) | ||
|
||
const data = { | ||
from: 'mailgunjs+test1@gmail.com', | ||
to: 'mailgunjstest+recv1@gmail.com', | ||
subject: 'Test email subject', | ||
text: 'Test email text' | ||
}; | ||
|
||
mailgun.messages().send(data, function (error, body) { | ||
console.log(body); | ||
}); | ||
``` | ||
|
||
Sample output: | ||
|
||
``` | ||
POST /v3/sandbox12345.mailgun.org/messages payload: true form: false | ||
undefined | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const auth = require('./data/auth.json') | ||
const fixture = require('./data/fixture.json') | ||
const clone = require('clone') | ||
const assert = require('assert') | ||
const path = require('path') | ||
const debug = require('debug') | ||
|
||
let mailgun | ||
describe('test mode', () => { | ||
describe('default operation', () => { | ||
before(() => { | ||
mailgun = require('../')({ | ||
apiKey: auth.api_key, | ||
domain: auth.domain, | ||
testMode: true | ||
}) | ||
}) | ||
|
||
beforeEach(done => { | ||
setTimeout(done, 100) | ||
}) | ||
|
||
it('test messages().send()', done => { | ||
const msg = clone(fixture.message) | ||
|
||
mailgun.messages().send(msg, (err, body) => { | ||
assert.ifError(err) | ||
done() | ||
}) | ||
}) | ||
|
||
it('test messages().send() with attachment using filename', done => { | ||
const msg = clone(fixture.message) | ||
const filename = path.join(__dirname, '/data/mailgun_logo.png') | ||
|
||
msg.attachment = filename | ||
|
||
mailgun.messages().send(msg, (err, body) => { | ||
assert.ifError(err) | ||
|
||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('default operation with DEBUG', () => { | ||
before(() => { | ||
mailgun = require('../')({ | ||
apiKey: auth.api_key, | ||
domain: auth.domain, | ||
testMode: true | ||
}) | ||
|
||
debug.enable('mailgun-js') | ||
}) | ||
|
||
after(() => { | ||
debug.disable() | ||
}) | ||
|
||
beforeEach(done => { | ||
setTimeout(done, 100) | ||
}) | ||
|
||
it('test messages().send()', done => { | ||
const msg = clone(fixture.message) | ||
|
||
mailgun.messages().send(msg, (err, body) => { | ||
assert.ifError(err) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('custom logger', () => { | ||
before(() => { | ||
mailgun = require('../')({ | ||
apiKey: auth.api_key, | ||
domain: auth.domain, | ||
testMode: true, | ||
testModeLogger: (httpOptions, payload, form) => { | ||
const { method, path } = httpOptions | ||
const hasPayload = !!payload | ||
const hasForm = !!form | ||
|
||
console.log(`%s %s payload: %s form: %s`, method, path, hasPayload, hasForm) | ||
} | ||
}) | ||
}) | ||
|
||
beforeEach(done => { | ||
setTimeout(done, 100) | ||
}) | ||
|
||
it('test messages().send()', done => { | ||
const msg = clone(fixture.message) | ||
|
||
mailgun.messages().send(msg, (err, body) => { | ||
assert.ifError(err) | ||
done() | ||
}) | ||
}) | ||
|
||
it('test messages().send() with attachment using filename', done => { | ||
const msg = clone(fixture.message) | ||
const filename = path.join(__dirname, '/data/mailgun_logo.png') | ||
|
||
msg.attachment = filename | ||
|
||
mailgun.messages().send(msg, (err, body) => { | ||
assert.ifError(err) | ||
|
||
done() | ||
}) | ||
}) | ||
}) | ||
}) |