Permalink
Browse files

adds testing for jsonp

  • Loading branch information...
1 parent fb44e56 commit 3401f737cf4a039ee2631c6664471c4672106954 @MathiasPaumgarten committed Sep 15, 2013
Showing with 135 additions and 24 deletions.
  1. +3 −3 CONTRIBUTING.md
  2. +20 −2 Gruntfile.js
  3. +3 −3 docs/net.md
  4. +4 −0 karma.config.js
  5. +1 −0 package.json
  6. +20 −0 server/main.js
  7. +8 −15 source/net/jsonp.js
  8. +76 −1 tests/net/spec-jsonp.js
View
@@ -12,10 +12,10 @@ Build a Pull Request
* Every PR should only include one feature or patch. If you have multiple
contributions, make it multiple PRs.
-* Make sure the code is well tested. While you're add it, make sure it's not
+* Make sure the code is well tested. While you're at it, make sure it's not
breaking any existing tests.
-* Proper commit messages and Try to keep the commit history clean.
-* Make sure the code is JSHinted. To do this automatically check out the
+* Proper commit messages and try to keep the commit history clean.
+* Make sure the code is JSHinted. To do this automatically, check out the
_Development_ section
__Note__: If you are proposing a major change, please open an issues before you
View
@@ -28,6 +28,15 @@ module.exports = function(grunt) {
}
},
+ server: {
+ options: {
+ node: true
+ },
+ files: {
+ src: ["server/**/*.js"]
+ }
+ },
+
tests: {
options: {
browser: true,
@@ -81,6 +90,11 @@ module.exports = function(grunt) {
tasks: ["jshint:source", "karma:dev:run"]
},
+ server: {
+ files: ["server/**/*.js"],
+ tasks: ["jshint:server"]
+ },
+
tests: {
files: ["tests/**/*.js", "!tests/runner.js"],
tasks: ["jshint:tests", "karma:dev:run"]
@@ -116,9 +130,9 @@ module.exports = function(grunt) {
"grunt-karma"
].forEach(grunt.loadNpmTasks);
- grunt.registerTask("default", ["karma:dev", "watch"]);
+ grunt.registerTask("default", ["server", "karma:dev", "watch"]);
grunt.registerTask("docs", ["mdoc"]);
- grunt.registerTask("test", ["jshint", "karma:test"]);
+ grunt.registerTask("test", ["jshint", "server", "karma:test"]);
grunt.registerMultiTask("mdoc", function() {
var opts = this.options(),
@@ -132,4 +146,8 @@ module.exports = function(grunt) {
});
});
+ grunt.registerTask("server", function() {
+ require("./server/main");
+ });
+
};
View
@@ -14,11 +14,11 @@ See also: [`request`](#request)
## jsonp(url, callback, [parameters])
-This module helps loading JSON content usen JSON-P methods. For more insight on JSON-P check out: [JSON-P](http://en.wikipedia.org/wiki/JSONP).
+This module helps loading JSON content using JSON-P methods. For more insight on JSON-P check out: [JSON-P](http://en.wikipedia.org/wiki/JSONP).
-* `url`: representing the path to the resource to be loaded without parameters.
+* `url`: representing the path to the resource (without parameters).
* `callback`: Function called with the json from the backend as it's only parameter.
-* `paramters`: Optional set of parameters. The content of this will be url encoded using `mout/queryString/encode`.
+* `parameters`: Optional set of parameters. The content of this will be url encoded using `mout/queryString/encode`.
```js
var parameters = {
View
@@ -27,3 +27,7 @@ autoWatch = true;
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
+
+proxies = {
+ "/express": "http://localhost:9000"
+};
View
@@ -46,6 +46,7 @@
"grunt-karma": "~0.4.4",
"grunt-contrib-watch": "~0.4.3",
"grunt-cli": "~0.1.8",
+ "express": "~3.4.0",
"mout": "~0.5.0",
"mdoc": "~0.3.4",
"sinon": "~1.7.3"
View
@@ -0,0 +1,20 @@
+var express = require("express");
+var clone = require("mout/lang/clone");
+var app = express();
+
+app.get("/jsonp/static", function(request, result) {
+ result.jsonp({
+ foo: "bar"
+ });
+});
+
+app.get("/jsonp/mirror", function(request, result) {
+ var params = clone(request.query);
+ delete params.callback;
+
+ result.jsonp(params);
+});
+
+app.listen(9000);
+
+module.exports = app;
View
@@ -3,32 +3,25 @@ define([
"../dom/append",
"../dom/remove",
"mout/queryString/encode",
- "mout/object/merge"
-], function(create, append, remove, encode, merge) {
+ "mout/object/merge",
+ "mout/array/remove"
+], function(create, append, domRemove, encode, merge, arrayRemove) {
- var usedName = [];
+ var usedNames = [];
function generateName() {
var timestamp = Date.now();
var name = "jsonp" + timestamp;
- while (usedName.indexOf(name) > -1) {
+ while (usedNames.indexOf(name) > -1) {
name = "jsonp" + (++timestamp);
}
- usedName.push(name);
+ usedNames.push(name);
return name;
}
- function freeName(name) {
- var index = usedName.indexOf( name );
-
- if ( index > -1 ) {
- usedName.splice(index, 1);
- }
- }
-
function jsonp(url, callback, parameters) {
parameters = parameters || {};
@@ -40,8 +33,8 @@ define([
window[callbackName] = function(data) {
delete window[callbackName];
- freeName(callbackName);
- remove(script);
+ arrayRemove(usedNames, callbackName);
+ domRemove(script);
callback(data);
};
View
@@ -1 +1,76 @@
-// not sure how to test this since a backend is required.
+define(["cane/net/jsonp"], function(jsonp) {
+
+ describe("net/jsonp()", function() {
+
+ it("should call resource using jsonp", function() {
+ var completed = false;
+ var content = null;
+
+ jsonp("/express/jsonp/static", function(data) {
+ completed = true;
+ content = data;
+ });
+
+ waitsFor(function() {
+ return completed;
+ }, "Request never completed", 10000);
+
+ runs(function() {
+ expect(content).toEqual({ "foo": "bar" });
+ });
+ });
+
+ it("should send data", function() {
+ var completed = false;
+ var content = null;
+ var parameters = {
+ foo: "complex string & characters like %^*",
+ bar: "another value"
+ };
+
+ function onComplete(data) {
+ completed = true;
+ content = data;
+ }
+
+ jsonp("/express/jsonp/mirror", onComplete, parameters);
+
+ waitsFor(function() {
+ return completed;
+ }, "Request never completed", 10000);
+
+ runs(function() {
+ expect(content).toEqual(parameters);
+ });
+ });
+
+ it("should manage multiple requests parallel", function() {
+
+ var complete = [false, false, false];
+ var content = [];
+
+ function onComplete(index) {
+ return function(data) {
+ complete[index] = true;
+ content[index] = data;
+ };
+ }
+
+ jsonp("/express/jsonp/mirror", onComplete(0), { value: "one" });
+ jsonp("/express/jsonp/mirror", onComplete(1), { value: "two" });
+ jsonp("/express/jsonp/mirror", onComplete(2), { value: "three" });
+
+ waitsFor(function() {
+ return complete[0] && complete[1] && complete[2];
+ }, "Reqeust never completed", 10000 );
+
+ runs(function() {
+ expect(content[0]).toEqual({ value: "one" });
+ expect(content[1]).toEqual({ value: "two" });
+ expect(content[2]).toEqual({ value: "three" });
+ });
+ });
+
+ });
+
+});

0 comments on commit 3401f73

Please sign in to comment.