origin-encoding.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = 'windows-1251'
  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('incorrect encoding', () => {
  40. before(async () => {
  41. // go to page serving windows-1251 encoded string
  42. // with UTF-8 content-type charset
  43. await content.goto('http://localhost:3000/windows-1251')
  44. await content.bringToFront()
  45. await content.waitFor(200)
  46. })
  47. it('use encoding set by the server', async () => {
  48. t.equal(
  49. await content.evaluate(() => document.charset),
  50. 'UTF-8',
  51. 'chrome should pick the encoding from the content-type charset'
  52. )
  53. t.equal(
  54. await content.evaluate(() => document.querySelector('#_html p').innerText),
  55. '�������',
  56. 'text should be decoded incorrectly'
  57. )
  58. })
  59. })
  60. describe('correct encoding', () => {
  61. before(async () => {
  62. await advanced.bringToFront()
  63. // set encoding
  64. await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Windows-1251')
  65. // go to page serving windows-1251 encoded string
  66. // with windows-1251 content-type charset
  67. await content.goto('http://localhost:3000/windows-1251')
  68. await content.bringToFront()
  69. await content.waitFor(200)
  70. })
  71. it('use encoding set for the origin', async () => {
  72. t.equal(
  73. await content.evaluate(() => document.charset),
  74. 'windows-1251',
  75. 'the content-type charset should be overridden'
  76. )
  77. t.equal(
  78. await content.evaluate(() => document.querySelector('#_html p').innerText),
  79. 'здрасти',
  80. 'text should be decoded correctly'
  81. )
  82. })
  83. })
  84. describe('persist state', () => {
  85. before(async () => {
  86. await advanced.bringToFront()
  87. await advanced.reload()
  88. await advanced.waitFor(200)
  89. // expand origin
  90. await advanced.click('.m-list li:nth-of-type(2)')
  91. })
  92. it('reload', async () => {
  93. t.equal(
  94. await advanced.evaluate(() =>
  95. document.querySelector('.m-list li:nth-of-type(2) .m-encoding select').value
  96. ),
  97. 'Windows-1251',
  98. 'should persist the selected encoding'
  99. )
  100. })
  101. })
  102. }