origin-encoding.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var t = require('assert')
  2. var defaults = require('./utils/defaults')
  3. module.exports = ({popup, advanced, content}) => {
  4. before(async () => {
  5. await defaults({popup, advanced, content})
  6. // enable path matching
  7. await advanced.evaluate(() => {
  8. document.querySelector('.m-list li:nth-of-type(1) input').value = 'encoding-.*'
  9. document.querySelector('.m-list li:nth-of-type(1) input').dispatchEvent(new Event('keyup'))
  10. })
  11. // there is debounce timeout of 750ms in the options UI
  12. await advanced.waitForTimeout(800)
  13. })
  14. describe('no content-type header set', () => {
  15. before(async () => {
  16. // set wrong encoding
  17. await advanced.select('.m-list li:nth-of-type(1) .m-encoding select', 'Shift_JIS')
  18. // go to page serving Big5 encoded string
  19. // with no content-type header set
  20. await content.goto('http://localhost:3000/encoding-no-content-type')
  21. await content.bringToFront()
  22. await content.waitForTimeout(300)
  23. })
  24. it('do not override if content-type header is missing', async () => {
  25. t.equal(
  26. await content.evaluate(() => document.charset),
  27. 'Big5',
  28. 'chrome detects the correct encoding automatically'
  29. )
  30. t.equal(
  31. await content.evaluate(() => document.querySelector('#_html p').innerText),
  32. '你好',
  33. 'text should be decoded correctly'
  34. )
  35. })
  36. })
  37. describe('no charset in content-type header', () => {
  38. before(async () => {
  39. // set wrong encoding
  40. await advanced.select('.m-list li:nth-of-type(1) .m-encoding select', 'Shift_JIS')
  41. // go to page serving Big5 encoded string
  42. // with no charset set in the content-type header
  43. await content.goto('http://localhost:3000/encoding-no-charset')
  44. await content.bringToFront()
  45. await content.waitForTimeout(300)
  46. })
  47. it('do not override if charset is missing in content-type header', async () => {
  48. t.equal(
  49. await content.evaluate(() => document.charset),
  50. 'Big5',
  51. 'chrome detects the correct encoding automatically'
  52. )
  53. t.equal(
  54. await content.evaluate(() => document.querySelector('#_html p').innerText),
  55. '你好',
  56. 'text should be decoded correctly'
  57. )
  58. })
  59. })
  60. describe('wrong charset in content-type header', () => {
  61. before(async () => {
  62. // detect encoding automatically
  63. await advanced.select('.m-list li:nth-of-type(1) .m-encoding select', '')
  64. // go to page serving windows-1251 encoded string
  65. // with UTF-8 charset set in content-type header
  66. await content.goto('http://localhost:3000/encoding-wrong-charset')
  67. await content.bringToFront()
  68. await content.waitForTimeout(300)
  69. })
  70. it('when encoding override is disabled', async () => {
  71. t.equal(
  72. await content.evaluate(() => document.charset),
  73. 'UTF-8',
  74. 'chrome picks the wrong encoding from the content-type charset'
  75. )
  76. t.equal(
  77. await content.evaluate(() => document.querySelector('#_html p').innerText),
  78. '�������',
  79. 'text should be decoded incorrectly'
  80. )
  81. })
  82. })
  83. // TEST: overriding content-type no longer works
  84. describe.skip('override charset set in content-type header', () => {
  85. before(async () => {
  86. await advanced.bringToFront()
  87. // override encoding
  88. await advanced.select('.m-list li:nth-of-type(1) .m-encoding select', 'Windows-1251')
  89. // go to page serving windows-1251 encoded string
  90. // with UTF-8 charset set in content-type header
  91. await content.goto('http://localhost:3000/encoding-wrong-charset')
  92. await content.bringToFront()
  93. await content.waitForTimeout(300)
  94. })
  95. it('use encoding set for the origin', async () => {
  96. t.equal(
  97. await content.evaluate(() => document.charset),
  98. 'windows-1251',
  99. 'the content-type charset should be overridden'
  100. )
  101. t.equal(
  102. await content.evaluate(() => document.querySelector('#_html p').innerText),
  103. 'здрасти',
  104. 'text should be decoded correctly'
  105. )
  106. })
  107. })
  108. describe('persist state', () => {
  109. before(async () => {
  110. await advanced.bringToFront()
  111. await advanced.reload()
  112. await advanced.waitForTimeout(300)
  113. // expand origin
  114. await advanced.click('.m-list li:nth-of-type(1)')
  115. })
  116. it('reload', async () => {
  117. t.equal(
  118. await advanced.evaluate(() =>
  119. document.querySelector('.m-list li:nth-of-type(1) .m-encoding select').value
  120. ),
  121. 'Windows-1251',
  122. 'should persist the selected encoding'
  123. )
  124. })
  125. })
  126. }