From 9d29f1d943dbfe561558cd46366ad5f7059387d1 Mon Sep 17 00:00:00 2001 From: Fraser Nevett Date: Mon, 29 Jun 2015 18:20:47 +0100 Subject: [PATCH] Fixed #111 -- allow the actor defined within URL configuration to be a Group. --- src/Agent.js | 4 ++++ src/Group.js | 4 ++++ src/TinCan.js | 8 +++++++- test/js/unit/Agent.js | 14 +++++++++++++- test/js/unit/Group.js | 12 ++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Agent.js b/src/Agent.js index 0496f80..1bb8353 100644 --- a/src/Agent.js +++ b/src/Agent.js @@ -289,6 +289,10 @@ TinCan client library Agent.prototype.log("fromJSON"); var _agent = JSON.parse(agentJSON); + if (_agent.hasOwnProperty("objectType") && _agent.objectType !== Agent.prototype.objectType) { + throw "Unexpected object type for agent: " + _agent.objectType; + } + return new Agent(_agent); }; }()); diff --git a/src/Group.js b/src/Group.js index 7338243..cee1961 100644 --- a/src/Group.js +++ b/src/Group.js @@ -156,6 +156,10 @@ TinCan client library Group.prototype.log("fromJSON"); var _group = JSON.parse(groupJSON); + if (_group.hasOwnProperty("objectType") && _group.objectType !== Group.prototype.objectType) { + throw "Unexpected object type for group: " + _group.objectType; + } + return new Group(_group); }; }()); diff --git a/src/TinCan.js b/src/TinCan.js index 9d30ef8..3ecb779 100644 --- a/src/TinCan.js +++ b/src/TinCan.js @@ -211,7 +211,13 @@ var TinCan; delete qsParams.actor; } catch (ex) { - this.log("_initFromQueryString - failed to set actor: " + ex); + try { + this.actor = TinCan.Group.fromJSON(qsParams.actor); + delete qsParams.actor; + } + catch (ex2) { + this.log("_initFromQueryString - failed to set actor: " + ", ".join([ex, ex2])); + } } } diff --git a/test/js/unit/Agent.js b/test/js/unit/Agent.js index 7227856..ecae7df 100644 --- a/test/js/unit/Agent.js +++ b/test/js/unit/Agent.js @@ -29,7 +29,19 @@ ; result = TinCan.Agent.fromJSON(JSON.stringify(raw)); - ok(result instanceof TinCan.Agent, "returns TinCan.Agent"); + ok(result instanceof TinCan.Agent, "returns TinCan.Agent (objectType omitted)"); + + raw.objectType = 'Agent'; + result = TinCan.Agent.fromJSON(JSON.stringify(raw)); + ok(result instanceof TinCan.Agent, "returns TinCan.Agent (objectType set explicitly)"); + + raw.objectType = 'invalid'; + throws( + function () { + TinCan.Agent.fromJSON(JSON.stringify(raw)); + }, + "exception on invalid objectType" + ); } ); diff --git a/test/js/unit/Group.js b/test/js/unit/Group.js index a16092d..f436c7d 100644 --- a/test/js/unit/Group.js +++ b/test/js/unit/Group.js @@ -34,6 +34,18 @@ result = TinCan.Group.fromJSON(JSON.stringify(raw)); ok(result instanceof TinCan.Group, "returns TinCan.Group"); + + raw.objectType = 'Group'; + result = TinCan.Group.fromJSON(JSON.stringify(raw)); + ok(result instanceof TinCan.Group, "returns TinCan.Group (objectType set explicitly)"); + + raw.objectType = 'invalid'; + throws( + function () { + TinCan.Group.fromJSON(JSON.stringify(raw)); + }, + "exception on invalid objectType" + ); } );