1
1
'use strict'
2
- const { describe, it, before, after } = require ( 'mocha' )
2
+ const { describe, it, before, beforeEach , after } = require ( 'mocha' )
3
3
const { expect } = require ( 'chai' )
4
4
const { URL } = require ( 'url' )
5
5
const sinon = require ( 'sinon' )
@@ -58,10 +58,15 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
58
58
global . URL = URL
59
59
} )
60
60
61
- const getState = ( ) => Object . assign ( initState ( testOptions ) , {
62
- ipfsNodeType : 'external' ,
63
- dnslinkPolicy : 'detectIpfsPathHeader' ,
64
- peerCount : 1
61
+ let getState
62
+ beforeEach ( ( ) => {
63
+ // ensure each case uses clean state
64
+ getState = ( ) => Object . assign ( initState ( testOptions ) , {
65
+ ipfsNodeType : 'external' ,
66
+ dnslinkPolicy : 'detectIpfsPathHeader' ,
67
+ redirect : true ,
68
+ peerCount : 1
69
+ } )
65
70
} )
66
71
const getExternalNodeState = ( ) => Object . assign ( getState ( ) , { ipfsNodeType : 'external' } )
67
72
const getEmbeddedNodeState = ( ) => Object . assign ( getState ( ) , { ipfsNodeType : 'embedded' } )
@@ -86,6 +91,18 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
86
91
expect ( dnslinkResolver . dnslinkAtGateway ( url . toString ( ) ) )
87
92
. to . equal ( 'http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d' )
88
93
} )
94
+ it ( '[external node] should return redirect to public gateway if dnslink is present in cache but redirect to local gw is off' , function ( ) {
95
+ const oldState = getState
96
+ getState = ( ) => Object . assign ( oldState ( ) , { redirect : false } )
97
+ const url = new URL ( 'https://dnslinksite4.io/foo/barl?a=b#c=d' )
98
+ const dnslinkResolver = createDnslinkResolver ( getExternalNodeState )
99
+ dnslinkResolver . setDnslink ( url . hostname , '/ipfs/bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge' )
100
+ expectNoDnsTxtRecordLookup ( url . hostname , dnslinkResolver )
101
+ // note: locahost will redirect to subdomain if its go-ipfs >0.5,
102
+ // so companion does not need to handle that
103
+ expect ( dnslinkResolver . dnslinkAtGateway ( url . toString ( ) ) )
104
+ . to . equal ( 'https://gateway.foobar.io/ipns/dnslinksite4.io/foo/barl?a=b#c=d' )
105
+ } )
89
106
it ( '[embedded node] should return redirect to public gateway if dnslink is present in cache' , function ( ) {
90
107
const url = new URL ( 'https://dnslinksite4.io/foo/barl?a=b#c=d' )
91
108
const dnslinkResolver = createDnslinkResolver ( getEmbeddedNodeState )
0 commit comments