Skip to content

Commit

Permalink
chore: Adds tests to cover #101
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed Aug 22, 2018
1 parent 20b4125 commit 1e79308
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 12 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"email": "[email protected]"
}
],
"workspaces": ["packages/@pollyjs/*"],
"workspaces": [
"packages/@pollyjs/*"
],
"scripts": {
"commitmsg": "commitlint -E GIT_PARAMS",
"build": "lerna run build --ignore=@pollyjs/ember --parallel",
Expand All @@ -30,11 +32,9 @@
"test:node": "mocha --opts tests/mocha.opts",
"test:jest": "jest",
"test:ember": "lerna run test --stream --no-prefix --scope=@pollyjs/ember",
"server:build":
"yarn build --scope=@pollyjs/node-server --scope=@pollyjs/utils",
"server:build": "yarn build --scope=@pollyjs/node-server --scope=@pollyjs/utils",
"docs:serve": "docsify serve ./docs",
"docs:publish":
"gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"docs:publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"release": "lerna publish",
"postrelease": "yarn docs:publish"
},
Expand All @@ -49,6 +49,7 @@
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"chai": "^4.1.2",
"deep-eql": "^3.0.1",
"deepmerge": "^2.1.1",
"docsify-cli": "^4.2.1",
"eslint": "^4.19.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ describe('Integration | XHR Adapter', function() {
return xhrRequest(...arguments);
}
});
setupPolly.afterEach();

setupPolly.afterEach();
adapterTests();
adapterBrowserTests();
});
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers/xhr-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export default function request(url, obj = {}) {

xhr.send(obj.body);
}).then(xhr => {
return new Response(xhr.responseText, {
const responseBody =
xhr.status === 204 && xhr.responseText === '' ? null : xhr.responseText;

return new Response(responseBody, {
status: xhr.status,
statusText: xhr.statusText,
headers: XHRUtils.serializeResponseHeaders(xhr.getAllResponseHeaders())
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/adapter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ export default function adapterTests() {
expect(res.status).to.equal(404);
});

it('should properly handle 204s requests', async function() {
const sharedRequest = {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ foo: 'bar' })
};

const createRespones = await this.fetchRecord({
...sharedRequest,
method: 'POST'
});

expect(createRespones.status).to.equal(200);

/* NOTE: our test API will 204 when doing a PUT with content it already contains */
const noContentResponse = await this.fetchRecord({
...sharedRequest,
method: 'PUT'
});

expect(noContentResponse.status).to.equal(204);
expect((await noContentResponse.text())).to.equal('');

await this.fetchRecord({ method: 'DELETE' });
});

it('should intercept', async function() {
const { server } = this.polly;

Expand Down
20 changes: 16 additions & 4 deletions tests/middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env node */
const path = require('path');
const deepEql = require('deep-eql');
const bodyParser = require('body-parser');
const { registerExpressAPI } = require('../packages/@pollyjs/node-server');

Expand All @@ -16,14 +17,25 @@ module.exports = function attachMiddleware(app) {
res.sendStatus(200);
});

app.put('/api/db/:id', (req, res) => {
const { id } = req.params;
const entry = DB[id];

if (deepEql(entry, req.body)) {
return res.status(204).end();
}

return res.status(200).end();
});

app.get('/api/db/:id', (req, res) => {
const { id } = req.params;

if (DB[id]) {
res.status(200).json(DB[id]);
} else {
res.status(404).end();
if (!DB[id]) {
return res.status(404).end();
}

res.status(200).json(DB[id]);
});

app.post('/api/db/:id', (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
{
"log": {
"_recordingName": "@pollyjs/adapter-fetch/Integration | Fetch Adapter | Browser/should properly handle 204s requests",
"browser": {
"name": "Chrome",
"version": "68.0"
},
"creator": {
"comment": "persister:rest",
"name": "Polly.JS",
"version": "1.1.3"
},
"entries": [
{
"_id": "27eb93e60c039c6e58d5424ee689650a",
"_order": 0,
"cache": {},
"request": {
"bodySize": 13,
"cookies": [],
"headers": [
{
"name": "content-type",
"value": "application/json"
}
],
"headersSize": 220,
"httpVersion": "HTTP/1.1",
"method": "POST",
"postData": {
"mimeType": "application/json",
"text": "{\"foo\":\"bar\"}"
},
"queryString": [],
"url": "http://localhost:4000/api/db/-pollyjs_2160168770%2Fadapter-fetch_162589637%2FIntegration-Fetch-Adapter-Browser_2847373818%2Fshould-properly-handle-204s-requests_125125220"
},
"response": {
"bodySize": 13,
"content": {
"mimeType": "application/json; charset=utf-8",
"size": 13,
"text": "{\"foo\":\"bar\"}"
},
"cookies": [],
"headers": [
{
"name": "connection",
"value": "keep-alive"
},
{
"name": "content-length",
"value": "13"
},
{
"name": "content-type",
"value": "application/json; charset=utf-8"
},
{
"name": "date",
"value": "Wed, 22 Aug 2018 06:01:33 GMT"
},
{
"name": "etag",
"value": "W/\"d-pedE0BZFQNM7HX6mFsKPL6l+dUo\""
},
{
"name": "x-powered-by",
"value": "Express"
}
],
"headersSize": 194,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-08-22T06:01:33.715Z",
"time": 12,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 12
}
},
{
"_id": "e04758833f211a9089f17ddb88c8ac1a",
"_order": 0,
"cache": {},
"request": {
"bodySize": 13,
"cookies": [],
"headers": [
{
"name": "content-type",
"value": "application/json"
}
],
"headersSize": 219,
"httpVersion": "HTTP/1.1",
"method": "PUT",
"postData": {
"mimeType": "application/json",
"text": "{\"foo\":\"bar\"}"
},
"queryString": [],
"url": "http://localhost:4000/api/db/-pollyjs_2160168770%2Fadapter-fetch_162589637%2FIntegration-Fetch-Adapter-Browser_2847373818%2Fshould-properly-handle-204s-requests_125125220"
},
"response": {
"bodySize": 0,
"content": {
"mimeType": "text/plain",
"size": 0
},
"cookies": [],
"headers": [
{
"name": "connection",
"value": "keep-alive"
},
{
"name": "date",
"value": "Wed, 22 Aug 2018 06:01:33 GMT"
},
{
"name": "x-powered-by",
"value": "Express"
}
],
"headersSize": 86,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 204,
"statusText": "No Content"
},
"startedDateTime": "2018-08-22T06:01:33.729Z",
"time": 14,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 14
}
},
{
"_id": "917150eb3db8668bb9c37f59391f029e",
"_order": 0,
"cache": {},
"request": {
"bodySize": 0,
"cookies": [],
"headers": [],
"headersSize": 192,
"httpVersion": "HTTP/1.1",
"method": "DELETE",
"queryString": [],
"url": "http://localhost:4000/api/db/-pollyjs_2160168770%2Fadapter-fetch_162589637%2FIntegration-Fetch-Adapter-Browser_2847373818%2Fshould-properly-handle-204s-requests_125125220"
},
"response": {
"bodySize": 0,
"content": {
"mimeType": "text/plain",
"size": 0
},
"cookies": [],
"headers": [
{
"name": "connection",
"value": "keep-alive"
},
{
"name": "content-length",
"value": "0"
},
{
"name": "date",
"value": "Wed, 22 Aug 2018 06:01:33 GMT"
},
{
"name": "x-powered-by",
"value": "Express"
}
],
"headersSize": 105,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2018-08-22T06:01:33.745Z",
"time": 2,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 2
}
}
],
"pages": [],
"version": "1.2"
}
}
Loading

0 comments on commit 1e79308

Please sign in to comment.