Permalink
Browse files

[test] full coverage and move form read

  • Loading branch information...
Swaagie committed Jan 19, 2015
1 parent b7ff8a2 commit 668fcc7f4662cd239925b82aaae21295bc200ea7
Showing with 175 additions and 64 deletions.
  1. +1 −0 .gitignore
  2. +0 −61 index.js
  3. +0 −1 package.json
  4. +9 −0 test/fixtures/plugin.js
  5. +165 −2 test/pipe.test.js
View
@@ -5,5 +5,6 @@ pagelets.sublime-workspace
npm-debug.log
example/dist/*
test/dist/*
+dist/*
.DS_Store
coverage
View
@@ -1,7 +1,6 @@
'use strict';
var debug = require('diagnostics')('bigpipe:server')
- , Formidable = require('formidable').IncomingForm
, Compiler = require('./lib/compiler')
, fabricate = require('fabricator')
, destroy = require('demolish')
@@ -518,66 +517,6 @@ BigPipe.readable('redirect', function redirect(pagelet, location, status, option
return pagelet.debug('Redirecting to %s', location);
});
-/**
- * Start buffering and reading the incoming request.
- *
- * @returns {Form}
- * @api private
- */
-BigPipe.readable('read', function read(pagelet) {
- var form = new Formidable()
- , pipe = this
- , fields = {}
- , files = {}
- , context
- , before;
-
- form.on('progress', function progress(received, expected) {
- //
- // @TODO if we're not sure yet if we should handle this form, we should only
- // buffer it to a predefined amount of bytes. Once that limit is reached we
- // need to `form.pause()` so the client stops uploading data. Once we're
- // given the heads up, we can safely resume the form and it's uploading.
- //
- }).on('field', function field(key, value) {
- fields[key] = value;
- }).on('file', function file(key, value) {
- files[key] = value;
- }).on('error', function error(err) {
- pagelet[pagelet.mode](err);
- fields = files = {};
- }).on('end', function end() {
- form.removeAllListeners();
-
- if (before) {
- before.call(context, fields, files, pagelet[pagelet.mode].bind(pagelet));
- }
- });
-
- /**
- * Add a hook for adding a completion callback.
- *
- * @param {Function} callback
- * @returns {Form}
- * @api public
- */
- form.before = function befores(callback, contexts) {
- if (form.listeners('end').length) {
- form.resume(); // Resume a possible buffered post.
-
- before = callback;
- context = contexts;
-
- return form;
- }
-
- callback.call(contexts || context, fields, files, pagelet[pagelet.mode].bind(pagelet));
- return form;
- };
-
- return form.parse(pagelet._req);
-});
-
/**
* Initialize a new Bootstrap Pagelet and return it so the routed Pagelet and
* its childs can use it as state keeper. The HTML of the bootstrap pagelet is
View
@@ -25,7 +25,6 @@
"diagnostics": "0.0.x",
"eventemitter3": "0.1.x",
"fabricator": "0.4.x",
- "formidable": "1.0.x",
"fusing": "0.4.x",
"mkdirp": "0.5.x",
"smithy": "0.5.x",
View
@@ -0,0 +1,9 @@
+'use strict';
+
+function noop () {}
+
+module.exports = {
+ name: 'fromfile',
+ client: noop,
+ server: noop
+};
View
@@ -548,6 +548,15 @@ describe('Pipe', function () {
pipe.once('listening', done);
});
+
+ it('defaults options to empty object', function (done) {
+ var pipe = Pipe.createServer(common.port);
+ pipe.once('listening', done);
+ })
+
+ it('returns pipe instance if listen is false', function () {
+ assume(Pipe.createServer(common.port, { listen: false })).to.be.instanceof(Pipe);
+ })
});
describe('.redirect', function () {
@@ -765,7 +774,7 @@ describe('Pipe', function () {
it('has optional name parameter', function () {
app.use('nameless', {
server: function noop() {}
- })
+ });
assume(app._plugins).to.have.property('nameless');
});
@@ -843,4 +852,158 @@ describe('Pipe', function () {
});
});
});
-});
+
+ describe('.destroy', function () {
+ it('is a function', function () {
+ assume(app.destroy).is.a('function');
+ assume(app.destroy.length).to.equal(0);
+ });
+
+ it('closes the server if required', function (done) {
+ var pipe = new Pipe(http.createServer(), {
+ dist: '/tmp/dist'
+ });
+
+ pipe.listen(common.port, function () {
+ pipe._server.close = done;
+ pipe.destroy();
+ });
+ });
+
+ it('removes listeners and cleans references', function (done) {
+ var pipe = new Pipe(http.createServer(), {
+ pagelets: __dirname +'/fixtures/pagelets',
+ dist: '/tmp/dist'
+ });
+
+ pipe.listen(common.port, function () {
+ pipe.destroy();
+
+ assume(pipe).to.not.have.property('_events');
+ assume(pipe).to.have.property('_pagelets', null);
+ assume(pipe).to.have.property('_temper', null);
+ assume(pipe).to.have.property('_plugins', null);
+ assume(pipe).to.have.property('middleware', null);
+
+ done();
+ });
+ });
+ });
+
+ describe('.bootstrap', function () {
+ it('is a function', function () {
+ assume(app.bootstrap).is.a('function');
+ assume(app.bootstrap.length).to.equal(3);
+ });
+
+ it('returns early if the response is finished', function () {
+ var req = new Request
+ , res = new Response
+ , pagelet = new Pagelet
+ , result;
+
+ res.finished = true;
+ result = app.bootstrap(pagelet, req, res);
+
+ assume(Object.keys(pagelet._bootstrap).length).to.equal(0);
+ assume(result).to.be.instanceof(Pipe);
+ });
+
+ it('proxies close listener on the response', function (done) {
+ var req = new Request
+ , res = new Response
+ , pagelet = new Pagelet({ req: req, res: res });
+
+ res.once = function (key, fn) {
+ assume(key).to.equal('close');
+ assume(fn.toString()).to.equal(app.emits('close').toString());
+ done();
+ };
+
+ app.bootstrap(pagelet, req, res);
+ });
+
+ it('forces sync mode if the JS is disabled or HTTP versions are missing', function () {
+ var req = new Request
+ , res = new Response
+ , pagelet = new Pagelet({ req: req, res: res });
+
+ req.query['no_pagelet_js'] = '1';
+ pagelet.mode = 'async';
+
+ app.bootstrap(pagelet, req, res);
+ assume(pagelet).to.have.property('mode', 'sync');
+
+ req.httpVersionMajor = '2';
+ pagelet.mode = 'async';
+
+ app.bootstrap(pagelet, req, res);
+ assume(pagelet).to.have.property('mode', 'sync');
+ });
+
+ it('bootstraps the parent in async mode by default', function () {
+ var req = new Request
+ , res = new Response
+ , pagelet = new Pagelet({ req: req, res: res });
+
+ req.httpVersionMajor = '2';
+ req.httpVersionMinor = '2';
+
+ app.bootstrap(pagelet, req, res);
+ assume(pagelet.mode).to.equal('async');
+ });
+
+ it('adds the bootstrap pagelet to the parent', function () {
+ var req = new Request
+ , res = new Response
+ , pagelet = new Pagelet({ req: req, res: res });
+
+ app.bootstrap(pagelet, req, res);
+ assume(pagelet._bootstrap).to.be.an('object');
+ assume(pagelet._bootstrap).to.be.instanceof(require('bootstrap-pagelet'));
+ });
+
+ it('calls .init on the parent pagelet', function (done) {
+ var req = new Request
+ , res = new Response
+ , pagelet = new (Pagelet.extend({
+ init: function () {
+ assume(arguments).to.have.length(0);
+ done();
+ }
+ }))({ req: req, res: res });
+
+ app.bootstrap(pagelet, req, res);
+ });
+
+ it('calls .initialize on the parent pagelet', function (done) {
+ var req = new Request
+ , res = new Response
+ , pagelet = new (Pagelet.extend({
+ initialize: function () {
+ assume(arguments).to.have.length(0);
+ done();
+ }
+ }))({ req: req, res: res });
+
+ app.bootstrap(pagelet, req, res);
+ });
+
+ it('calls .initialize (async) on the parent pagelet', function (done) {
+ var req = new Request
+ , res = new Response
+ , pagelet = new (Pagelet.extend({
+ init: function () {
+ done();
+ },
+ initialize: function (next) {
+ assume(arguments).to.have.length(1);
+ assume(next).to.be.a('function');
+ next();
+ }
+ }))({ req: req, res: res });
+
+ app.bootstrap(pagelet, req, res);
+ });
+ });
+});

0 comments on commit 668fcc7

Please sign in to comment.