-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: recover from failed HTTP requests to third party gateways #783
Merged
lidel
merged 9 commits into
ipfs:master
from
colinfruit:recover-dead-public-gateway-links
Oct 17, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7111845
recover dead public gateways to public gateway url
colinfruit 3420a55
add experimental option to enable recover of broken requests to third…
colinfruit 642a8ec
add tests for request recovery via public gateway
colinfruit 05a0090
rename recoverViaPublicGateway => recoverFailedHttpRequests
colinfruit 7298d05
update recovery tests
colinfruit 3a89f1e
rename recoverableViaPubGw => recoverable
colinfruit ff43eff
update messages
colinfruit 6d6f0cc
update log message on recovering failed request
colinfruit 3998391
modify failed http request description to match style of other messages
colinfruit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
163 changes: 163 additions & 0 deletions
163
test/functional/lib/ipfs-request-gateway-recover.test.js
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,163 @@ | ||
'use strict' | ||
const { describe, it, before, beforeEach, after, afterEach } = require('mocha') | ||
const sinon = require('sinon') | ||
const { assert } = require('chai') | ||
const { URL } = require('url') | ||
const browser = require('sinon-chrome') | ||
const { initState } = require('../../../add-on/src/lib/state') | ||
const { createRuntimeChecks } = require('../../../add-on/src/lib/runtime-checks') | ||
const { createRequestModifier } = require('../../../add-on/src/lib/ipfs-request') | ||
const createDnslinkResolver = require('../../../add-on/src/lib/dnslink') | ||
const { createIpfsPathValidator } = require('../../../add-on/src/lib/ipfs-path') | ||
const { optionDefaults } = require('../../../add-on/src/lib/options') | ||
|
||
const url2request = (url, type = 'main_frame') => { | ||
return { url, type } | ||
} | ||
const urlRequestWithStatus = (url, statusCode = 200, type = 'main_frame') => { | ||
return { ...url2request(url, type), statusCode } | ||
} | ||
|
||
describe('requestHandler.onCompleted:', function () { | ||
let state, dnslinkResolver, ipfsPathValidator, requestHandler, runtime | ||
|
||
before(function () { | ||
global.URL = URL | ||
browser.tabs = { ...browser.tabs, query: sinon.stub().resolves([{ id: 20 }]) } | ||
global.browser = browser | ||
}) | ||
|
||
beforeEach(async function () { | ||
state = initState(optionDefaults) | ||
const getState = () => state | ||
const getIpfs = () => {} | ||
dnslinkResolver = createDnslinkResolver(getState) | ||
runtime = Object.assign({}, await createRuntimeChecks(browser)) // make it mutable for tests | ||
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver) | ||
requestHandler = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime) | ||
}) | ||
|
||
describe('with recoverFailedHttpRequests=true', function () { | ||
beforeEach(function () { | ||
state.recoverFailedHttpRequests = true | ||
}) | ||
it('should do nothing if broken request is a non-IPFS request', async function () { | ||
const request = urlRequestWithStatus('https://wikipedia.org', 500) | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if broken request is a non-public IPFS request', async function () { | ||
const request = urlRequestWithStatus('http://127.0.0.1:8080/ipfs/QmYzZgeWE7r8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if broken request is to the default public gateway', async function () { | ||
const request = urlRequestWithStatus('https://ipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if broken request is not a \'main_frame\' request', async function () { | ||
const request = urlRequestWithStatus('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500, 'stylesheet') | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should redirect broken non-default public gateway IPFS request to public gateway', async function () { | ||
const request = urlRequestWithStatus('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.withArgs({ url: 'https://ipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', active: true, openerTabId: 20 }).calledOnce, 'tabs.create should be called with IPFS default public gateway URL') | ||
}) | ||
}) | ||
|
||
describe('with recoverFailedHttpRequests=false', function () { | ||
beforeEach(function () { | ||
state.recoverFailedHttpRequests = false | ||
}) | ||
it('should do nothing on broken non-default public gateway IPFS request', async function () { | ||
const request = urlRequestWithStatus('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onCompleted(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
}) | ||
|
||
afterEach(function () { | ||
browser.tabs.create.reset() | ||
}) | ||
|
||
after(function () { | ||
delete global.url | ||
delete global.browser | ||
browser.flush() | ||
}) | ||
}) | ||
|
||
describe('requestHandler.onErrorOccurred:', function () { | ||
let state, dnslinkResolver, ipfsPathValidator, requestHandler, runtime | ||
|
||
before(function () { | ||
global.URL = URL | ||
browser.tabs = { ...browser.tabs, query: sinon.stub().resolves([{ id: 20 }]) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise this query will throw an error. |
||
global.browser = browser | ||
}) | ||
|
||
beforeEach(async function () { | ||
state = initState(optionDefaults) | ||
const getState = () => state | ||
const getIpfs = () => {} | ||
dnslinkResolver = createDnslinkResolver(getState) | ||
runtime = Object.assign({}, await createRuntimeChecks(browser)) // make it mutable for tests | ||
ipfsPathValidator = createIpfsPathValidator(getState, getIpfs, dnslinkResolver) | ||
requestHandler = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime) | ||
}) | ||
|
||
describe('with recoverFailedHttpRequests=true', function () { | ||
beforeEach(function () { | ||
state.recoverFailedHttpRequests = true | ||
}) | ||
it('should do nothing if failed request is a non-IPFS request', async function () { | ||
const request = url2request('https://wikipedia.org', 500) | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if failed request is a non-public IPFS request', async function () { | ||
const request = url2request('http://127.0.0.1:8080/ipfs/QmYzZgeWE7r8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if failed request is to the default public gateway', async function () { | ||
const request = url2request('https://ipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 500) | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should do nothing if failed request is not a \'main_frame\' request', async function () { | ||
const request = url2request('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', 'stylesheet') | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
it('should redirect failed non-default public gateway IPFS request to public gateway', async function () { | ||
const request = url2request('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h') | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.withArgs({ url: 'https://ipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h', active: true, openerTabId: 20 }).calledOnce, 'tabs.create should be called with IPFS default public gateway URL') | ||
}) | ||
}) | ||
|
||
describe('with recoverFailedHttpRequests=false', function () { | ||
beforeEach(function () { | ||
state.recoverFailedHttpRequests = false | ||
}) | ||
it('should do nothing on failed non-default public gateway IPFS request', async function () { | ||
const request = url2request('https://nondefaultipfs.io/ipfs/QmYbZgeWE7y8HXkH8zbb8J9ddHQvp8LTqm6isL791eo14h') | ||
await requestHandler.onErrorOccurred(request) | ||
assert.ok(browser.tabs.create.notCalled, 'tabs.create should not be called') | ||
}) | ||
}) | ||
|
||
afterEach(function () { | ||
browser.tabs.create.reset() | ||
}) | ||
|
||
after(function () { | ||
delete global.url | ||
delete global.browser | ||
browser.flush() | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we drop
redirect
and useredirectUrl
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is the cleanest way to do this. Without it we would have to worry about the dnslink redirect, and do something like
const redirectUrl = redirect ? redirect.redirectUrl : null
to be able to use this with the samecreateTabWithURL
function.