forked from bnoguchi/mongoose-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.test.js
55 lines (51 loc) · 1.68 KB
/
url.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require('should');
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests');
require("../").loadTypes(mongoose, 'url');
var WebpageSchema = new Schema({
url: mongoose.SchemaTypes.Url
});
mongoose.model('Webpage', WebpageSchema);
var Webpage;
module.exports = {
before: function(){
Webpage = db.model('Webpage', WebpageSchema);
Webpage.remove({}, function (err) {});
},
'test invalid url validation': function () {
var webpage = new Webpage({url: 'file:///home/'});
webpage.save(function (err) {
err.message.should.equal('Validator "url is invalid" failed for path url');
webpage.isNew.should.be.true;
});
},
'test valid url validation': function () {
var webpage = new Webpage({ url: 'http://www.google.com/' });
webpage.save(function (err) {
err.should.eql(null);
webpage.isNew.should.be.false;
});
},
'url normalization should remove www.': function () {
var webpage = new Webpage({ url: 'http://www.google.com/'});
webpage.save(function (err) {
webpage.url.should.equal('http://google.com/');
Webpage.findById(webpage._id, function (err, refreshed) {
refreshed.url.should.equal('http://google.com/');
});
});
},
'url normalization should add a trailing slash': function () {
var webpage = new Webpage({ url: 'http://google.com'});
webpage.save(function (err) {
webpage.url.should.equal('http://google.com/');
Webpage.findById(webpage._id, function (err, refreshed) {
refreshed.url.should.equal('http://google.com/');
});
});
},
teardown: function(){
db.close();
}
};