@@ -9,18 +9,17 @@ var request = require("supertest");
99
1010describe ( "Accepting single middleware as a proxy option" , function ( ) {
1111
12- var bs , spy , server ;
12+ it ( "should call the middleware" , function ( done ) {
1313
14- before ( function ( done ) {
14+ var path = "/forms.html" ;
1515
1616 browserSync . reset ( ) ;
1717
1818 var app = connect ( ) ;
1919 app . use ( require ( "serve-static" ) ( "./test/fixtures" ) ) ;
2020
21- server = app . listen ( ) ;
22-
23- spy = sinon . spy ( ) ;
21+ var server = app . listen ( ) ;
22+ var spy = sinon . spy ( ) ;
2423
2524 var fn = function ( req , res , next ) {
2625 spy ( req . url ) ;
@@ -36,28 +35,67 @@ describe("Accepting single middleware as a proxy option", function () {
3635 open : false
3736 } ;
3837
39- bs = browserSync . init ( config , done ) . instance ;
40- } ) ;
38+ browserSync . init ( config , function ( err , bs ) {
4139
42- after ( function ( ) {
43- bs . cleanup ( ) ;
44- server . close ( ) ;
45- } ) ;
40+ assert . equal ( bs . options . get ( "middleware" ) . size , 2 ) ;
4641
47- it ( "serves files from the middleware with snippet added" , function ( ) {
48- assert . equal ( bs . options . get ( "middleware" ) . size , 2 ) ;
42+ request ( bs . server )
43+ . get ( path )
44+ . set ( "accept" , "text/html" )
45+ . expect ( 200 )
46+ . end ( function ( err , res ) {
47+
48+ sinon . assert . calledWithExactly ( spy , path ) ;
49+ assert . include ( res . text , bs . options . get ( "snippet" ) ) ;
50+
51+ bs . cleanup ( function ( ) {
52+ server . close ( ) ;
53+ done ( ) ;
54+ } ) ;
55+ } ) ;
56+ } ) ;
4957 } ) ;
50- it ( "should call the middlewares" , function ( done ) {
58+ it ( "can add middleware at run-time" , function ( done ) {
59+
5160 var path = "/forms.html" ;
52- request ( bs . server )
53- . get ( path )
54- . set ( "accept" , "text/html" )
55- . expect ( 200 )
56- . end ( function ( err , res ) {
57- sinon . assert . calledWithExactly ( spy , path ) ;
58- assert . include ( res . text , bs . options . get ( "snippet" ) ) ;
59- done ( ) ;
60- } ) ;
61+
62+ browserSync . reset ( ) ;
63+
64+ var app = connect ( ) ;
65+ app . use ( require ( "serve-static" ) ( "./test/fixtures" ) ) ;
66+
67+ var server = app . listen ( ) ;
68+ var spy = sinon . spy ( ) ;
69+
70+ var fn = function ( req , res , next ) {
71+ spy ( req . url ) ;
72+ next ( ) ;
73+ } ;
74+
75+ var config = {
76+ proxy : "http://localhost:" + server . address ( ) . port ,
77+ logLevel : "silent" ,
78+ open : false
79+ } ;
80+
81+ browserSync . init ( config , function ( err , bs ) {
82+
83+ bs . addMiddleware ( "*" , fn ) ;
84+
85+ request ( bs . server )
86+ . get ( path )
87+ . set ( "accept" , "text/html" )
88+ . expect ( 200 )
89+ . end ( function ( ) {
90+
91+ sinon . assert . calledWithExactly ( spy , path ) ;
92+
93+ bs . cleanup ( function ( ) {
94+ server . close ( ) ;
95+ done ( ) ;
96+ } ) ;
97+ } ) ;
98+ } ) ;
6199 } ) ;
62100} ) ;
63101
0 commit comments