simov il y a 7 ans
Parent
commit
2bd235c5ba
4 fichiers modifiés avec 116 ajouts et 52 suppressions
  1. 1 1
      background/detect.js
  2. 66 16
      test/advanced-csp.js
  3. 22 25
      test/advanced-defaults.js
  4. 27 10
      test/advanced-origins.js

+ 1 - 1
background/detect.js

@@ -66,7 +66,7 @@ md.detect = ({storage: {state}, inject}) => {
         state.origins['*://*']
     }
 
-    if (origin && new RegExp(origin.match).test(location.href)) {
+    if (origin && origin.match && new RegExp(origin.match).test(location.href)) {
       return origin
     }
   }

+ 66 - 16
test/advanced-csp.js

@@ -15,9 +15,17 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
     // await advanced.waitFor(() => document.querySelectorAll('.m-list li').length === 2)
     await advanced.waitFor(200)
 
+    // expand origin
+    if (!await advanced.evaluate(() =>
+      document.querySelector('.m-list li:nth-of-type(2)')
+        .classList.contains('m-exapanded')))
+    {
+      await advanced.click('.m-list li:nth-of-type(2)')
+    }
+
     // disable csp
-    if (await advanced.evaluate(() => state.csp)) {
-      await advanced.click('.m-switch:nth-of-type(2)')
+    if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+      await advanced.click('.m-list li:nth-of-type(2) .m-switch')
     }
 
     // enable path matching
@@ -36,8 +44,8 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
       await advanced.bringToFront()
 
       // enable csp
-      if (!await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
       await advanced.reload()
 
@@ -46,9 +54,12 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
       // await advanced.waitFor(100)
       await advanced.waitFor(200)
 
+      // expand origin
+      await advanced.click('.m-list li:nth-of-type(2)')
+
       t.strictEqual(
         await advanced.evaluate(() =>
-          document.querySelector('.m-switch:nth-of-type(2)')
+          document.querySelector('.m-list li:nth-of-type(2) .m-switch')
             .classList.contains('is-checked')
         ),
         true,
@@ -56,8 +67,8 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
       )
 
       // disable csp
-      if (await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
       await advanced.reload()
       // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
@@ -65,9 +76,12 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
       // await advanced.waitFor(100)
       await advanced.waitFor(200)
 
+      // expand origin
+      await advanced.click('.m-list li:nth-of-type(2)')
+
       t.strictEqual(
         await advanced.evaluate(() =>
-          document.querySelector('.m-switch:nth-of-type(2)')
+          document.querySelector('.m-list li:nth-of-type(2) .m-switch')
             .classList.contains('is-checked')
         ),
         false,
@@ -79,9 +93,18 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
   describe('strip csp header only on matching content type or url', () => {
     it('non matching urls should be skipped', async () => {
       await advanced.bringToFront()
+
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // enable csp
-      if (!await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
 
       // go to page serving content with strict csp
@@ -109,9 +132,18 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
   describe('enable csp', () => {
     it('webRequest.onHeadersReceived event is enabled', async () => {
       await advanced.bringToFront()
+
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // enable csp
-      if (!await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
 
       // go to page serving content with strict csp
@@ -132,9 +164,18 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
   describe('disable csp', () => {
     it('webRequest.onHeadersReceived event is disabled', async () => {
       await advanced.bringToFront()
+
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // disable csp
-      if (await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
 
       // go to page serving content with strict csp
@@ -163,9 +204,18 @@ module.exports = ({browser, extensions, popup, advanced, content}) => {
   describe('enable csp + suspend the event page', () => {
     it('the tab is reloaded on event page wakeup', async () => {
       await advanced.bringToFront()
+
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // enable csp
-      if (!await advanced.evaluate(() => state.csp)) {
-        await advanced.click('.m-switch:nth-of-type(2)')
+      if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
       }
 
       await extensions.bringToFront()

+ 22 - 25
test/advanced-defaults.js

@@ -28,43 +28,40 @@ module.exports = ({advanced}) => {
     )
   })
 
-  it('csp option', async () => {
-    t.strictEqual(
-      await advanced.evaluate(() =>
-        state.csp
-      ),
-      false,
-      'state.csp should be false'
-    )
-  })
-
   it('allowed origins', async () => {
     t.deepStrictEqual(
       await advanced.evaluate(() =>
         state.origins
       ),
       {
-        'file://': '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\\?.*)?$'
+        'file://': {
+          match: '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\\?.*)?$',
+          csp: false,
+          encoding: ''
+        }
       },
-      'state.origins should contain only the file:// origin'
+      'state.origins should contain the file:// origin'
     )
-
-    t.deepStrictEqual(
+    t.equal(
       await advanced.evaluate(() =>
-        state.origins
+        document.querySelectorAll('.m-list li').length
+      ),
+      1,
+      'should contain only one origin'
+    )
+    t.equal(
+      await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(1) .m-origin').innerText
       ),
+      'file://',
+      'origin name should be file://'
+    )
+    t.equal(
       await advanced.evaluate(() =>
-        Array.from(document.querySelectorAll('.m-list li'))
-          .reduce((obj, origin) => (
-            obj[
-              origin.querySelector('span:nth-of-type(1)').innerText.trim() +
-              '://' +
-              origin.querySelector('span:nth-of-type(2)').innerText.trim()
-            ] = origin.querySelector('.m-textfield input').value,
-            obj
-          ), {})
+        document.querySelector('.m-list li:nth-of-type(1) .m-match input').value
       ),
-      'state.origins should be identical to dom origins'
+      '\\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text)(?:#.*|\\?.*)?$',
+      'the text input should contain the default path matching regexp'
     )
   })
 

+ 27 - 10
test/advanced-origins.js

@@ -27,17 +27,10 @@ module.exports = ({browser, advanced, content}) => {
       )
       t.equal(
         await advanced.evaluate(() =>
-          document.querySelector('.m-list li:nth-of-type(2) span:nth-of-type(1)').innerText
+          document.querySelector('.m-list li:nth-of-type(2) .m-origin').innerText
         ),
-        'http',
-        'protocol should be http'
-      )
-      t.equal(
-        await advanced.evaluate(() =>
-          document.querySelector('.m-list li:nth-of-type(2) span:nth-of-type(2)').innerText
-        ),
-        'localhost:3000',
-        'hostname should be localhost:3000'
+        'http://localhost:3000',
+        'origin name should be http://localhost:3000'
       )
     })
   })
@@ -51,6 +44,14 @@ module.exports = ({browser, advanced, content}) => {
         await advanced.click('.m-switch')
       }
 
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // disable path matching
       await advanced.evaluate(() => {
         document.querySelector('.m-list li:nth-of-type(2) input')
@@ -87,6 +88,14 @@ module.exports = ({browser, advanced, content}) => {
         await advanced.click('.m-switch')
       }
 
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // disable path matching
       await advanced.evaluate(() => {
         document.querySelector('.m-list li:nth-of-type(2) input')
@@ -138,6 +147,14 @@ module.exports = ({browser, advanced, content}) => {
         await advanced.click('.m-switch')
       }
 
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-exapanded')))
+      {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+
       // enable path matching
       await advanced.evaluate(() => {
         document.querySelector('.m-list li:nth-of-type(2) input')