From 7b40537a8a6f357df75c76695ec5cae5f64203a3 Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Sat, 12 Jan 2013 22:44:24 -0800 Subject: [PATCH] get POST working better --- instruments/bootstrap_example.js | 28 ++++++++++++++-------------- instruments/example.js | 21 +++++++-------------- instruments/instruments.js | 18 +++++++++--------- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/instruments/bootstrap_example.js b/instruments/bootstrap_example.js index 5f86b21d1..66245608d 100644 --- a/instruments/bootstrap_example.js +++ b/instruments/bootstrap_example.js @@ -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); } diff --git a/instruments/example.js b/instruments/example.js index 7b3cb27a8..c06d9dda5 100644 --- a/instruments/example.js +++ b/instruments/example.js @@ -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); }); }); }); diff --git a/instruments/instruments.js b/instruments/instruments.js index 2e4b83693..90e7d3e3b 100644 --- a/instruments/instruments.js +++ b/instruments/instruments.js @@ -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}); }); };