Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop accessing remote when testing fetchers
- Loading branch information
Showing
4 changed files
with
238 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters