origin-encoding.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(() => document.querySelector('.m-list li:nth-of-type(2)').classList.contains('m-expanded'))) {
  9. await advanced.click('.m-list li:nth-of-type(2)')
  10. }
  11. await advanced.click('.m-list li:nth-of-type(2) .m-footer .m-button')
  12. }
  13. // add origin
  14. await advanced.select('.m-select', 'http')
  15. await advanced.type('[type=text]', 'localhost:3000')
  16. await advanced.click('button')
  17. await advanced.waitFor(200)
  18. // expand origin
  19. if (!await advanced.evaluate(() => document.querySelector('.m-list li:nth-of-type(2)').classList.contains('m-expanded'))) {
  20. await advanced.click('.m-list li:nth-of-type(2)')
  21. }
  22. // enable header detection
  23. if (!await advanced.evaluate(() => state.header)) {
  24. await advanced.click('.m-switch')
  25. }
  26. // enable path matching
  27. await advanced.evaluate(() => {
  28. document.querySelector('.m-list li:nth-of-type(2) input').value = 'windows-1251'
  29. document.querySelector('.m-list li:nth-of-type(2) input').dispatchEvent(new Event('keyup'))
  30. })
  31. // there is debounce timeout of 750ms in the options UI
  32. await advanced.waitFor(800)
  33. })
  34. describe('incorrect encoding', () => {
  35. before(async () => {
  36. // go to page serving windows-1251 encoded string
  37. // with UTF-8 content-type charset
  38. await content.goto('http://localhost:3000/windows-1251')
  39. await content.bringToFront()
  40. await content.waitFor(200)
  41. })
  42. it('use encoding set by the server', async () => {
  43. t.equal(
  44. await content.evaluate(() => document.charset),
  45. 'UTF-8',
  46. 'chrome should pick the encoding from the content-type charset'
  47. )
  48. t.equal(
  49. await content.evaluate(() => document.querySelector('#_html p').innerText),
  50. '�������',
  51. 'text should be decoded incorrectly'
  52. )
  53. })
  54. })
  55. describe('correct encoding', () => {
  56. before(async () => {
  57. await advanced.bringToFront()
  58. // set encoding
  59. await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Windows-1251')
  60. // go to page serving windows-1251 encoded string
  61. // with windows-1251 content-type charset
  62. await content.goto('http://localhost:3000/windows-1251')
  63. await content.bringToFront()
  64. await content.waitFor(200)
  65. })
  66. it('use encoding set for the origin', async () => {
  67. t.equal(
  68. await content.evaluate(() => document.charset),
  69. 'windows-1251',
  70. 'the content-type charset should be overridden'
  71. )
  72. t.equal(
  73. await content.evaluate(() => document.querySelector('#_html p').innerText),
  74. 'здрасти',
  75. 'text should be decoded correctly'
  76. )
  77. })
  78. })
  79. describe('persist state', () => {
  80. before(async () => {
  81. await advanced.bringToFront()
  82. await advanced.reload()
  83. await advanced.waitFor(200)
  84. // expand origin
  85. await advanced.click('.m-list li:nth-of-type(2)')
  86. })
  87. it('reload', async () => {
  88. t.equal(
  89. await advanced.evaluate(() =>
  90. document.querySelector('.m-list li:nth-of-type(2) .m-encoding select').value
  91. ),
  92. 'Windows-1251',
  93. 'should persist the selected encoding'
  94. )
  95. })
  96. })
  97. }