advanced-encoding.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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:nth-of-type(2)')
  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. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  20. // await advanced.waitFor(() => document.querySelectorAll('.m-list li').length === 2)
  21. await advanced.waitFor(200)
  22. // expand origin
  23. if (!await advanced.evaluate(() =>
  24. document.querySelector('.m-list li:nth-of-type(2)')
  25. .classList.contains('m-expanded'))) {
  26. await advanced.click('.m-list li:nth-of-type(2)')
  27. }
  28. // enable path matching
  29. await advanced.evaluate(() => {
  30. document.querySelector('.m-list li:nth-of-type(2) input')
  31. .value = 'windows-1251'
  32. document.querySelector('.m-list li:nth-of-type(2) input')
  33. .dispatchEvent(new Event('keyup'))
  34. })
  35. // there is debounce timeout of 750ms in the options UI
  36. await advanced.waitFor(800)
  37. })
  38. describe('incorrect encoding', () => {
  39. before(async () => {
  40. // go to page serving windows-1251 encoded string
  41. // with UTF-8 content-type charset
  42. await content.goto('http://localhost:3000/windows-1251')
  43. await content.bringToFront()
  44. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  45. // await content.waitFor('pre')
  46. await content.waitFor(200)
  47. })
  48. it('use encoding set by the server', async () => {
  49. t.equal(
  50. await content.evaluate(() => document.charset),
  51. 'UTF-8',
  52. 'chrome should pick the encoding from the content-type charset'
  53. )
  54. t.equal(
  55. await content.evaluate(() => document.querySelector('#_html p').innerText),
  56. '�������',
  57. 'text should be decoded incorrectly'
  58. )
  59. })
  60. })
  61. describe('correct encoding', () => {
  62. before(async () => {
  63. await advanced.bringToFront()
  64. // enable csp - required to enable the webRequest permission!
  65. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  66. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  67. }
  68. // set encoding
  69. await advanced.select('.m-list li:nth-of-type(2) .m-encoding select', 'Windows-1251')
  70. // go to page serving windows-1251 encoded string
  71. // with windows-1251 content-type charset
  72. await content.goto('http://localhost:3000/windows-1251')
  73. await content.bringToFront()
  74. // TODO: wait for https://github.com/GoogleChrome/puppeteer/pull/2289
  75. // await content.waitFor('pre')
  76. await content.waitFor(200)
  77. })
  78. it('use encoding set for the origin', async () => {
  79. t.equal(
  80. await content.evaluate(() => document.charset),
  81. 'windows-1251',
  82. 'the content-type charset should be overridden'
  83. )
  84. t.equal(
  85. await content.evaluate(() => document.querySelector('#_html p').innerText),
  86. 'здрасти',
  87. 'text should be decoded correctly'
  88. )
  89. })
  90. })
  91. describe('persist state', () => {
  92. before(async () => {
  93. await advanced.bringToFront()
  94. await advanced.reload()
  95. await advanced.waitFor(200)
  96. // expand origin
  97. if (!await advanced.evaluate(() =>
  98. document.querySelector('.m-list li:nth-of-type(2)')
  99. .classList.contains('m-expanded'))) {
  100. await advanced.click('.m-list li:nth-of-type(2)')
  101. }
  102. })
  103. it('reload', async () => {
  104. t.equal(
  105. await advanced.evaluate(() =>
  106. document.querySelector('.m-list li:nth-of-type(2) .m-encoding select').value
  107. ),
  108. 'Windows-1251',
  109. 'should persist the selected encoding'
  110. )
  111. })
  112. })
  113. }