Skip to content

Commit

Permalink
Release v1.0.3 (#148)
Browse files Browse the repository at this point in the history
* 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
morsdyce authored Oct 29, 2016
1 parent 37e6e1e commit 31d9698
Show file tree
Hide file tree
Showing 25 changed files with 1,181 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ before_install:
- sudo apt-get update
- sudo apt-get install -y libappindicator1 fonts-liberation
- wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome*.deb
- sudo dpkg -i google-chrome*.deb
2 changes: 2 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const api = require('./dist/bdsm.api.js');
module.exports = api;
219 changes: 219 additions & 0 deletions integration-tests/index.html
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>
30 changes: 30 additions & 0 deletions integration-tests/worker.js
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'));
}
});
66 changes: 66 additions & 0 deletions lib/api/communicator.js
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);
}
Loading

0 comments on commit 31d9698

Please sign in to comment.