diff --git a/add-on/src/lib/dnslink.js b/add-on/src/lib/dnslink.js index c9c44201b..f18bbf686 100644 --- a/add-on/src/lib/dnslink.js +++ b/add-on/src/lib/dnslink.js @@ -59,7 +59,7 @@ module.exports = function createDnslinkResolver (getState) { // to load the correct path from IPFS // - https://github.com/ipfs/ipfs-companion/issues/298 const ipnsPath = dnslinkResolver.convertToIpnsPath(url) - const gateway = state.localGwAvailable ? state.gwURLString : state.pubGwURLString + const gateway = state.redirect && state.localGwAvailable ? state.gwURLString : state.pubGwURLString return pathAtHttpGateway(ipnsPath, gateway) } }, diff --git a/test/functional/lib/dnslink.test.js b/test/functional/lib/dnslink.test.js index 1e52cb763..ee0caa882 100644 --- a/test/functional/lib/dnslink.test.js +++ b/test/functional/lib/dnslink.test.js @@ -1,5 +1,5 @@ 'use strict' -const { describe, it, before, after } = require('mocha') +const { describe, it, before, beforeEach, after } = require('mocha') const { expect } = require('chai') const { URL } = require('url') const sinon = require('sinon') @@ -58,10 +58,15 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () { global.URL = URL }) - const getState = () => Object.assign(initState(testOptions), { - ipfsNodeType: 'external', - dnslinkPolicy: 'detectIpfsPathHeader', - peerCount: 1 + let getState + beforeEach(() => { + // ensure each case uses clean state + getState = () => Object.assign(initState(testOptions), { + ipfsNodeType: 'external', + dnslinkPolicy: 'detectIpfsPathHeader', + redirect: true, + peerCount: 1 + }) }) const getExternalNodeState = () => Object.assign(getState(), { ipfsNodeType: 'external' }) const getEmbeddedNodeState = () => Object.assign(getState(), { ipfsNodeType: 'embedded' }) @@ -86,6 +91,18 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () { expect(dnslinkResolver.dnslinkAtGateway(url.toString())) .to.equal('http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d') }) + it('[external node] should return redirect to public gateway if dnslink is present in cache but redirect to local gw is off', function () { + const oldState = getState + getState = () => Object.assign(oldState(), { redirect: false }) + const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d') + const dnslinkResolver = createDnslinkResolver(getExternalNodeState) + dnslinkResolver.setDnslink(url.hostname, '/ipfs/bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge') + expectNoDnsTxtRecordLookup(url.hostname, dnslinkResolver) + // note: locahost will redirect to subdomain if its go-ipfs >0.5, + // so companion does not need to handle that + expect(dnslinkResolver.dnslinkAtGateway(url.toString())) + .to.equal('https://gateway.foobar.io/ipns/dnslinksite4.io/foo/barl?a=b#c=d') + }) it('[embedded node] should return redirect to public gateway if dnslink is present in cache', function () { const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d') const dnslinkResolver = createDnslinkResolver(getEmbeddedNodeState)