소스 검색

Improve encoding tests

simov 7 년 전
부모
커밋
7baa40c492
2개의 변경된 파일71개의 추가작업 그리고 11개의 파일을 삭제
  1. 8 1
      test/index.js
  2. 63 10
      test/origin-encoding.js

+ 8 - 1
test/index.js

@@ -121,7 +121,14 @@ describe('markdown-viewer', () => {
           res.end('# h1')
         }
         // encoding
-        else if (/windows-1251/.test(req.url)) {
+        else if (/encoding-no-content-type/.test(req.url)) {
+          res.end(iconv.encode('你好', 'big5'))
+        }
+        else if (/encoding-no-charset/.test(req.url)) {
+          res.setHeader('Content-Type', 'text/markdown')
+          res.end(iconv.encode('你好', 'big5'))
+        }
+        else if (/encoding-wrong-charset/.test(req.url)) {
           res.setHeader('Content-Type', 'text/markdown; charset=UTF-8')
           res.end(iconv.encode('здрасти', 'win1251'))
         }

+ 63 - 10
test/origin-encoding.js

@@ -40,26 +40,79 @@ module.exports = ({popup, advanced, content}) => {
 
     // 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').value = 'encoding-.*'
       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', () => {
+  describe('no content-type header set', () => {
     before(async () => {
+      // set wrong encoding
+      await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Shift_JIS')
+
+      // go to page serving Big5 encoded string
+      // with no content-type header set
+      await content.goto('http://localhost:3000/encoding-no-content-type')
+      await content.bringToFront()
+      await content.waitFor(200)
+    })
+    it('do not override if content-type header is missing', async () => {
+      t.equal(
+        await content.evaluate(() => document.charset),
+        'Big5',
+        'chrome detects the correct encoding automatically'
+      )
+      t.equal(
+        await content.evaluate(() => document.querySelector('#_html p').innerText),
+        '你好',
+        'text should be decoded correctly'
+      )
+    })
+  })
+
+  describe('no charset in content-type header', () => {
+    before(async () => {
+      // set wrong encoding
+      await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Shift_JIS')
+
+      // go to page serving Big5 encoded string
+      // with no charset set in the content-type header
+      await content.goto('http://localhost:3000/encoding-no-charset')
+      await content.bringToFront()
+      await content.waitFor(200)
+    })
+    it('do not override if charset is missing in content-type header', async () => {
+      t.equal(
+        await content.evaluate(() => document.charset),
+        'Big5',
+        'chrome detects the correct encoding automatically'
+      )
+      t.equal(
+        await content.evaluate(() => document.querySelector('#_html p').innerText),
+        '你好',
+        'text should be decoded correctly'
+      )
+    })
+  })
+
+  describe('wrong charset in content-type header', () => {
+    before(async () => {
+      // detect encoding automatically
+      await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', '')
+
       // go to page serving windows-1251 encoded string
-      // with UTF-8 content-type charset
-      await content.goto('http://localhost:3000/windows-1251')
+      // with UTF-8 charset set in content-type header
+      await content.goto('http://localhost:3000/encoding-wrong-charset')
       await content.bringToFront()
       await content.waitFor(200)
     })
-    it('use encoding set by the server', async () => {
+    it('when encoding override is disabled', async () => {
       t.equal(
         await content.evaluate(() => document.charset),
         'UTF-8',
-        'chrome should pick the encoding from the content-type charset'
+        'chrome picks the wrong encoding from the content-type charset'
       )
       t.equal(
         await content.evaluate(() => document.querySelector('#_html p').innerText),
@@ -69,16 +122,16 @@ module.exports = ({popup, advanced, content}) => {
     })
   })
 
-  describe('correct encoding', () => {
+  describe('override charset set in content-type header', () => {
     before(async () => {
       await advanced.bringToFront()
 
-      // set encoding
+      // override 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')
+      // with UTF-8 charset set in content-type header
+      await content.goto('http://localhost:3000/encoding-wrong-charset')
       await content.bringToFront()
       await content.waitFor(200)
     })