| OLD | NEW |
| (Empty) | |
| 1 |
| 2 var url = require('url'); |
| 3 var playback = require('./playback.js'); |
| 4 |
| 5 /** |
| 6 * Returns connect middleware that will record and playback for Angular.dart's |
| 7 * HTTP playback service. |
| 8 * @param opts |
| 9 * path is the path where the record / playback endpoint is served |
| 10 * playbackImpl is the playback module, used for mocking |
| 11 * @returns {Function} |
| 12 */ |
| 13 function endpoint(opts) { |
| 14 opts = opts || {}; |
| 15 opts.path = opts.path || '/record'; |
| 16 opts.playbackImpl = opts.playbackImpl || playback.playback(); |
| 17 |
| 18 |
| 19 return function playbackEndpoint(req, res, next) { |
| 20 if (url.parse(req.url).path != opts.path) { |
| 21 next(); |
| 22 return; |
| 23 } |
| 24 |
| 25 if (req.method == 'POST') { |
| 26 var body = ''; |
| 27 req.on('data', function(data) { |
| 28 body += data; |
| 29 }); |
| 30 req.on('end', function() { |
| 31 var parsedBody = JSON.parse(body); |
| 32 |
| 33 opts.playbackImpl.record(parsedBody.key, parsedBody.data); |
| 34 res.writeHead(200); |
| 35 res.end(); |
| 36 }); |
| 37 } else if (req.method == 'GET') { |
| 38 var data = opts.playbackImpl.playback(); |
| 39 res.writeHead(200, { |
| 40 'Content-Type': 'application/dart', |
| 41 'Content-Length': Buffer.byteLength(data) |
| 42 }); |
| 43 res.end(data); |
| 44 } |
| 45 } |
| 46 } |
| 47 |
| 48 module.exports = { |
| 49 endpoint: endpoint |
| 50 }; |
| OLD | NEW |