Skip to content

Commit

Permalink
refactor: switch dnslink tests to subrequests
Browse files Browse the repository at this point in the history
The request for main page (request.type=main_request) is often optimized
early by preloading DNSLink etc.

We switch tests to one of subrequest types to ensure the test is not
impacted by main_test logic.
  • Loading branch information
lidel committed Feb 23, 2019
1 parent ba20aa7 commit 8d1f3b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
ignore(request.requestId)
}
// skip if a per-site redirect opt-out exists
const parentUrl = request.originUrl || request.initiator // FF: originUrl, Chrome: initiator
const parentUrl = request.originUrl || request.initiator // FF: originUrl (Referer-like Origin URL), Chrome: initiator (just Origin)
const fqdn = new URL(request.url).hostname
const parentFqdn = parentUrl && request.url !== parentUrl ? new URL(parentUrl).hostname : null
if (state.noRedirectHostnames.some(optout =>
Expand All @@ -76,9 +76,9 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
}
// additional checks limited to requests for root documents
if (request.type === 'main_frame') {
// trigger DNSLink lookup if status for root domain is not in cache yet
// lazily trigger DNSLink lookup (will do anything only if status for root domain is not in cache)
if (state.dnslinkPolicy && dnslinkResolver.canLookupURL(request.url)) {
(async () => dnslinkResolver.readAndCacheDnslink(parentFqdn || fqdn))()
dnslinkResolver.preloadDnslink(request.url)
}
}
return isIgnored(request.requestId)
Expand Down
5 changes: 4 additions & 1 deletion test/functional/lib/ipfs-request-dnslink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,15 @@ describe('modifyRequest processing', function () {
// force-clear dnslink cache to enable cache miss
dnslinkResolver.clearCache()
})
it('should redirect in onHeadersReceived if DNS TXT record exists and x-ipfs-path header is present', function () {
it('should redirect subrequests in onHeadersReceived if DNS TXT record exists and x-ipfs-path header is present', function () {
// clear dnslink cache to ensure miss
dnslinkResolver.clearCache()
// stub existence of a valid DNS record
const fqdn = 'explore.ipld.io'
dnslinkResolver.readDnslinkFromTxtRecord = sinon.stub().withArgs(fqdn).returns('/ipfs/QmbfimSwTuCvGL8XBr3yk1iCjqgk2co2n21cWmcQohymDd')
//
const request = url2request('http://explore.ipld.io/index.html?argTest#hashTest')
request.type = 'sub_frame' // we test a subrequests because main_frame gets early DNSLink preload in onBeforeRequest
expect(modifyRequest.onBeforeRequest(request)).to.equal(undefined)
// simulate presence of x-ipfs-path header returned by HTTP gateway
request.responseHeaders = [{ name: 'X-Ipfs-Path', value: '/ipfs/QmbfimSwTuCvGL8XBr3yk1iCjqgk2co2n21cWmcQohymDd' }]
Expand All @@ -241,6 +242,7 @@ describe('modifyRequest processing', function () {
dnslinkResolver.readDnslinkFromTxtRecord = sinon.stub().withArgs(fqdn).returns(false)
//
const request = url2request('http://explore.ipld.io/index.html?argTest#hashTest')
request.type = 'sub_frame' // we test a subrequest here because main_frame gets early DNSLink preload in onBeforeRequest
expect(modifyRequest.onBeforeRequest(request)).to.equal(undefined)
// simulate presence of x-ipfs-path header returned by HTTP gateway
request.responseHeaders = [{ name: 'X-Ipfs-Path', value: '/ipfs/QmbfimSwTuCvGL8XBr3yk1iCjqgk2co2n21cWmcQohymDd' }]
Expand All @@ -255,6 +257,7 @@ describe('modifyRequest processing', function () {
dnslinkResolver.readDnslinkFromTxtRecord = sinon.stub().withArgs(fqdn).returns('/ipfs/QmbfimSwTuCvGL8XBr3yk1iCjqgk2co2n21cWmcQohymDd')
//
const request = url2request('http://explore.ipld.io/index.html?argTest#hashTest')
request.type = 'sub_frame' // we test a subrequest here because main_frame gets early DNSLink preload in onBeforeRequest
expect(modifyRequest.onBeforeRequest(request)).to.equal(undefined)
expect(modifyRequest.onHeadersReceived(request)).to.equal(undefined)
})
Expand Down

0 comments on commit 8d1f3b3

Please sign in to comment.