Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A positive "Response" is now a one-liner.

Added a "decorator" method (res.success()) to handle success responses with minimum code.
  • Loading branch information...
commit b5dc1cc31f1b697434220f116726fb73d823e5d1 1 parent 271ff1a
@detro detro authored
View
19 src/request_handlers/request_handler.js
@@ -56,10 +56,27 @@ ghostdriver.RequestHandler = function() {
request.urlParsed = require("./third_party/parseuri.js").parse(request.url);
},
+ _writeJSONDecorator = function(obj) {
+ this.write(JSON.stringify(obj));
+ },
+
+ _successDecorator = function(sessionId, value) {
+ this.statusCode = 200;
+
+ if (arguments.length > 0) {
+ // write something, only if there is something to write
+ this.writeJSON(_buildSuccessResponseBody(sessionId, value));
+ this.close();
+ } else {
+ this.closeGracefully();
+ }
+ },
+
_decorateResponse = function(response) {
response.setHeader("Cache", "no-cache");
response.setHeader("Content-Type", "application/json;charset=UTF-8");
- response.writeJSON = function(obj) { this.write(JSON.stringify(obj)); };
+ response.writeJSON = _writeJSONDecorator;
+ response.success = _successDecorator;
},
_buildResponseBody = function(sessionId, value, statusCode) {
View
12 src/request_handlers/session_manager_request_handler.js
@@ -80,8 +80,6 @@ ghostdriver.SessionManagerReqHand = function() {
var activeSessions = [],
sessionId;
- res.statusCode = 200;
-
// Create array of format '[{ "id" : SESSION_ID, "capabilities" : SESSION_CAPABILITIES_OBJECT }]'
for (sessionId in _sessions) {
activeSessions.push({
@@ -90,8 +88,7 @@ ghostdriver.SessionManagerReqHand = function() {
});
}
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, null, activeSessions));
- res.close();
+ res.success(null, activeSessions);
},
_deleteSession = function(sessionId) {
@@ -112,8 +109,7 @@ ghostdriver.SessionManagerReqHand = function() {
if (typeof(_sessions[sId]) !== "undefined") {
_deleteSession(sId);
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
} else {
throw _errors.createInvalidReqVariableResourceNotFoundEH(req);
}
@@ -128,9 +124,7 @@ ghostdriver.SessionManagerReqHand = function() {
session = _getSession(sId);
if (session !== null) {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, sId, _sessions[sId].getCapabilities()));
- res.close();
+ res.success(sId, _sessions[sId].getCapabilities());
} else {
throw _errors.createInvalidReqVariableResourceNotFoundEH(req);
}
View
41 src/request_handlers/session_request_handler.js
@@ -130,9 +130,7 @@ ghostdriver.SessionReqHand = function(session) {
_createOnSuccessHandler = function(res) {
return function (status) {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(res, _session.getId()));
- res.closeGracefully();
+ res.success(_session.getId());
};
},
@@ -176,9 +174,7 @@ ghostdriver.SessionReqHand = function(session) {
}
// If we arrive here, everything should be fine, birds are singing, the sky is blue
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), result.value));
- res.close();
+ res.success(_session.getId(), result.value);
},
_refreshCommand = function(req, res) {
@@ -270,23 +266,16 @@ ghostdriver.SessionReqHand = function(session) {
},
_getWindowHandle = function(req, res) {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), _session.getCurrentWindowHandle()));
- res.close();
+ res.success(_session.getId(), _session.getCurrentWindowHandle());
},
_getWindowHandles = function(req, res) {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), _session.getWindowHandles()));
- res.close();
+ res.success(_session.getId(), _session.getWindowHandles());
},
_getScreenshotCommand = function(req, res) {
var rendering = _session.getCurrentWindow().renderBase64PNG();
-
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), rendering));
- res.close();
+ res.success(_session.getId(), rendering);
},
_getUrlCommand = function(req, res) {
@@ -312,8 +301,7 @@ ghostdriver.SessionReqHand = function(session) {
clearTimeout(timer);
if (status === "success") {
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
} else {
_errors.handleInvalidReqInvalidCommandMethodEH(req, res);
}
@@ -343,11 +331,9 @@ ghostdriver.SessionReqHand = function(session) {
postObj["type"] = _session.timeoutNames().ASYNC_SCRIPT;
}
- if (typeof(postObj["type"]) !== "undefined" &&
- typeof(postObj["ms"]) !== "undefined") {
+ if (typeof(postObj["type"]) !== "undefined" && typeof(postObj["ms"]) !== "undefined") {
_session.setTimeout(postObj["type"], postObj["ms"]);
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
} else {
throw _errors.createInvalidReqMissingCommandParameterEH(req);
}
@@ -356,8 +342,7 @@ ghostdriver.SessionReqHand = function(session) {
_deleteWindowCommand = function(req, res) {
// TODO An optional JSON parameter "name" might be given
_session.closeCurrentWindow();
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
},
_postWindowCommand = function(req, res) {
@@ -367,9 +352,7 @@ ghostdriver.SessionReqHand = function(session) {
_getTitleCommand = function(req, res) {
var result = _session.getCurrentWindow().evaluate(function() { return document.title; });
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), result));
- res.close();
+ res.success(_session.getId(), result);
},
_postElementCommand = function(req, res) {
@@ -383,9 +366,7 @@ ghostdriver.SessionReqHand = function(session) {
do {
element = _locator.locateElement(JSON.parse(req.post));
if (element) {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, _session.getId(), element.getJSON()));
- res.close();
+ res.success(_session.getId(), element.getJSON());
return;
}
} while(searchStartTime + _session.getTimeout(_session.timeoutNames().IMPLICIT) >= new Date().getTime());
View
4 src/request_handlers/status_request_handler.js
@@ -48,9 +48,7 @@ ghostdriver.StatusReqHand = function() {
_protoParent.handle.call(this, req, res);
if (req.method === "GET" && req.urlParsed.file === "status") {
- res.statusCode = 200;
- res.writeJSON(_protoParent.buildSuccessResponseBody.call(this, null, _statusObj));
- res.close();
+ res.success(null, _statusObj);
return;
}
View
6 src/request_handlers/webelement_request_handler.js
@@ -70,8 +70,7 @@ ghostdriver.WebElementReqHand = function(id, session) {
// TODO - Error handling based on the value of "typeRes"
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
return;
}
@@ -85,8 +84,7 @@ ghostdriver.WebElementReqHand = function(id, session) {
// Listen for the page to Finish Loading after the submit
_getSession().getCurrentWindow().setOneShotCallback("onLoadFinished", function(status) {
if (status === "success") {
- res.statusCode = 200;
- res.closeGracefully();
+ res.success();
}
// TODO - what do we do if this fails?
Please sign in to comment.
Something went wrong with that request. Please try again.