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
\ncontent1 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
\ncontent2 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':[ + { + '*':'