Skip to content

Commit

Permalink
Add test mode (#225)
Browse files Browse the repository at this point in the history
* Remove key- and pubkey- prefix from documentation. Fixes #208.

* Add test mode. Fixes #160

* update test mode docs

* add spy tests for test mode
  • Loading branch information
bojand committed Oct 26, 2018
1 parent 294843f commit 106420e
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 29 deletions.
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ of the `error` object passed in the callback.
See the `/docs` folder for detailed documentation. For full usage examples see the `/test` folder.

```js
var api_key = 'key-XXXXXXXXXXXXXXXXXXXXXXX';
var api_key = 'XXXXXXXXXXXXXXXXXXXXXXX';
var domain = 'www.mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

Expand Down Expand Up @@ -104,6 +104,8 @@ list.members('bob@gmail.com').delete(function (err, data) {
That is, we will try an operation only once with no retries on error. You can also use a config
object compatible with the `async` library for more control as to how the retries take place.
See docs [here](https://caolan.github.io/async/docs.html#retry)
* `testMode` - turn test mode on. If test mode is on, no requests are made, rather the request options and data is logged
* `testModeLogger` - custom test mode logging function


#### Attachments
Expand Down Expand Up @@ -423,12 +425,90 @@ mailgun.parse([ 'test@mail.com', 'test2@mail.com' ], function (err, body) {
});
```

## Debug logging

[debug](https://npmjs.com/package/debug) package is used for debug logging.

```sh
DEBUG=mailgun-js node app.js
```

## 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('mailgun-js')({ 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('mailgun-js')({ 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
```

## Tests

To run the test suite you must first have a Mailgun account with a domain setup. Then create a file named _./test/data/auth.json_, which contains your credentials as JSON, for example:

```json
{ "api_key": "key-XXXXXXXXXXXXXXXXXXXXXXX", "public_api_key": "pubkey-XXXXXXXXXXXXXXXXXXXXXXX", "domain": "mydomain.mailgun.org" }
{ "api_key": "XXXXXXXXXXXXXXXXXXXXXXX", "public_api_key": "XXXXXXXXXXXXXXXXXXXXXXX", "domain": "mydomain.mailgun.org" }
```

You should edit _./test/data/fixture.json_ and modify the data to match your context.
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ of the `error` object passed in the callback.
See the `/docs` folder for detailed documentation. For full usage examples see the `/test` folder.

```js
var api_key = 'key-XXXXXXXXXXXXXXXXXXXXXXX';
var api_key = 'XXXXXXXXXXXXXXXXXXXXXXX';
var domain = 'www.mydomain.com';
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

Expand Down
2 changes: 2 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- [Promises](/promises)
- [Webhook Validation](/webhook)
- [Email Validation](/email_validation)
- [Debug Logging](/debug)
- [Test Mode](/testmode)
- [Tests](/tests)
- API Reference
- [Message](/api/message)
Expand Down
7 changes: 7 additions & 0 deletions docs/debug.md
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
```
69 changes: 69 additions & 0 deletions docs/testmode.md
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('mailgun-js')({ 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('mailgun-js')({ 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
```
2 changes: 1 addition & 1 deletion docs/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
To run the test suite you must first have a Mailgun account with a domain setup. Then create a file named _./test/data/auth.json_, which contains your credentials as JSON, for example:

```json
{ "api_key": "key-XXXXXXXXXXXXXXXXXXXXXXX", "public_api_key": "pubkey-XXXXXXXXXXXXXXXXXXXXXXX", "domain": "mydomain.mailgun.org" }
{ "api_key": "XXXXXXXXXXXXXXXXXXXXXXX", "public_api_key": "XXXXXXXXXXXXXXXXXXXXXXX", "domain": "mydomain.mailgun.org" }
```

You should edit _./test/data/fixture.json_ and modify the data to match your context.
Expand Down
21 changes: 13 additions & 8 deletions lib/mailgun.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@ class Mailgun {
this.port = options.port || 443
this.retry = options.retry || 1

this.testMode = options.testMode
this.testModeLogger = options.testModeLogger

if (options.proxy) {
this.proxy = options.proxy
}

this.options = {
'host': this.host,
'endpoint': this.endpoint,
'protocol': this.protocol,
'port': this.port,
'auth': this.auth,
'proxy': this.proxy,
'timeout': this.timeout,
'retry': this.retry
host: this.host,
endpoint: this.endpoint,
protocol: this.protocol,
port: this.port,
auth: this.auth,
proxy: this.proxy,
timeout: this.timeout,
retry: this.retry,
testMode: this.testMode,
testModeLogger: this.testModeLogger
}

this.mailgunTokens = {}
Expand Down
15 changes: 15 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class Request {
this.proxy = options.proxy
this.timeout = options.timeout
this.retry = options.retry || 1
this.testMode = options.testMode
this.testModeLogger = typeof options.testModeLogger === 'function'
? options.testModeLogger : this.defaultTestModeLogger
}

_request (method, resource, data, fn) {
Expand Down Expand Up @@ -129,6 +132,11 @@ class Request {
opts.agent = new ProxyAgent(this.proxy)
}

if (this.testMode) {
this.testModeLogger(opts, this.payload, this.form)
return fn()
}

if (typeof this.retry === 'object' || this.retry > 1) {
retry(this.retry, (retryCb) => {
this.callback = retryCb
Expand Down Expand Up @@ -372,6 +380,13 @@ class Request {
req.end()
}
}

defaultTestModeLogger (httpOptions, payload, formData) {
const testlog = debug.enabled ? debug : console.log
testlog('options: %o', httpOptions)
testlog('payload: %o', payload)
testlog('form: %o', formData)
}
}

module.exports = Request
Loading

0 comments on commit 106420e

Please sign in to comment.