-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up request-handler, github-auth, and analytics; upgrade to Moch…
…a 4 (#1142) - Add tests to request-handler to prepare for some tweaks to caching for #820 - Clean up code in request-handler: renames, DRY, arrows, imports - Allow for clean shutdown of `setInterval` code. This requires the ability to cancel autosaving. - Upgrade to Mocha 4, and clean up so the process exits on its own (see mochajs/mocha#3044) - Better encapsulate analytics
- Loading branch information
1 parent
f371c8f
commit dc44ba7
Showing
7 changed files
with
192 additions
and
84 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
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,75 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const fetch = require('node-fetch'); | ||
const config = require('./test-config'); | ||
const Camp = require('camp'); | ||
const analytics = require('./analytics'); | ||
const { makeBadgeData: getBadgeData } = require('./badge-data'); | ||
const { | ||
handleRequest, | ||
clearRequestCache | ||
} = require('./request-handler'); | ||
|
||
const baseUri = `http://127.0.0.1:${config.port}`; | ||
|
||
function performTwoRequests (first, second) { | ||
return fetch(`${baseUri}${first}`) | ||
.then(res => { | ||
assert.ok(res.ok); | ||
return fetch(`${baseUri}${second}`) | ||
.then(res => { | ||
assert.ok(res.ok); | ||
}) | ||
}); | ||
} | ||
|
||
describe('The request handler', function() { | ||
before(analytics.load); | ||
|
||
let camp; | ||
beforeEach(function (done) { | ||
camp = Camp.start({ port: config.port, hostname: '::' }); | ||
camp.on('listening', () => done()); | ||
}); | ||
afterEach(function (done) { | ||
clearRequestCache(); | ||
if (camp) { | ||
camp.close(() => done()); | ||
camp = null; | ||
} | ||
}); | ||
|
||
let handlerCallCount; | ||
beforeEach(function () { | ||
handlerCallCount = 0; | ||
camp.route(/^\/testing\/([^/]+)\.(svg|png|gif|jpg|json)$/, | ||
handleRequest((queryParams, match, sendBadge, request) => { | ||
++handlerCallCount; | ||
const [, someValue, format] = match; | ||
const badgeData = getBadgeData('testing', queryParams); | ||
badgeData.text[1] = someValue; | ||
sendBadge(format, badgeData); | ||
})); | ||
}); | ||
|
||
it('should cache identical requests', function () { | ||
return performTwoRequests('/testing/123.svg', '/testing/123.svg').then(() => { | ||
assert.equal(handlerCallCount, 1); | ||
}); | ||
}); | ||
|
||
it('should differentiate known query parameters', function () { | ||
return performTwoRequests( | ||
'/testing/123.svg?label=foo', | ||
'/testing/123.svg?label=bar' | ||
).then(() => { assert.equal(handlerCallCount, 2); }); | ||
}); | ||
|
||
it('should ignore unknown query parameters', function () { | ||
return performTwoRequests( | ||
'/testing/123.svg?foo=1', | ||
'/testing/123.svg?foo=2' | ||
).then(() => { assert.equal(handlerCallCount, 1); }); | ||
}); | ||
}); |
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
Oops, something went wrong.