1
0

advanced-encoding.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var t = require('assert')
  2. module.exports = ({advanced, content}) => {
  3. before(async () => {
  4. await advanced.bringToFront()
  5. // remove origin
  6. if (await advanced.evaluate(() => Object.keys(state.origins).length > 1)) {
  7. // expand origin
  8. if (!await advanced.evaluate(() =>
  9. document.querySelector('.m-list li:nth-of-type(2)')
  10. .classList.contains('m-expanded'))) {
  11. await advanced.click('.m-list li:nth-of-type(2)')
  12. }
  13. await advanced.click('.m-list li:nth-of-type(2) .m-footer .m-button')
  14. }
  15. // add origin
  16. await advanced.select('.m-select', 'http')
  17. await advanced.type('[type=text]', 'localhost:3000')
  18. await advanced.click('button')
  19. await advanced.waitFor(200)
  20. // expand origin
  21. if (!await advanced.evaluate(() =>
  22. document.querySelector('.m-list li:nth-of-type(2)')
  23. .classList.contains('m-expanded'))) {
  24. await advanced.click('.m-list li:nth-of-type(2)')
  25. }
  26. // enable path matching
  27. await advanced.evaluate(() => {
  28. document.querySelector('.m-list li:nth-of-type(2) input')
  29. .value = 'windows-1251'
  30. document.querySelector('.m-list li:nth-of-type(2) input')
  31. .dispatchEvent(new Event('keyup'))
  32. })
  33. // there is debounce timeout of 750ms in the options UI
  34. await advanced.waitFor(800)
  35. })
  36. describe('incorrect encoding', () => {
  37. before(async () => {
  38. // go to page serving windows-1251 encoded string
  39. // with UTF-8 content-type charset
  40. await content.goto('http://localhost:3000/windows-1251')
  41. await content.bringToFront()
  42. await content.waitFor(200)
  43. })
  44. it('use encoding set by the server', async () => {
  45. t.equal(
  46. await content.evaluate(() => document.charset),
  47. 'UTF-8',
  48. 'chrome should pick the encoding from the content-type charset'
  49. )
  50. t.equal(
  51. await content.evaluate(() => document.querySelector('#_html p').innerText),
  52. '�������',
  53. 'text should be decoded incorrectly'
  54. )
  55. })
  56. })
  57. describe('correct encoding', () => {
  58. before(async () => {
  59. await advanced.bringToFront()
  60. // enable csp - required to enable the webRequest permission!
  61. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  62. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  63. }
  64. // set encoding
  65. await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Windows-1251')
  66. // go to page serving windows-1251 encoded string
  67. // with windows-1251 content-type charset
  68. await content.goto('http://localhost:3000/windows-1251')
  69. await content.bringToFront()
  70. await content.waitFor(200)
  71. })
  72. it('use encoding set for the origin', async () => {
  73. t.equal(
  74. await content.evaluate(() => document.charset),
  75. 'windows-1251',
  76. 'the content-type charset should be overridden'
  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('persist state', () => {
  86. before(async () => {
  87. await advanced.bringToFront()
  88. await advanced.reload()
  89. await advanced.waitFor(200)
  90. // expand origin
  91. if (!await advanced.evaluate(() =>
  92. document.querySelector('.m-list li:nth-of-type(2)')
  93. .classList.contains('m-expanded'))) {
  94. await advanced.click('.m-list li:nth-of-type(2)')
  95. }
  96. })
  97. it('reload', async () => {
  98. t.equal(
  99. await advanced.evaluate(() =>
  100. document.querySelector('.m-list li:nth-of-type(2) .m-encoding select').value
  101. ),
  102. 'Windows-1251',
  103. 'should persist the selected encoding'
  104. )
  105. })
  106. })
  107. }