Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

http-browserify.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // These tests are taken 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('Make sure http object has correct properties', function (t) {
  17. t.ok(http.Agent, 'Agent defined')
  18. t.ok(http.ClientRequest, 'ClientRequest defined')
  19. t.ok(http.ClientRequest.prototype, 'ClientRequest.prototype defined')
  20. t.ok(http.IncomingMessage, 'IncomingMessage defined')
  21. t.ok(http.IncomingMessage.prototype, 'IncomingMessage.prototype defined')
  22. t.ok(http.METHODS, 'METHODS defined')
  23. t.ok(http.STATUS_CODES, 'STATUS_CODES defined')
  24. t.ok(http.get, 'get defined')
  25. t.ok(http.globalAgent, 'globalAgent defined')
  26. t.ok(http.request, 'request defined')
  27. t.end()
  28. })
  29. test('Test simple url string', function(t) {
  30. var testUrl = { path: '/api/foo' }
  31. var request = http.get(testUrl, noop)
  32. var resolved = url.resolve(location, request._opts.url)
  33. t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
  34. t.end()
  35. })
  36. test('Test full url object', function(t) {
  37. var testUrl = {
  38. host: "localhost:8081",
  39. hostname: "localhost",
  40. href: "http://localhost:8081/api/foo?bar=baz",
  41. method: "GET",
  42. path: "/api/foo?bar=baz",
  43. pathname: "/api/foo",
  44. port: "8081",
  45. protocol: "http:",
  46. query: "bar=baz",
  47. search: "?bar=baz",
  48. slashes: true
  49. }
  50. var request = http.get(testUrl, noop)
  51. var resolved = url.resolve(location, request._opts.url)
  52. t.equal(resolved, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct')
  53. t.end()
  54. })
  55. test('Test alt protocol', function(t) {
  56. var params = {
  57. protocol: "foo:",
  58. hostname: "localhost",
  59. port: "3000",
  60. path: "/bar"
  61. }
  62. var request = http.get(params, noop)
  63. var resolved = url.resolve(location, request._opts.url)
  64. t.equal(resolved, 'foo://localhost:3000/bar', 'Url should be correct')
  65. t.end()
  66. })
  67. test('Test page with \'file:\' protocol', function (t) {
  68. var params = {
  69. hostname: 'localhost',
  70. port: 3000,
  71. path: '/bar'
  72. }
  73. var fileLocation = 'file:///home/me/stuff/index.html'
  74. var normalLocation = global.location
  75. global.location = url.parse(fileLocation) // Temporarily change the location
  76. var request = http.get(params, noop)
  77. global.location = normalLocation // Reset the location
  78. var resolved = url.resolve(fileLocation, request._opts.url)
  79. t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
  80. t.end()
  81. })
  82. test('Test string as parameters', function(t) {
  83. var testUrl = '/api/foo'
  84. var request = http.get(testUrl, noop)
  85. var resolved = url.resolve(location, request._opts.url)
  86. t.equal(resolved, 'http://localhost:8081/api/foo', 'Url should be correct')
  87. t.end()
  88. })
  89. test('Test withCredentials param', function(t) {
  90. var url = '/api/foo'
  91. var request = http.get({ url: url, withCredentials: false }, noop)
  92. t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
  93. var request = http.get({ url: url, withCredentials: true }, noop)
  94. t.equal(request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
  95. var request = http.get({ url: url }, noop)
  96. t.equal(request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
  97. t.end()
  98. })
  99. test('Test ipv6 address', function(t) {
  100. var testUrl = 'http://[::1]:80/foo'
  101. var request = http.get(testUrl, noop)
  102. var resolved = url.resolve(location, request._opts.url)
  103. t.equal(resolved, 'http://[::1]:80/foo', 'Url should be correct')
  104. t.end()
  105. })
  106. test('Test relative path in url', function(t) {
  107. var params = { path: './bar' }
  108. var request = http.get(params, noop)
  109. var resolved = url.resolve(location, request._opts.url)
  110. t.equal(resolved, 'http://localhost:8081/foo/bar', 'Url should be correct')
  111. t.end()
  112. })
  113. test('Cleanup', function (t) {
  114. delete global.location
  115. delete global.XMLHttpRequest
  116. delete require.cache[moduleName]
  117. t.end()
  118. })