UNPKG

6.48 kBJavaScriptView Raw
1var assert = require("assert");
2var _ = require("underscore");
3var seoaudit = require("../plugins/audit-plugin.js");
4var cs = require("../plugins/console-plugin.js");
5
6var testSite = require("./website/start.js").site;
7
8var crawler = require("../index.js");
9
10
11describe('Audit Plugin Basic tests', function() {
12
13
14 it('Should return an error for an invalid site', function(done) {
15 var audit = new seoaudit.Plugin();
16 var end = function(){
17 assert(audit.errors.length == 2);
18 done();
19
20 };
21 crawler.init({ externalDomains :true, externalHosts : true, retries : 0}, end);
22 crawler.registerPlugin(audit);
23
24 crawler.queue({url : "http://localhost:9999/error.html" });
25
26 });
27
28 it('Should return the redirect info for 301', function(done) {
29
30 var audit = new seoaudit.Plugin();
31
32 var end = function() {
33 var resource = audit.resources.get("http://localhost:9999/redirect");
34 assert(resource.statusCode==301);
35 var pageDest = audit.outLinks.get("http://localhost:9999/redirect")[0].page;
36 assert( pageDest == "/page2.html", "invalide destination page for the redirect : " + pageDest );
37
38 done();
39
40 };
41
42 crawler.init(null, end);
43 crawler.registerPlugin(audit);
44 crawler.queue({url : "http://localhost:9999/redirect"});
45
46 });
47
48 it('Should return the redirect chain info for a series of 301', function(done) {
49
50
51 var audit = new seoaudit.Plugin();
52
53 var end = function(){
54
55 var resource = audit.resources.get("http://localhost:9999/redirect1");
56 assert(resource.statusCode==301);
57
58 resource = audit.resources.get("http://localhost:9999/redirect2");
59 assert(resource.statusCode==302);
60 assert(audit.redirects.get("http://localhost:9999/redirect1").to == "http://localhost:9999/redirect2");
61 assert(audit.redirects.get("http://localhost:9999/redirect1").statusCode == 301);
62 assert(audit.redirects.get("http://localhost:9999/redirect2").to == "http://localhost:9999/redirect3");
63 assert(audit.redirects.get("http://localhost:9999/redirect2").statusCode == 302);
64 assert(audit.redirects.get("http://localhost:9999/redirect3").to == "http://localhost:9999/index.html");
65 assert(audit.redirects.get("http://localhost:9999/redirect3").statusCode == 301);
66 done();
67
68 };
69 crawler.init(null, end);
70 crawler.registerPlugin(audit);
71 crawler.queue({url : "http://localhost:9999/redirect1"});
72
73 });
74
75 it('Should return only the latest url after a 301 chain with the option followRedirect = true', function(done) {
76
77 var audit = new seoaudit.Plugin();
78 var end = function(){
79
80 var resource = audit.resources.get("http://localhost:9999/index.html");
81 assert(resource.statusCode==200);
82
83 assert(audit.redirects.keys = []);
84
85 done();
86
87 };
88
89 crawler.init({followRedirect : true}, end);
90 crawler.registerPlugin(audit);
91
92 crawler.queue({url : "http://localhost:9999/redirect1"});
93
94 });
95
96 it('Should crawl images', function(done) {
97
98 var audit = new seoaudit.Plugin();
99 //var cons = new cs.Plugin();
100 var end = function(){
101 var resource = audit.resources.get("http://localhost:9999/200x200-image.jpg");
102 assert(resource.contentType =='image/jpeg');
103
104 done();
105
106 };
107
108 crawler.init(null, end);
109 crawler.registerPlugin(audit);
110 //crawler.registerPlugin(cons);
111
112 crawler.queue({url : "http://localhost:9999/page1.html"});
113
114 });
115
116 it('Should crawl 404 urls', function(done) {
117
118 var audit = new seoaudit.Plugin();
119 //var cons = new cs.Plugin();
120 var end = function(){
121
122 var resource = audit.resources.get("http://localhost:9999/404-test.html");
123 assert(resource.statusCode==404);
124 assert(audit.outLinks.get("http://localhost:9999/page3.html")[0].page == "http://localhost:9999/404-test.html");
125 assert(audit.inLinks.get("http://localhost:9999/404-test.html")[0].page == "http://localhost:9999/page3.html");
126
127 done();
128
129 };
130
131 crawler.init(null, end);
132 crawler.registerPlugin(audit);
133 //crawler.registerPlugin(cons);
134
135 crawler.queue({url : "http://localhost:9999/page3.html"});
136
137 });
138
139
140 it('Should crawl 500 urls', function(done) {
141
142 var audit = new seoaudit.Plugin();
143 var end = function(){
144
145 var resource = audit.resources.get("http://localhost:9999/internal-error");
146
147 assert(resource.statusCode==500);
148 assert(audit.outLinks.get("http://localhost:9999/page3.html")[1].page == "http://localhost:9999/internal-error");
149 assert(audit.inLinks.get("http://localhost:9999/internal-error")[0].page == "http://localhost:9999/page3.html");
150
151 done();
152
153 };
154
155 crawler.init(null, end);
156 crawler.registerPlugin(audit);
157 crawler.queue({url : "http://localhost:9999/page3.html"});
158
159 });
160
161
162 it('Should crawl even with dns error', function(done) {
163
164 var end = function(){
165
166 var resource = audit.resources.get("http://www.thisnotcorrect.abc/");
167
168 assert(resource.statusCode == "DNS lookup failed");
169 assert(audit.outLinks.get("http://localhost:9999/page5.html")[0].page == "http://www.thisnotcorrect.abc/");
170 assert(audit.outLinks.get("http://localhost:9999/page5.html")[1].page == "http://localhost:9999/");
171 assert(audit.inLinks.get("http://www.thisnotcorrect.abc/")[0].page == "http://localhost:9999/page5.html");
172
173 done();
174
175 };
176
177 var audit = new seoaudit.Plugin();
178 crawler.init({externalDomains : true, retries : 0}, end);
179 crawler.registerPlugin(audit);
180 crawler.queue({url : "http://localhost:9999/page5.html"});
181
182 });
183
184});