Procházet zdrojové kódy

Add encoding tests

simov před 7 roky
rodič
revize
247e17c64b
3 změnil soubory, kde provedl 128 přidání a 0 odebrání
  1. 1 0
      package.json
  2. 121 0
      test/advanced-encoding.js
  3. 6 0
      test/index.js

+ 1 - 0
package.json

@@ -34,6 +34,7 @@
     "babel-core": "^6.26.0",
     "babel-preset-es2015": "^6.24.1",
     "csso": "^3.4.0",
+    "iconv-lite": "^0.4.21",
     "mocha": "^4.1.0",
     "node-sass": "^4.7.2",
     "puppeteer": "^1.2.0",

+ 121 - 0
test/advanced-encoding.js

@@ -0,0 +1,121 @@
+
+var t = require('assert')
+
+
+module.exports = ({advanced, content}) => {
+
+  before(async () => {
+    await advanced.bringToFront()
+
+    // remove origin
+    if (await advanced.evaluate(() => Object.keys(state.origins).length > 1)) {
+      await advanced.click('.m-list li:nth-of-type(2) .m-footer .m-button:nth-of-type(2)')
+    }
+
+    // add origin
+    await advanced.select('.m-select', 'http')
+    await advanced.type('[type=text]', 'localhost:3000')
+    await advanced.click('button')
+    // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
+    // 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-expanded'))) {
+      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')
+        .value = 'windows-1251'
+      document.querySelector('.m-list li:nth-of-type(2) input')
+        .dispatchEvent(new Event('keyup'))
+    })
+    // there is debounce timeout of 750ms in the options UI
+    await advanced.waitFor(800)
+  })
+
+  describe('incorrect encoding', () => {
+    before(async () => {
+      // go to page serving windows-1251 encoded string
+      // with UTF-8 content-type charset
+      await content.goto('http://localhost:3000/windows-1251')
+      await content.bringToFront()
+      // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
+      // await content.waitFor('pre')
+      await content.waitFor(200)
+    })
+    it('use encoding set by the server', async () => {
+      t.equal(
+        await content.evaluate(() => document.charset),
+        'UTF-8',
+        'chrome should pick the encoding from the content-type charset'
+      )
+      t.equal(
+        await content.evaluate(() => document.querySelector('#_html p').innerText),
+        '�������',
+        'text should be decoded incorrectly'
+      )
+    })
+  })
+
+
+  describe('correct encoding', () => {
+    before(async () => {
+      await advanced.bringToFront()
+      // enable csp - required to enable the webRequest permission!
+      if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
+        await advanced.click('.m-list li:nth-of-type(2) .m-switch')
+      }
+      // set encoding
+      await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Windows-1251')
+
+      // go to page serving windows-1251 encoded string
+      // with windows-1251 content-type charset
+      await content.goto('http://localhost:3000/windows-1251')
+      await content.bringToFront()
+      // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
+      // await content.waitFor('pre')
+      await content.waitFor(200)
+    })
+    it('use encoding set for the origin', async () => {
+      t.equal(
+        await content.evaluate(() => document.charset),
+        'windows-1251',
+        'the content-type charset should be overridden'
+      )
+      t.equal(
+        await content.evaluate(() => document.querySelector('#_html p').innerText),
+        'здрасти',
+        'text should be decoded correctly'
+      )
+    })
+  })
+
+  describe('persist state', () => {
+    before(async () => {
+      await advanced.bringToFront()
+      await advanced.reload()
+      await advanced.waitFor(200)
+      // expand origin
+      if (!await advanced.evaluate(() =>
+        document.querySelector('.m-list li:nth-of-type(2)')
+          .classList.contains('m-expanded'))) {
+        await advanced.click('.m-list li:nth-of-type(2)')
+      }
+    })
+    it('reload', async () => {
+      t.equal(
+        await advanced.evaluate(() =>
+          document.querySelector('.m-list li:nth-of-type(2) .m-encoding select').value
+        ),
+        'Windows-1251',
+        'should persist the selected encoding'
+      )
+    })
+  })
+
+}

+ 6 - 0
test/index.js

@@ -2,6 +2,7 @@
 var path = require('path')
 var http = require('http')
 var puppeteer = require('puppeteer')
+var iconv = require('iconv-lite')
 
 var options = {
   headless: false,
@@ -17,6 +18,7 @@ var tests = [
   'advanced-defaults',
   'advanced-origins',
   'popup-options',
+  'advanced-encoding',
   'advanced-csp', // should be last - destroys popup and advanced
 ]
 
@@ -100,6 +102,10 @@ describe('markdown-viewer', () => {
             `default-src 'none'; style-src 'unsafe-inline'; sandbox`)
           res.end('# h1')
         }
+        else if (/windows-1251/.test(req.url)) {
+          res.setHeader('Content-Type', 'text/markdown; charset=UTF-8')
+          res.end(iconv.encode('здрасти', 'win1251'))
+        }
       })
       server.listen(3000, resolve)
     })