get POST working better

This commit is contained in:
Jonathan Lipps
2013-01-12 22:44:24 -08:00
parent 817ec934be
commit 7b40537a8a
3 changed files with 30 additions and 37 deletions

View File

@@ -4,9 +4,7 @@ var application = target.frontMostApp();
var host = target.host();
var mainWindow = application.mainWindow();
var elements = {};
var bufLen = 16384;
var bufferFlusher = [];
// 16384 is apprently the buffer size used by instruments
var bufLen = 16384; // 16384 is apprently the buffer size used by instruments
var console = {
log: function(msg) {
@@ -16,7 +14,6 @@ var console = {
newMsg += "*";
}
UIALogger.logMessage(newMsg);
//UIALogger.logDebug(bufferFlusher);
}
};
@@ -34,14 +31,12 @@ function delay(secs)
var doCurl = function(method, url, data) {
args = ["-i", "-X", method];
if (data) {
for (var k in data) {
if (data.hasOwnProperty(k)) {
args = args.concat(['-d', k+"="+encodeURIComponent(data[k])]);
}
}
args = args.concat(['-d', JSON.stringify(data)]);
args = args.concat(["-H", "Content-Type: application/json"]);
}
args.push(url);
//console.log(url)
//console.log(args);
var res = host.performTaskWithPathArgumentsTimeout("/usr/bin/curl", args, 10);
var response = res.stdout;
//console.log(res.stdout);
@@ -83,9 +78,12 @@ var getNextCommand = function() {
};
var sendCommandResult = function(commandId, result) {
var url = 'send_result/'+commandId+'/'+encodeURIComponent(result);
var res = doCurl('GET', endpoint + url);
console.log(res.status);
var url = 'send_result/'+commandId;
var res = doCurl('POST', endpoint + url, {result: result});
res = JSON.parse(res.value);
if (res.error) {
console.log("Error: " + res.error);
}
};
while(runLoop) {
@@ -93,9 +91,11 @@ while(runLoop) {
if (cmd) {
console.log("Executing command " + cmd.commandId + ": " + cmd.command);
var result = eval(cmd.command);
if (typeof result === "undefined") {
result = false;
}
console.log(cmd.commandId+": "+result);
sendCommandResult(cmd.commandId, result);
} else {
delay(10);
}
delay(1);
}

View File

@@ -1,17 +1,14 @@
var http = require('http')
, url = require('url')
, express = require('express')
, app = express()
, path = require('path')
, server = http.createServer(app)
, ap = require('argparse').ArgumentParser
, colors = require('colors')
, spawn = require('child_process').spawn
, build = require('./build')
, instruments = require('./instruments');
app.configure(function() {
app.use(express.bodyParser());
app.use(express.bodyParser()); // this is required
app.use(app.router);
});
@@ -38,16 +35,12 @@ build(appRoot, function(err) {
);
inst.launch(function() {
console.log('done launching');
inst.sendCommand("application.bundleID()", function(bundleId) {
console.log("Bundle ID is " + bundleId);
inst.sendCommand("mainWindow.textFields()[0].setValue('3');", function() {
inst.sendCommand("mainWindow.textFields()[1].setValue('5');", function() {
inst.sendCommand("mainWindow.buttons()[0].tap();", function() {
inst.sendCommand("mainWindow.staticTexts()[0].value()", function(sum) {
console.log("Sum should be 8 and is " + sum);
//process.exit(0);
});
inst.sendCommand("mainWindow.textFields()[0].setValue('3');", function() {
inst.sendCommand("mainWindow.textFields()[1].setValue('5');", function() {
inst.sendCommand("mainWindow.buttons()[0].tap();", function() {
inst.sendCommand("mainWindow.staticTexts()[0].value()", function(sum) {
console.log("Sum should be 8 and is " + sum);
//process.exit(0);
});
});
});

View File

@@ -1,7 +1,8 @@
// Wrapper around Apple's Instruments app
//
var spawn = require('child_process').spawn;
var spawn = require('child_process').spawn,
express = require('express');
var Instruments = function(server, app, udid, bootstrap, template) {
this.server = server;
@@ -85,30 +86,29 @@ Instruments.prototype.extendServer = function(err, cb) {
}
// }
});
this.server.post('/instruments/send_result/:commandId?', function(req, res) {
console.log(req.body);
this.server.post('/instruments/send_result/:commandId', function(req, res) {
var commandId = parseInt(req.params.commandId, 10);
var result = req.body;
var result = req.body.result;
if (typeof commandId != "undefined" && typeof result != "undefined") {
if (!self.curCommand) {
res.send('ERROR: Not waiting for a command result');
res.send(500, {error: "Not waiting for a command result"});
} else if (commandId != self.curCommandId) {
res.send('ERROR: Command ID ' + commandId + ' does not match ' + self.curCommandId);
res.send(500, {error: 'Command ID ' + commandId + ' does not match ' + self.curCommandId});
} else {
if (typeof result === "object" && typeof result.result !== "undefined") {
result = result.result;
}
self.curCommand = null;
self.commandCallbacks[commandId](result);
res.send('OK');
res.send({success: true});
}
} else {
res.send('ERROR: Bad parameters sent');
res.send(500, {error: 'Bad parameters sent'});
}
});
this.server.post('/instruments/ready', function(req, res) {
self.readyHandler();
res.send('OK');
res.send({success: true});
});
};