Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

condense and cleanup for jsonp test

  • Loading branch information...
commit 3d7c2f6b355dc660457b5645b172cdd72f011434 1 parent 483e6d0
Elijah Insua authored
Showing with 32 additions and 62 deletions.
  1. +32 −62 test/jsonp/jsonp.js
94 test/jsonp/jsonp.js
View
@@ -1,73 +1,43 @@
/*
* Test making a jsonp request from jsdom window using jqery.
*/
-var jsdom = require('../../lib/jsdom'),
- url = require('url'),
- targetUrl = "http://localhost:43213?jsoncallback=?",
- assert = require('assert'),
- server = require("http").createServer(function(req, res) {
- res.writeHead(200);
- var u = url.parse(req.url);
- var q = require('querystring').parse(u.query);
- if(q.jsoncallback){
- res.write(q.jsoncallback + "({'message':'jsonp works!'});");
- }
- res.end();
- }),
- jQueryFile = __dirname + "/jquery-1.6.4.min.js";
-
+var
+jsdom = require('../../lib/jsdom'),
+url = require('url'),
+jQueryFile = __dirname + "/jquery-1.6.4.min.js";
exports.tests = {
- test_jquery_getJSON : function(test){
-
- var done = false;
-
- // Allow a the jsonp request a few seconds to complete.
- var wait = function(time, maxTime){
- if(done){
- complete(true);
- return;
- }
- if(time<maxTime){
- setTimeout(function(){wait(time+100, maxTime);}, 100);
- }else{
- complete(false, 'expected jsonp to complete within ' + maxTime + ' milliseconds');
- }
- }
+ test_jquery_getJSON : function(test){
- var complete = function(success, message){
- if(done){
- return;
- }
- done=true;
- assert.ok(success, message);
+ var server = require("http").createServer(function(req, res) {
+ res.writeHead(200);
+ var u = url.parse(req.url);
+ var q = require('querystring').parse(u.query);
+ res.write(q.jsoncallback + "({'message':'jsonp works!'});");
+ res.end();
+ });
+
+ server.listen(43213, '127.0.0.1', function() {
+ jsdom.env({
+ html : "<html><head></head><body></body></html>",
+ scripts : [jQueryFile],
+ features : {
+ FetchExternalResources : ['script'],
+ ProcessExternalResources: ['script']
+ },
+ done : function(errors, window){
+ if(errors) {
+ test.fail('jsdom setup failed');
+ }
+
+ window.jQuery.getJSON('http://localhost:43213?jsoncallback=?', function(data) {
+ test.equal(data.message, 'jsonp works!');
server.close();
test.done();
- }
-
- var runTest = function(){
- jsdom.env({
- html : "<html><head></head><body></body></html>",
- scripts : [jQueryFile],
- features : {
- FetchExternalResources : ['script'],
- ProcessExternalResources: ['script']
- },
- done : function(errors, window){
- var ok = false;
- if(errors) {
- complete(false, 'jsdom setup failed: ');
- }
- window.jQuery.getJSON(targetUrl, function(data) {
- assert.equal(data.message, 'jsonp works!');
- complete(true);
- });
- }
});
- };
-
- // run the test against the server
- server.listen(43213, '127.0.0.1', runTest);
- }
+ }
+ });
+ });
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.