source: trip-planner-front/node_modules/needle/test/url_spec.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1var needle = require('../'),
2 sinon = require('sinon'),
3 should = require('should'),
4 http = require('http'),
5 helpers = require('./helpers');
6
7var port = 3456;
8
9describe('urls', function() {
10
11 var server, url;
12
13 function send_request(cb) {
14 return needle.get(url, cb);
15 }
16
17 before(function(done){
18 server = helpers.server({ port: port }, done);
19 })
20
21 after(function(done) {
22 server.close(done);
23 })
24
25 describe('null URL', function(){
26
27 it('throws', function(){
28 (function() {
29 send_request()
30 }).should.throw();
31 })
32
33 })
34
35 describe('invalid protocol', function(){
36
37 before(function() {
38 url = 'foo://google.com/what'
39 })
40
41 it('does not throw', function(done) {
42 (function() {
43 send_request(function(err) {
44 done();
45 })
46 }).should.not.throw()
47 })
48
49 it('returns an error', function(done) {
50 send_request(function(err) {
51 err.should.be.an.Error;
52 err.code.should.match(/ENOTFOUND|EADDRINFO|EAI_AGAIN/)
53 done();
54 })
55 })
56
57 })
58
59 describe('invalid host', function(){
60
61 before(function() {
62 url = 'http://s1\\\u0002.com/'
63 })
64
65 it('fails', function(done) {
66 (function() {
67 send_request(function(){ })
68 }.should.throw(TypeError))
69 done()
70 })
71
72 })
73
74/*
75 describe('invalid path', function(){
76
77 before(function() {
78 url = 'http://www.google.com\\\/x\\\ %^&*() /x2.com/'
79 })
80
81 it('fails', function(done) {
82 send_request(function(err) {
83 err.should.be.an.Error;
84 done();
85 })
86 })
87
88 })
89*/
90
91 describe('valid protocol and path', function() {
92
93 before(function() {
94 url = 'http://localhost:' + port + '/foo';
95 })
96
97 it('works', function(done) {
98 send_request(function(err){
99 should.not.exist(err);
100 done();
101 })
102 })
103
104 })
105
106 describe('no protocol but with slashes and valid path', function() {
107
108 before(function() {
109 url = '//localhost:' + port + '/foo';
110 })
111
112 it('works', function(done) {
113 send_request(function(err){
114 should.not.exist(err);
115 done();
116 })
117 })
118
119 })
120
121 describe('no protocol nor slashes and valid path', function() {
122
123 before(function() {
124 url = 'localhost:' + port + '/foo';
125 })
126
127 it('works', function(done) {
128 send_request(function(err){
129 should.not.exist(err);
130 done();
131 })
132 })
133
134 })
135
136 describe('double encoding', function() {
137
138 var path = '/foo?email=' + encodeURIComponent('what-ever@Example.Com');
139
140 before(function() {
141 url = 'localhost:' + port + path
142 });
143
144 it('should not occur', function(done) {
145 send_request(function(err, res) {
146 should.not.exist(err);
147 should(res.req.path).be.exactly(path);
148 done();
149 });
150
151 });
152
153 })
154
155})
Note: See TracBrowser for help on using the repository browser.