-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update xhook dependency with modified support for web workers * Add an abstraction between the interceptor and mock data * Add ability to mock a fetch request inside a web worker * bump version to v1.0.3-beta.1 * Change interval to a timeout and rename data property to payload * bump version to v1.0.3-beta.2 * Fix bdsm not updating content with correct message type * Register an event listener on the worker and if it belongs to bdsm stop the event from propagating * Namespace postmessage events and stop event propagation on bdsm messages on the client as well * restore normal requests * Add bdsm/worker entry point * Add bdsm/api entry point * feat(Export): Add new export dialog with ability to prettify exports (#143) * feat(API): Add ability to prettify exports in the API Add a flag to the export function that allows you to get a prettified response. New method signature is export(prettify:bool). If prettyify is supplied the output will be a json string with 2 spaces indentation * Implement new export dialog * Tests: Add Interceptor E2E tests Add e2e tests to all use cases of requests being captured and modified via the interceptor * fix(export): Remove an incorrect check of the content type to attempt parsing request parameters (#146) Before this fix, only mocks that returned a content type of application/json would parse the request parameters and request body due to a wrong assumption. if a request was sent using json request parameters (json body) but the server would return a different response type, such as text/html the check will fail and return an incorrect export file. * chore(scripts): Streamline the npm run test:integration command (#147) the test:integration command now builds the project, opens an http server, runs the tests and kills the http server process in the end instead of having to do these steps manually * chore(Release): Release version 1.0.3
- Loading branch information
Showing
25 changed files
with
1,181 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const api = require('./dist/bdsm.api.js'); | ||
module.exports = api; |
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,219 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
<script src="https://cdn.jsdelivr.net/jquery/2.1.4/jquery.js"></script> | ||
</head> | ||
<body> | ||
|
||
<div> | ||
<h3>Requests Basics</h3> | ||
<button id="get-fetch">Get FETCH</button> | ||
<button id="get-xhr">Get XHR</button> | ||
|
||
<button id="post-fetch">POST FETCH</button> | ||
<button id="post-xhr">POST XHR</button> | ||
</div> | ||
|
||
<div> | ||
<h3>Url wildcards</h3> | ||
<button id="get-fetch-wildcard">Get FETCH Wildcard</button> | ||
<button id="get-xhr-wildcard">Get XHR wildcard</button> | ||
</div> | ||
|
||
<div> | ||
<h3>Request param wildcards</h3> | ||
<button id="post-fetch-params-wildcard">POST FETCH Request Body Wildcard</button> | ||
<button id="post-xhr-params-wildcard">POST XHR Request Body wildcard</button> | ||
</div> | ||
|
||
<div> | ||
<h3>Url Wildcards + Request param wildcards</h3> | ||
<button id="post-fetch-both-wildcard">POST FETCH Request Body + url Wildcard</button> | ||
<button id="post-xhr-both-wildcard">POST XHR Request Body + url wildcard</button> | ||
</div> | ||
|
||
<div> | ||
<h3>Request Delays</h3> | ||
<button id="get-fetch-delay">Get FETCH Delay</button> | ||
<button id="get-xhr-delay">Get XHR Delay</button> | ||
</div> | ||
|
||
<div> | ||
<h3>Web worker request</h3> | ||
<button id="webworker-get-fetch">Get FETCH</button> | ||
<button id="webworker-get-xhr">Get XHR</button> | ||
</div> | ||
|
||
<h3>Result</h3> | ||
<div id="result"></div> | ||
|
||
<script src="../dist/bdsm.js"></script> | ||
<script> | ||
function outputResponse(response) { | ||
$('#result').text(response); | ||
} | ||
|
||
function outputError() { | ||
$('#result').text('request failed'); | ||
} | ||
|
||
function outputWorkerResponse(response) { | ||
const data = response.data ? JSON.stringify(response.data) : 'request failed'; | ||
$('#result').text(data); | ||
} | ||
|
||
function clearResponse() { | ||
$('#result').text(''); | ||
} | ||
|
||
function parseResponseAsText(response) { | ||
return response.text(); | ||
} | ||
|
||
function getRandomString() { | ||
return Math.round(Math.random() * 100).toString(); | ||
} | ||
|
||
|
||
// Request basics | ||
$('#get-fetch').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/get') | ||
.then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#get-xhr').click(function() { | ||
clearResponse(); | ||
$.get('http://bdsm-example.com/get') | ||
.then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
$('#post-fetch').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/post', { | ||
method: 'post', | ||
body: JSON.stringify({ user: 'user', password: 'password' }) | ||
}).then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#post-xhr').click(function() { | ||
clearResponse(); | ||
$.post('http://bdsm-example.com/post', { | ||
user: 'user', | ||
password: 'password' | ||
}).then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
// Url Wildcards | ||
$('#get-fetch-wildcard').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/get-wildcard/' + getRandomString()) | ||
.then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#get-xhr-wildcard').click(function() { | ||
clearResponse(); | ||
$.get('http://bdsm-example.com/get-wildcard/' + getRandomString()) | ||
.then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
// Request parameters wildcards | ||
$('#post-fetch-params-wildcard').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/post-wildcard', { | ||
method: 'post', | ||
body: JSON.stringify({ user: 'user' + getRandomString(), password: 'password' + getRandomString() }) | ||
}).then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#post-xhr-params-wildcard').click(function() { | ||
clearResponse(); | ||
$.post('http://bdsm-example.com/post-wildcard', { | ||
user: 'user' + getRandomString(), | ||
password: 'password' + getRandomString() | ||
}).then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
// Url wildcards + request param wildcards | ||
$('#post-fetch-both-wildcard').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/post-wildcard-both/' + getRandomString(), { | ||
method: 'post', | ||
body: JSON.stringify({ user: 'user' + getRandomString(), password: 'password' + getRandomString() }) | ||
}).then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#post-xhr-both-wildcard').click(function() { | ||
clearResponse(); | ||
$.post('http://bdsm-example.com/post-wildcard-both/' + getRandomString(), { | ||
user: 'user' + getRandomString(), | ||
password: 'password' + getRandomString() | ||
}).then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
// request delays | ||
$('#get-fetch-delay').click(function() { | ||
clearResponse(); | ||
fetch('http://bdsm-example.com/get-delay') | ||
.then(parseResponseAsText) | ||
.then(outputResponse) | ||
.catch(outputError); | ||
}); | ||
|
||
$('#get-xhr-delay').click(function() { | ||
clearResponse(); | ||
$.get('http://bdsm-example.com/get-delay') | ||
.then(outputResponse) | ||
.fail(outputError); | ||
}); | ||
|
||
// web worker requests | ||
$('#webworker-get-fetch').click(function() { | ||
clearResponse(); | ||
var worker = new Worker('worker.js'); | ||
window.bdsm.api.bootstrapWorker(worker); | ||
|
||
worker.onmessage = outputWorkerResponse; | ||
|
||
worker.postMessage({ | ||
type: 'REQUEST', | ||
url: 'http://bdsm-example.com/worker', | ||
method: 'GET', | ||
requestMethod: 'fetch' | ||
}); | ||
}); | ||
|
||
$('#webworker-get-xhr').click(function() { | ||
clearResponse(); | ||
var worker = new Worker('worker.js'); | ||
window.bdsm.api.bootstrapWorker(worker); | ||
|
||
worker.onmessage = outputWorkerResponse; | ||
|
||
worker.postMessage({ | ||
type: 'REQUEST', | ||
url: 'http://bdsm-example.com/worker', | ||
method: 'GET', | ||
requestMethod: 'xhr' | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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,30 @@ | ||
importScripts('../dist/bdsm.worker.js'); | ||
importScripts('https://cdnjs.cloudflare.com/ajax/libs/superagent/2.3.0/superagent.js'); | ||
|
||
self.addEventListener('message', (message) => { | ||
const { method, url, requestMethod } = message.data; | ||
|
||
console.log({ method, url, requestMethod }); | ||
|
||
// XHR Sample with superagent | ||
if (requestMethod === 'xhr') { | ||
superagent | ||
.get(url) | ||
.end((err, res) => { | ||
if (err) { | ||
self.postMessage('request failed'); | ||
return; | ||
} | ||
|
||
self.postMessage(res.text); | ||
}); | ||
} | ||
|
||
// fetch API sample | ||
if (requestMethod === 'fetch') { | ||
fetch(url) | ||
.then((response) => response.text()) | ||
.then((response) => self.postMessage(response)) | ||
.catch((err) => self.postMessage('request failed')); | ||
} | ||
}); |
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,66 @@ | ||
import Emitter from 'api/emitter'; | ||
import Scenarios from 'api/scenarios'; | ||
import Requests from 'api/requests'; | ||
import { MockedRequest } from 'api/models/mocked-request'; | ||
import { getHeadersObject } from 'api/utils/headers'; | ||
|
||
const isWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; | ||
|
||
let mockedRequests = []; | ||
let fetchedRequests = false; | ||
|
||
if (isWebWorker) { | ||
self.addEventListener('message', (event) => { | ||
if (event.data.type === 'BDSM_SET_DATA') { | ||
event.stopImmediatePropagation(); | ||
mockedRequests = event.data.payload.map((request) => new MockedRequest(request)); | ||
fetchedRequests = true; | ||
} | ||
}); | ||
} | ||
|
||
export function getCurrentMockedRequests() { | ||
return new Promise((resolve, reject) => { | ||
if (!isWebWorker) { | ||
resolve(Scenarios.getCurrentMockedRequests()); | ||
} | ||
|
||
setTimeout(() => resolve(mockedRequests), 0); | ||
}); | ||
} | ||
|
||
export function captureRequest(request, response) { | ||
if (isWebWorker) { | ||
const jsonRequest = { | ||
method: request.method, | ||
url: request.url, | ||
headers: getHeadersObject(request.headers), | ||
startTime: request.startTime | ||
}; | ||
const jsonResponse = { | ||
headers: getHeadersObject(response.headers), | ||
status: response.status, | ||
statusText: response.statusText, | ||
url: response.url, | ||
data: response.data | ||
}; | ||
|
||
self.postMessage({ type: 'BDSM_CAPTURE_REQUEST', payload: { | ||
mockId: request.mock ? request.mock.id : null, | ||
request: jsonRequest, | ||
response: jsonResponse | ||
}}); | ||
return; | ||
} | ||
|
||
Requests.capture(request, response); | ||
} | ||
|
||
export function emit(event) { | ||
if (isWebWorker) { | ||
self.postMessage({ type: 'BDSM_EMIT_EVENT', payload: event }); | ||
return; | ||
} | ||
|
||
Emitter.emit(event); | ||
} |
Oops, something went wrong.