http-browserify.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // These tests are teken from http-browserify to ensure compatibility with
  2. // that module
  3. var test = require('tape')
  4. var url = require('url')
  5. var location = 'http://localhost:8081/foo/123'
  6. var noop = function() {}
  7. global.location = url.parse(location)
  8. global.XMLHttpRequest = function() {
  9. this.open = noop
  10. this.send = noop
  11. this.withCredentials = false
  12. }
  13. var moduleName = require.resolve('../../')
  14. delete require.cache[moduleName]
  15. var http = require('../../')
  16. test('Test simple url string', function(t) {
  17. var testUrl = { path: '/api/foo' }
  18. var request = http.get(testUrl, noop)
  19. var resolved = url.resolve(location, request._opts.url)
  20. t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
  21. t.end()
  22. })
  23. test('Test full url object', function(t) {
  24. var testUrl = {
  25. host: "localhost:8081",
  26. hostname: "localhost",
  27. href: "http://localhost:8081/api/foo?bar=baz",
  28. method: "GET",
  29. path: "/api/foo?bar=baz",
  30. pathname: "/api/foo",
  31. port: "8081",
  32. protocol: "http:",
  33. query: "bar=baz",
  34. search: "?bar=baz",
  35. slashes: true
  36. }
  37. var request = http.get(testUrl, noop)
  38. var resolved = url.resolve(location, request._opts.url)
  39. t.equal(resolved, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct')
  40. t.end()
  41. })
  42. test('Test alt protocol', function(t) {
  43. var params = {
  44. protocol: "foo:",
  45. hostname: "localhost",
  46. port: "3000",
  47. path: "/bar"
  48. }
  49. var request = http.get(params, noop)
  50. var resolved = url.resolve(location, request._opts.url)
  51. t.equal(resolved, 'foo://localhost:3000/bar', 'Url should be correct')
  52. t.end()
  53. })
  54. test('Test page with \'file:\' protocol', function (t) {
  55. var params = {
  56. hostname: 'localhost',
  57. port: 3000,
  58. path: '/bar'
  59. }
  60. var fileLocation = 'file:///home/me/stuff/index.html'
  61. var normalLocation = global.location
  62. global.location = url.parse(fileLocation) // Temporarily change the location
  63. var request = http.get(params, noop)
  64. global.location = normalLocation // Reset the location
  65. var resolved = url.resolve(fileLocation, request._opts.url)
  66. t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
  67. t.end()
  68. })
  69. test('Test string as parameters', function(t) {
  70. var testUrl = '/api/foo'
  71. var request = http.get(testUrl, noop)
  72. var resolved = url.resolve(location, request._opts.url)
  73. t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
  74. t.end()
  75. })
  76. test('Test withCredentials param', function(t) {
  77. var url = '/api/foo'
  78. var request = http.get({ url: url, withCredentials: false }, noop)
  79. t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
  80. var request = http.get({ url: url, withCredentials: true }, noop)
  81. t.equal(request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
  82. var request = http.get({ url: url }, noop)
  83. t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
  84. t.end()
  85. })
  86. test('Test ipv6 address', function(t) {
  87. var testUrl = 'http://[::1]:80/foo'
  88. var request = http.get(testUrl, noop)
  89. var resolved = url.resolve(location, request._opts.url)
  90. t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
  91. t.end()
  92. })
  93. test('Test relative path in url', function(t) {
  94. var params = { path: './bar' }
  95. var request = http.get(params, noop)
  96. var resolved = url.resolve(location, request._opts.url)
  97. t.equal(resolved, 'http://localhost:8081/foo/bar', 'Url should be correct')
  98. t.end()
  99. })
  100. test('Cleanup', function (t) {
  101. delete global.location
  102. delete global.XMLHttpRequest
  103. delete require.cache[moduleName]
  104. t.end()
  105. })