-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new network interception testing capability
- Loading branch information
Tomer Ohana
committed
Aug 7, 2019
1 parent
e087859
commit ec10a27
Showing
9 changed files
with
230 additions
and
0 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,35 @@ | ||
## Intro | ||
Sentry testkit supports using network interception libraries, without the need to change your application's `Sentry.init` function. \ | ||
The testkit is agnostic to the interception library you use. | ||
|
||
### Example using nock | ||
```javascript | ||
|
||
const nock = require('nock') | ||
const sentryTestkit = require('sentry-testkit') | ||
const { testkit, initNetworkInterceptor } = sentryTestkit() | ||
|
||
beforeAll(() => { | ||
const myAppDSN = '<your DSN goes here>' | ||
initNetworkInterceptor(myAppDSN, (baseUrl, handleRequestBody) => { | ||
// This callback is where we init our interceptor. | ||
// The interceptor should intercept requests from `baseUrl` and pass the | ||
// request body (as json) to the `handleRequestBody` function. | ||
nock(baseUrl) | ||
.persist() | ||
.post(/.*/) | ||
.reply(200, (_, requestBody) => { | ||
handleRequestBody(requestBody) | ||
}) | ||
}) | ||
}) | ||
|
||
test('findReport example', async function() { | ||
const err = new Error('error to look for') | ||
|
||
// Some faulty scenario that will report err | ||
|
||
const report = testkit.findReport(err) | ||
expect(report).toBeDefined() | ||
}) | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
'use strict' | ||
|
||
const dsnKeys = 'source protocol user pass host port path'.split(' ') | ||
const dsnPattern = /^(?:(\w+):)?\/\/(?:(\w+)(:\w+)?@)?([\w\.-]+)(?::(\d+))?(\/.*)/ //eslint-disable-line no-useless-escape | ||
|
||
const parseDsn = dsn => { | ||
const { protocol, host, path } = dsn.match(dsnPattern).reduce( | ||
(parsed, current, index) => | ||
Object.assign({}, parsed, { | ||
[dsnKeys[index]]: current, | ||
}), | ||
{} | ||
) | ||
|
||
const project = path.substr(path.lastIndexOf('/') + 1) | ||
|
||
return { protocol, project, host } | ||
} | ||
|
||
module.exports.parseDsn = parseDsn |
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,35 @@ | ||
const Sentry = require('@sentry/node') | ||
const nock = require('nock') | ||
const sentryTestkit = require('../src/index') | ||
const { createCommonTests } = require('./commonTests') | ||
|
||
const { testkit, initNetworkInterceptor } = sentryTestkit() | ||
const DUMMY_DSN = 'https://acacaeaccacacacabcaacdacdacadaca@sentry.io/000001' | ||
describe('sentry test-kit test suite - network interception', function() { | ||
beforeAll(() => { | ||
nock.cleanAll() | ||
nock.disableNetConnect() | ||
|
||
initNetworkInterceptor(DUMMY_DSN, (baseUrl, handleRequestBody) => { | ||
nock(baseUrl) | ||
.persist() | ||
.post(/.*/) | ||
.reply(200, (_, requestBody) => { | ||
handleRequestBody(requestBody) | ||
}) | ||
}) | ||
|
||
Sentry.init({ | ||
dsn: DUMMY_DSN, | ||
release: 'test', | ||
beforeSend(event) { | ||
event.extra = { os: 'mac-os' } | ||
return event | ||
}, | ||
}) | ||
}) | ||
|
||
beforeEach(() => testkit.reset()) | ||
|
||
createCommonTests({ Sentry, testkit }) | ||
}) |