|
1 |
| -import { expect } from 'chai' |
2 |
| -import { before, describe, it } from 'mocha' |
| 1 | +import {expect} from 'chai' |
| 2 | +import {before, describe, it} from 'mocha' |
3 | 3 | import sinon from 'sinon'
|
4 | 4 | import browserMock from 'sinon-chrome'
|
5 | 5 |
|
6 |
| -import { optionDefaults } from '../../../../add-on/src/lib/options.js' |
7 |
| -import { addRuleToDynamicRuleSetGenerator, isLocalHost } from '../../../../add-on/src/lib/redirect-handler/blockOrObserve' |
8 |
| -import { initState } from '../../../../add-on/src/lib/state.js' |
| 6 | +import {optionDefaults} from '../../../../add-on/src/lib/options.js' |
| 7 | +import {addRuleToDynamicRuleSetGenerator, cleanupRules, isLocalHost} from '../../../../add-on/src/lib/redirect-handler/blockOrObserve' |
| 8 | +import {initState} from '../../../../add-on/src/lib/state.js' |
9 | 9 | import DeclarativeNetRequestMock from './declarativeNetRequest.mock.js'
|
10 | 10 |
|
11 | 11 | const dynamicRulesConditions = (regexFilter) => ({
|
@@ -64,10 +64,14 @@ describe('lib/redirect-handler/blockOrObserve', () => {
|
64 | 64 | addRuleToDynamicRuleSet = addRuleToDynamicRuleSetGenerator(() => state)
|
65 | 65 | })
|
66 | 66 |
|
67 |
| - beforeEach(() => { |
| 67 | + beforeEach(async () => { |
68 | 68 | sinonSandbox.restore()
|
69 |
| - browserMock.flush() |
70 |
| - browserMock.tabs.query.resolves([{ id: 1234 }]) |
| 69 | + browserMock.tabs.query.resetHistory() |
| 70 | + browserMock.tabs.reload.resetHistory() |
| 71 | + browserMock.declarativeNetRequest = sinonSandbox.spy(new DeclarativeNetRequestMock()) |
| 72 | + // this cleans up the rules from the previous test stored in memory. |
| 73 | + await cleanupRules(true) |
| 74 | + // this is just to reset the call count. |
71 | 75 | browserMock.declarativeNetRequest = sinonSandbox.spy(new DeclarativeNetRequestMock())
|
72 | 76 | })
|
73 | 77 |
|
@@ -126,13 +130,34 @@ describe('lib/redirect-handler/blockOrObserve', () => {
|
126 | 130 | expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$'))
|
127 | 131 | })
|
128 | 132 |
|
| 133 | + it('Should add redirect for local gateway where originUrl is similar to redirectUrl and is not https', async () => { |
| 134 | + await addRuleToDynamicRuleSet({ |
| 135 | + originUrl: 'http://docs.ipfs.tech', |
| 136 | + redirectUrl: 'http://localhost:8080/ipns/docs.ipfs.tech' |
| 137 | + }) |
| 138 | + expect(browserMock.declarativeNetRequest.updateDynamicRules.called).to.be.true |
| 139 | + const [{ addRules, removeRuleIds }] = browserMock.declarativeNetRequest.updateDynamicRules.firstCall.args |
| 140 | + expect(removeRuleIds).to.deep.equal([]) |
| 141 | + expect(addRules).to.have.lengthOf(1) |
| 142 | + const [{ id, priority, action, condition }] = addRules |
| 143 | + expect(id).to.be.a('number') |
| 144 | + expect(priority).to.equal(1) |
| 145 | + expect(action).to.deep.equal({ |
| 146 | + type: 'redirect', redirect: { |
| 147 | + "regexSubstitution": "http://localhost:8080/ipns/docs.ipfs.tech\\1" |
| 148 | + } |
| 149 | + }) |
| 150 | + expect(condition).to.deep.equal(dynamicRulesConditions('^https?\\:\\/\\/docs\\.ipfs\\.tech((?:[^\\.]|$).*)$')) |
| 151 | + }) |
| 152 | + |
129 | 153 | it('Should refresh the tab when redirect URL is added', async () => {
|
| 154 | + browserMock.tabs.query.resolves([{id: 1234}]) |
130 | 155 | await addRuleToDynamicRuleSet({
|
131 | 156 | originUrl: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org',
|
132 | 157 | redirectUrl: 'http://localhost:8080/ipns/en.wikipedia-on-ipfs.org'
|
133 | 158 | })
|
134 |
| - expect(browserMock.tabs.query.calledWith({ url: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org' })).to.be.true |
135 |
| - expect(browserMock.tabs.reload.calledWith(1234)).to.be.true |
| 159 | + sinon.assert.calledWith(browserMock.tabs.query, { url: 'https://ipfs.io/ipns/en.wikipedia-on-ipfs.org*' }) |
| 160 | + sinon.assert.calledWith(browserMock.tabs.reload, 1234) |
136 | 161 | })
|
137 | 162 | })
|
138 | 163 | })
|
0 commit comments