diff --git a/test/fake-jsonp-google-feed-api.js b/test/fake-jsonp-google-feed-api.js new file mode 100644 index 0000000..c54b3bd --- /dev/null +++ b/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': '

content1

\n

content1 line 2

', + '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': '

content2

\n

content2 line 2

', + '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(); +})(); diff --git a/test/fake-jsonp-hello.js b/test/fake-jsonp-hello.js new file mode 100644 index 0000000..57f53d9 --- /dev/null +++ b/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(); +})(); diff --git a/test/fake-jsonp-wikipedia-api.js b/test/fake-jsonp-wikipedia-api.js new file mode 100644 index 0000000..f84b6a1 --- /dev/null +++ b/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':[ + { + '*':'' + } + ] + } + } + } + }; + + 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(); +})(); diff --git a/test/unit/fetchers.js b/test/unit/fetchers.js index bdd36f8..dfcff55 100644 --- a/test/unit/fetchers.js +++ b/test/unit/fetchers.js @@ -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() { @@ -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(); } @@ -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 = { @@ -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 = { @@ -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();