origin-encoding.js 5.5 KB

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