query-fragment-normalization.test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233
  1. 'use strict'
  2. const test = require('tape')
  3. const fastURI = require('..')
  4. test('query percent-encoding is normalized (RFC 3986 §6.2.2)', (t) => {
  5. // hex digits are uppercased, like the path component already does
  6. t.equal(fastURI.normalize('x://h/p?a=%2a'), 'x://h/p?a=%2A', 'uppercases hex in query')
  7. // unreserved characters are decoded
  8. t.equal(fastURI.normalize('x://h/p?a=%7e%2e'), 'x://h/p?a=~.', 'decodes unreserved in query')
  9. // reserved characters stay percent-encoded
  10. t.equal(fastURI.normalize('x://h/p?a=%2F'), 'x://h/p?a=%2F', 'keeps reserved encoded in query')
  11. // raw characters that are not allowed are percent-encoded
  12. t.equal(fastURI.normalize('x://h/p?a b'), 'x://h/p?a%20b', 'encodes a raw space in query')
  13. t.equal(fastURI.normalize('x://h/p?café'), 'x://h/p?caf%C3%A9', 'encodes raw non-ASCII in query')
  14. // `?` is allowed unencoded in a query
  15. t.equal(fastURI.normalize('x://h/p?a?b'), 'x://h/p?a?b', 'keeps `?` raw in query')
  16. t.end()
  17. })
  18. test('fragment percent-encoding is normalized without decoding reserved characters', (t) => {
  19. // reserved characters must NOT be decoded — `%2F` is not `/`
  20. t.equal(fastURI.normalize('x://h/p#a%2Fb%2A'), 'x://h/p#a%2Fb%2A', 'keeps reserved encoded in fragment')
  21. // hex is uppercased and unreserved is decoded
  22. t.equal(fastURI.normalize('x://h/p#a%2a%7e'), 'x://h/p#a%2A~', 'uppercases hex and decodes unreserved in fragment')
  23. // raw characters are still encoded
  24. t.equal(fastURI.normalize('x://h/p#a b'), 'x://h/p#a%20b', 'encodes a raw space in fragment')
  25. // `?` and `/` are allowed unencoded in a fragment
  26. t.equal(fastURI.normalize('x://h/p#f?x/y'), 'x://h/p#f?x/y', 'keeps `?` and `/` raw in fragment')
  27. t.end()
  28. })