Skip to content

Commit

Permalink
Stop accessing remote when testing fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
timdream committed Apr 12, 2014
1 parent 3f35823 commit bcc890d
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 7 deletions.
87 changes: 87 additions & 0 deletions test/fake-jsonp-google-feed-api.js
@@ -0,0 +1,87 @@
'use strict';

(function() {
var FakeGoogleFeedAPIPScript = function() {
};

FakeGoogleFeedAPIPScript.prototype.FILENAME = 'fake-jsonp-google-feed-api.js';

FakeGoogleFeedAPIPScript.prototype.RESPONSE = {
'feed': {
'feedUrl': 'http://foo.bar/feed/',
'title': 'foobar',
'link': 'http://foo.bar/',
'author': '',
'description': '',
'type': 'rss20',
'entries': [
{
'title': 'title1',
'link': 'http://foo.bar/1/',
'author': 'author1',
'publishedDate': 'Sun, 16 Mar 2014 18:31:19 +0700',
'contentSnippet': 'snippet1\nsnippet1 line 2',
'content': '<p>content1</p>\n<p>content1 line 2</p>',
'categories': ['foo', 'bar']
},
{
'title': 'title2',
'link': 'http://foo.bar/2/',
'author': 'author2',
'publishedDate': 'Sun, 16 Mar 2014 18:31:19 +0700',
'contentSnippet': 'snippet2\nsnippet2 line 2',
'content': '<p>content2</p>\n<p>content2 line 2</p>',
'categories': ['foo2', 'bar2']
}
]
}
};

FakeGoogleFeedAPIPScript.prototype.getJSONScript = function() {
var scriptEls = document.getElementsByTagName('script');
for (var i = 0; i < scriptEls.length; i++) {
if (scriptEls[i].src.indexOf(this.FILENAME) === -1) {
continue;
}

return scriptEls[i];
}
};

FakeGoogleFeedAPIPScript.prototype.getSearchString = function(scriptEl) {
var urlUtil;
if (typeof window.URL === 'function') {
urlUtil = new window.URL(scriptEl.src, window.location);
} else {
urlUtil = document.createElement('a');
urlUtil.href = scriptEl.src;
}

return urlUtil.search.substr(1);
};

FakeGoogleFeedAPIPScript.prototype.getSearchParams = function(searchString) {
var searchParams = {};

searchString.split('&').forEach(function(keyValue) {
var arr = keyValue.split('=');
searchParams[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1]);
});

return searchParams;
};

FakeGoogleFeedAPIPScript.prototype.getCallbackName = function(searchParams) {
return searchParams['callback'];
};

FakeGoogleFeedAPIPScript.prototype.respond = function() {
var callbackName = this.getCallbackName(
this.getSearchParams(this.getSearchString(this.getJSONScript())));

window[callbackName]('ctx', this.RESPONSE, 200, null, 200);
};

var fakeJSONPScript = new FakeGoogleFeedAPIPScript();
fakeJSONPScript.respond();
})();
60 changes: 60 additions & 0 deletions test/fake-jsonp-hello.js
@@ -0,0 +1,60 @@
'use strict';

(function() {
var FakeJSONPScript = function() {
};

FakeJSONPScript.prototype.FILENAME = 'fake-jsonp-hello.js';

FakeJSONPScript.prototype.RESPONSE = {
'hello': 'world'
};

FakeJSONPScript.prototype.getJSONScript = function() {
var scriptEls = document.getElementsByTagName('script');
for (var i = 0; i < scriptEls.length; i++) {
if (scriptEls[i].src.indexOf(this.FILENAME) === -1) {
continue;
}

return scriptEls[i];
}
};

FakeJSONPScript.prototype.getSearchString = function(scriptEl) {
var urlUtil;
if (typeof window.URL === 'function') {
urlUtil = new window.URL(scriptEl.src, window.location);
} else {
urlUtil = document.createElement('a');
urlUtil.href = scriptEl.src;
}

return urlUtil.search.substr(1);
};

FakeJSONPScript.prototype.getSearchParams = function(searchString) {
var searchParams = {};

searchString.split('&').forEach(function(keyValue) {
var arr = keyValue.split('=');
searchParams[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1]);
});

return searchParams;
};

FakeJSONPScript.prototype.getCallbackName = function(searchParams) {
return searchParams['callback'];
};

FakeJSONPScript.prototype.respond = function() {
var callbackName = this.getCallbackName(
this.getSearchParams(this.getSearchString(this.getJSONScript())));

window[callbackName](this.RESPONSE);
};

var fakeJSONPScript = new FakeJSONPScript();
fakeJSONPScript.respond();
})();
81 changes: 81 additions & 0 deletions test/fake-jsonp-wikipedia-api.js
@@ -0,0 +1,81 @@
'use strict';

(function() {
var FakeJSONPScript = function() {
};

FakeJSONPScript.prototype.FILENAME = 'fake-jsonp-wikipedia-api.js';

FakeJSONPScript.prototype.RESPONSE = {
'query':{
'pages':{
'169409':{
'pageid':169409,
'ns':0,
'title':'Happiness',
'revisions':[
{
'*':'<div class="dablink">Several terms redirect here. ' +
'For other uses, see <a href="/wiki/Happiness_' +
'(disambiguation)" title="Happiness (disambiguation)' +
'">Happiness (disambiguation)</a>, <a href="/wiki/' +
'Happy_(disambiguation)" title="Happy' +
' (disambiguation)">Happy (disambiguation)</a>' +
', and <a href="/wiki/Jolly_(disambiguation)"' +
' title="Jolly (disambiguation)">Jolly' +
' (disambiguation)</a>.</div>'
}
]
}
}
}
};

FakeJSONPScript.prototype.getJSONScript = function() {
var scriptEls = document.getElementsByTagName('script');
for (var i = 0; i < scriptEls.length; i++) {
if (scriptEls[i].src.indexOf(this.FILENAME) === -1) {
continue;
}

return scriptEls[i];
}
};

FakeJSONPScript.prototype.getSearchString = function(scriptEl) {
var urlUtil;
if (typeof window.URL === 'function') {
urlUtil = new window.URL(scriptEl.src, window.location);
} else {
urlUtil = document.createElement('a');
urlUtil.href = scriptEl.src;
}

return urlUtil.search.substr(1);
};

FakeJSONPScript.prototype.getSearchParams = function(searchString) {
var searchParams = {};

searchString.split('&').forEach(function(keyValue) {
var arr = keyValue.split('=');
searchParams[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1]);
});

return searchParams;
};

FakeJSONPScript.prototype.getCallbackName = function(searchParams) {
return searchParams['callback'];
};

FakeJSONPScript.prototype.respond = function() {
var callbackName = this.getCallbackName(
this.getSearchParams(this.getSearchString(this.getJSONScript())));

window[callbackName](this.RESPONSE);
};

var fakeJSONPScript = new FakeJSONPScript();
fakeJSONPScript.respond();
})();
17 changes: 10 additions & 7 deletions test/unit/fetchers.js
Expand Up @@ -203,12 +203,12 @@ module('JSONPFetcher');
test('requestData(url)', function() {
var fetcher = new JSONPFetcher();
fetcher.handleResponse = function handleResponse(res) {
ok(res && res.ip === '127.0.0.1', 'got data.');
ok(res && res.hello === 'world', 'got data.');

start();
};
stop();
fetcher.requestData('http://freegeoip.net/json/127.0.0.1');
fetcher.requestData('./fake-jsonp-hello.js');
});

test('requestData(non-exist API)', function() {
Expand All @@ -219,19 +219,19 @@ test('requestData(non-exist API)', function() {
start();
};
stop();
/* example.com:443 should refuse the connection. */
fetcher.requestData('https://example.com/');
fetcher.requestData('./404');
});

module('FeedFetcher');

test('getData(\'feed\')', function() {
var fetcher = new FeedFetcher();
var data = 'http://blog.timc.idv.tw/feed/';
fetcher.FEED_API_LOAD_URL = './fake-jsonp-google-feed-api.js';
var data = 'http://foo.bar/feed/';
stop();
fetcher.app = {
handleData: function gotData(data) {
ok(!!data, 'Received data, length: ' + data.length);
equal(data, 'title1\ncontent1\ncontent1 line 2\n\ntitle2\ncontent2\ncontent2 line 2\n');

start();
}
Expand All @@ -241,7 +241,8 @@ test('getData(\'feed\')', function() {

test('stop()', function() {
var fetcher = new FeedFetcher();
var data = 'http://blog.timc.idv.tw/feed/';
fetcher.FEED_API_LOAD_URL = './fake-jsonp-google-feed-api.js';
var data = 'http://foo.bar/feed/';
var timer;
stop();
fetcher.app = {
Expand All @@ -265,6 +266,7 @@ module('WikipediaFetcher');

test('getData(\'wikipedia\')', function() {
var fetcher = new WikipediaFetcher();
fetcher.WIKIPEDIA_API_URL = './fake-jsonp-wikipedia-api.js';
var data = 'Happiness';
stop();
fetcher.app = {
Expand All @@ -279,6 +281,7 @@ test('getData(\'wikipedia\')', function() {

test('stop()', function() {
var fetcher = new WikipediaFetcher();
fetcher.WIKIPEDIA_API_URL = './fake-jsonp-wikipedia-api.js';
var data = 'Happiness';
var timer;
stop();
Expand Down

0 comments on commit bcc890d

Please sign in to comment.