2828define ( function ( require , exports , module ) {
2929 "use strict" ;
3030
31- var StaticServer = require ( "main" ) ,
32- NodeConnection = brackets . getModule ( "utils/NodeConnection" ) ,
33- FileUtils = brackets . getModule ( "file/FileUtils" ) ;
31+ var StaticServer = require ( "main" ) ,
32+ NodeConnection = brackets . getModule ( "utils/NodeConnection" ) ,
33+ FileUtils = brackets . getModule ( "file/FileUtils" ) ,
34+ SpecRunnerUtils = brackets . getModule ( "spec/SpecRunnerUtils" ) ;
3435
3536 var testFolder = FileUtils . getNativeModuleDirectoryPath ( module ) + "/unittest-files/" ;
36-
37+
38+ var CONNECT_TIMEOUT = 5000 ;
39+
40+ var LOCALHOST_PORT_PARSER_RE = / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 : ( \d + ) \/ / ;
41+
3742 describe ( "StaticServer" , function ( ) {
3843
3944 // Unit tests for the underlying node server.
@@ -43,7 +48,7 @@ define(function (require, exports, module) {
4348 beforeEach ( function ( ) {
4449 runs ( function ( ) {
4550 nodeConnection = new NodeConnection ( ) ;
46- waitsForDone ( nodeConnection . connect ( false ) , "connecting to node server" ) ;
51+ waitsForDone ( nodeConnection . connect ( false ) , "connecting to node server" , CONNECT_TIMEOUT ) ;
4752 } ) ;
4853
4954 runs ( function ( ) {
@@ -195,10 +200,104 @@ define(function (require, exports, module) {
195200
196201 // Unit tests for the StaticServerProvider that wraps the underlying node server.
197202 describe ( "StaticServerProvider" , function ( ) {
203+ var brackets ,
204+ ProjectManager ,
205+ StaticServer ;
198206
199- it ( "should have initialized the static server provider by app ready time" , function ( ) {
207+ beforeEach ( function ( ) {
208+ runs ( function ( ) {
209+ SpecRunnerUtils . createTestWindowAndRun ( this , function ( testWindow ) {
210+ // Load module instances from brackets.test
211+ brackets = testWindow . brackets ;
212+ ProjectManager = testWindow . brackets . test . ProjectManager ;
213+ } ) ;
214+ } ) ;
215+
216+ waitsFor ( function ( ) {
217+ return brackets . test . extensions . StaticServer !== undefined ;
218+ } , "StaticServer to fully initialize" ) ;
219+
220+ runs ( function ( ) {
221+ StaticServer = brackets . test . extensions . StaticServer ;
222+ waitsForDone ( StaticServer . _getNodeConnectionDeferred ( ) , "connecting to node server" , CONNECT_TIMEOUT ) ;
223+ } ) ;
224+ } ) ;
225+
226+ afterEach ( function ( ) {
227+ SpecRunnerUtils . closeTestWindow ( ) ;
228+ } ) ;
229+
230+
231+ it ( "should have initialized the static server provider immediately after launch" , function ( ) {
232+ // Note: the goal is to test this as quickly as possible. We can't
233+ // actually test immediately when appReady fires because that happens
234+ // asynchronously in our test window. But we know this will run shortly
235+ // after appReady fires. There's no way to test it synchronously
236+ // because appReady is the event that gives us access to StaticServer in
237+ // the test window.
200238 expect ( StaticServer . _getStaticServerProvider ( ) ) . toBeTruthy ( ) ;
201239 } ) ;
240+
241+
242+ it ( "should only serve html files that are in the project file hierarchy" , function ( ) {
243+ waitsForDone ( ProjectManager . openProject ( testFolder ) , "opens test folder in ProjectManager" ) ;
244+
245+ runs ( function ( ) {
246+ var provider = StaticServer . _getStaticServerProvider ( ) ;
247+
248+ // should not serve files outside project hierarchy
249+ expect ( provider . canServe ( "/foo.html" ) ) . toBe ( false ) ;
250+
251+ // should not serve non-HTML files inside hierarchy
252+ expect ( provider . canServe ( testFolder + "foo.jpg" ) ) . toBe ( false ) ;
253+
254+ // should serve .htm files inside hierarchy
255+ expect ( provider . canServe ( testFolder + "foo.htm" ) ) . toBe ( true ) ;
256+
257+ // should serve .html files inside hierarchy
258+ expect ( provider . canServe ( testFolder + "foo.html" ) ) . toBe ( true ) ;
259+
260+ // should serve .HTML files inside hierarchy
261+ expect ( provider . canServe ( testFolder + "foo.HTML" ) ) . toBe ( true ) ;
262+
263+ // should serve root of hierarchy
264+ expect ( provider . canServe ( testFolder ) ) . toBe ( true ) ;
265+
266+ } ) ;
267+
268+ } ) ;
269+
270+ it ( "should be ready to serve a file in the project and return an appropriate baseUrl" , function ( ) {
271+ waitsForDone ( ProjectManager . openProject ( testFolder ) , "opens test folder in ProjectManager" ) ;
272+
273+ waitsForDone ( StaticServer . _getStaticServerProvider ( ) . readyToServe ( ) , "being ready to serve" ) ;
274+
275+ runs ( function ( ) {
276+ var baseUrl = StaticServer . _getStaticServerProvider ( ) . getBaseUrl ( ) ;
277+ var parsedUrl = LOCALHOST_PORT_PARSER_RE . exec ( baseUrl ) ;
278+ expect ( parsedUrl ) . toBeTruthy ( ) ;
279+ expect ( parsedUrl . length ) . toBe ( 2 ) ;
280+ expect ( Number ( parsedUrl [ 1 ] ) ) . toBeGreaterThan ( 0 ) ;
281+ } ) ;
282+ } ) ;
283+
284+ it ( "should decline serving if not connected to node" , function ( ) {
285+ var nodeConnectionDeferred ;
286+ runs ( function ( ) {
287+ nodeConnectionDeferred = StaticServer . _getNodeConnectionDeferred ( ) ;
288+ waitsForDone ( nodeConnectionDeferred , "connecting to node server" , CONNECT_TIMEOUT ) ;
289+ } ) ;
290+
291+ runs ( function ( ) {
292+ nodeConnectionDeferred . done ( function ( nodeConnection ) {
293+ // this will be run synchronously because of the waitsFor above
294+ nodeConnection . disconnect ( ) ;
295+ } ) ;
296+ expect ( StaticServer . _getStaticServerProvider ( ) . canServe ( testFolder + "foo.html" ) ) . toBe ( false ) ;
297+ } ) ;
298+
299+ } ) ;
300+
202301 } ) ;
203302 } ) ;
204303} ) ;
0 commit comments