url_spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var needle = require('../'),
  2. sinon = require('sinon'),
  3. should = require('should'),
  4. http = require('http'),
  5. helpers = require('./helpers');
  6. var port = 3456;
  7. describe('urls', function() {
  8. var server, url;
  9. function send_request(cb) {
  10. return needle.get(url, cb);
  11. }
  12. before(function(done){
  13. server = helpers.server({ port: port }, done);
  14. })
  15. after(function(done) {
  16. server.close(done);
  17. })
  18. describe('null URL', function(){
  19. it('throws', function(){
  20. (function() {
  21. send_request()
  22. }).should.throw();
  23. })
  24. })
  25. describe('invalid protocol', function(){
  26. before(function() {
  27. url = 'foo://google.com/what'
  28. })
  29. it('does not throw', function(done) {
  30. (function() {
  31. send_request(function(err) {
  32. done();
  33. })
  34. }).should.not.throw()
  35. })
  36. it('returns an error', function(done) {
  37. send_request(function(err) {
  38. err.should.be.an.Error;
  39. err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
  40. done();
  41. })
  42. })
  43. })
  44. describe('invalid host', function(){
  45. before(function() {
  46. url = 'http://s1\\\u0002.com/'
  47. })
  48. it('fails', function(done) {
  49. (function() {
  50. send_request(function(){ })
  51. }.should.throw(TypeError))
  52. done()
  53. })
  54. })
  55. /*
  56. describe('invalid path', function(){
  57. before(function() {
  58. url = 'http://www.google.com\\\/x\\\ %^&*() /x2.com/'
  59. })
  60. it('fails', function(done) {
  61. send_request(function(err) {
  62. err.should.be.an.Error;
  63. done();
  64. })
  65. })
  66. })
  67. */
  68. describe('valid protocol and path', function() {
  69. before(function() {
  70. url = 'http://localhost:' + port + '/foo';
  71. })
  72. it('works', function(done) {
  73. send_request(function(err){
  74. should.not.exist(err);
  75. done();
  76. })
  77. })
  78. })
  79. describe('no protocol but with slashes and valid path', function() {
  80. before(function() {
  81. url = '//localhost:' + port + '/foo';
  82. })
  83. it('works', function(done) {
  84. send_request(function(err){
  85. should.not.exist(err);
  86. done();
  87. })
  88. })
  89. })
  90. describe('no protocol nor slashes and valid path', function() {
  91. before(function() {
  92. url = 'localhost:' + port + '/foo';
  93. })
  94. it('works', function(done) {
  95. send_request(function(err){
  96. should.not.exist(err);
  97. done();
  98. })
  99. })
  100. })
  101. describe('double encoding', function() {
  102. var path = '/foo?email=' + encodeURIComponent('what-ever@Example.Com');
  103. before(function() {
  104. url = 'localhost:' + port + path
  105. });
  106. it('should not occur', function(done) {
  107. send_request(function(err, res) {
  108. should.not.exist(err);
  109. should(res.req.path).be.exactly(path);
  110. done();
  111. });
  112. });
  113. })
  114. })