Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content-type based mocks #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ before(function() {
});

module.exports = function autoRecord() {
const whitelistHeaderRegexes = whitelistHeaders.map(str => RegExp(str));
const whitelistHeaderRegexes = ['content-type', ...whitelistHeaders].map(str => RegExp(str));

// For cleaning, to store the test names that are active per file
let testNames = [];
Expand Down Expand Up @@ -63,7 +63,7 @@ module.exports = function autoRecord() {
cy.server({
// Filter out blacklisted routes from being recorded and logged
whitelist: xhr => {
if(xhr.url) {
if (xhr.url) {
// TODO: Use blobs
return blacklistRoutes.some(route => xhr.url.includes(route));
}
Expand All @@ -77,11 +77,11 @@ module.exports = function autoRecord() {
const body = response.request.body;
const headers = Object.entries(response.response.headers)
.filter(([key]) => whitelistHeaderRegexes.some(regex => regex.test(key)))
.reduce((obj, [key, value]) => ({...obj, [key]: value}), {});
.reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});

// We push a new entry into the routes array
// Do not rerecord duplicate requests
if(!routes.some(route => route.url === url && route.body === body && route.method === method)) {
if (!routes.some(route => route.url === url && route.body === body && route.method === method)) {
routes.push({ url, method, status, data, body, headers });
}
},
Expand Down Expand Up @@ -129,7 +129,7 @@ module.exports = function autoRecord() {
url: url,
status: response.status,
headers: response.headers,
response: response.fixtureId ? `fixture:${response.fixtureId}.json` : response.response,
response: response.fixtureId ? `fixture:${response.fixtureId}.${response.fixtureExt}` : response.response,
// This handles requests from the same url but with different request bodies
onResponse: () => onResponse(method, url, index + 1),
});
Expand Down Expand Up @@ -163,30 +163,40 @@ module.exports = function autoRecord() {
// Check to see if the current test already has mock data or if forceRecord is on
if (
(!routesByTestId[this.currentTest.title]
|| isTestForceRecord
|| recordTests.includes(this.currentTest.title))
|| isTestForceRecord
|| recordTests.includes(this.currentTest.title))
&& !isCleanMocks
) {
// Construct endpoint to be saved locally
const endpoints = routes.map((request) => {
// Check to see of mock data is too large for request header
const isFileOversized = sizeInMbytes(request.data) > 70;
let fixtureId;
let fixtureExt = 'json';

// Handling data type. Need to add more content types handlers (img, etc)
if (request.headers && request.headers['content-type']) {
const contentType = request.headers['content-type'];
if (contentType.includes('text/html')) fixtureExt = 'html';
else if (contentType.includes('application/json')) fixtureExt = 'json';
else fixtureExt = 'odt'; // odt - Other Data Type
}

// If the mock data is too large, store it in a separate json
if (isFileOversized) {
if (isFileOversized || fixtureExt !== 'json') {
fixtureId = guidGenerator();
addFixture[path.join(fixturesFolder, `${fixtureId}.json`)] = request.data;
addFixture[path.join(fixturesFolder, `${fixtureId}.${fixtureExt}`)] = request.data;
}

return {
fixtureId: fixtureId,
fixtureId,
fixtureExt,
url: request.url,
method: request.method,
status: request.status,
headers: request.headers,
body: request.body,
response: isFileOversized ? undefined : request.data,
response: fixtureId ? undefined : request.data,
};
});

Expand Down