Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit c42ddbb

Browse files
committed
Merge pull request #3001 from adobe/jrb/static-server-provider-tests
add unit tests for StaticServerProvider
2 parents abbad78 + 7c50041 commit c42ddbb

3 files changed

Lines changed: 123 additions & 6 deletions

File tree

src/brackets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ define(function (require, exports, module) {
144144
NativeApp : require("utils/NativeApp"),
145145
ExtensionUtils : ExtensionUtils,
146146
UpdateNotification : require("utils/UpdateNotification"),
147+
extensions : {}, // place for extensions to hang modules for unit tests
147148
doneLoading : false
148149
};
149150

src/extensions/default/StaticServer/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ define(function (require, exports, module) {
165165
return _staticServerProvider;
166166
}
167167

168+
/**
169+
* Allows access to the deferred that manages the node connection. This
170+
* is *only* for unit tests. Messing with this not in testing will
171+
* potentially break everything.
172+
*
173+
* @private
174+
* @return {jQuery.Deferred} The deferred that manages the node connection
175+
*/
176+
function _getNodeConnectionDeferred() {
177+
return _nodeConnectionDeferred;
178+
}
179+
168180
AppInit.appReady(function () {
169181
// Register as a Live Development server provider
170182
LiveDevServerManager.registerProvider(_staticServerProvider, 5);
@@ -193,8 +205,13 @@ define(function (require, exports, module) {
193205
}
194206
);
195207
});
208+
209+
if (brackets.test) {
210+
brackets.test.extensions.StaticServer = module.exports;
211+
}
196212
});
197213

198214
// For unit tests only
199215
exports._getStaticServerProvider = _getStaticServerProvider;
216+
exports._getNodeConnectionDeferred = _getNodeConnectionDeferred;
200217
});

src/extensions/default/StaticServer/unittests.js

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@
2828
define(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 = /http:\/\/127\.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

Comments
 (0)