Server-class is functional
[gopher-lib.git] / test.js
1 const assert = require('assert');
2 const Gopher = require('./lib');
3 console.log('Testing Gopher.Resource...');
4
5 var proto = [ '','gopher://' ];
6 var host = [ 'dusted.dk', 'floodgap.com' ];
7 var port = [ '',':70', ':1337' ];
8 var type = [ '','0','1','2','3','4','5','6','7','8','9','g','I','h' ];
9 var selector = [ '', '/', '/dir', '/dir/subdir', '/dir/', '/dir/subdir/', 'dir', 'dir/subdir', 'dir/subdir/','selectors with space', 'URI%20Enoded%20Selectors%20With%20Space' ];
10 var query = [ '','?query', '?query with space', '?uri%20paced%20query' ];
11 var name = [ '', '#A_name', "#A name with spaces",'#An%20URI%20Encoded%20Name' ];
12
13 var ok=0, fail=0;
14
15 proto.forEach( (p)=>{
16         host.forEach( (h)=>{
17                 port.forEach( (P)=>{
18                         type.forEach( (t)=>{
19                                 selector.forEach( (s)=>{
20                                         query.forEach( (q)=>{
21                                                 name.forEach( (n)=>{
22                                                         var URI= p+h+P;
23                                                         if(t) { URI+='/'+t; }
24                                                         if(s) {
25                                                                 if(!t) { URI+='/'; }
26                                                                 URI+=s;
27                                                         }
28                                                         if(q) {
29                                                                 if(!s && !t) { URI+='/'; }
30                                                                 URI+=q;
31                                                         }
32                                                         if(n) {
33                                                                 if(!s && !t) { URI+='/'; }
34                                                                 URI+=n;
35                                                         }
36                                                         
37                                                         var res;
38                                                         try {
39                                                                 assert.doesNotThrow( ()=>{ res = new Gopher.Resource(URI); }, 'Could not create from URI "'+URI+'"');
40                                                                 assert( res instanceof Gopher.Resource, "Expected to create a gopher resource.");
41                                                                 assert.equal( res.host, h, 'wrong hostname');
42                                                                 if(P) {
43                                                                         assert.equal(res.port, P.substring(1), 'wrong set port');
44                                                                 } else {
45                                                                         assert.equal(res.port, '70', 'wrong default port');
46                                                                 }
47                                                                 
48                                                                 if(s) {
49                                                                         assert.equal(res.selector, decodeURIComponent(s), 'wrong set selector');
50                                                                 } else if(!q) {
51                                                                         assert.equal(res.selector, '', 'wrong default selector');
52                                                                 }
53                                                                 if(q && s) {
54                                                                         assert.equal(res.query, decodeURIComponent(q.substring(1)), 'wrong set query');
55                                                                 } else {
56                                                                         assert.equal(res.query, false, 'wrong default query');
57                                                                 }
58
59                                                                 if(n) {
60                                                                         assert.equal(res.name, decodeURIComponent(n.substring(1)), 'wrong set name');
61                                                                 }
62                                                                 ok++;
63                                                                 //console.log( '{ "URI": '+URI+', "Resource": '+res.toJson() );
64                                                         } catch(e) {
65                                                                 fail++;
66                                                                 console.error('Could not create resource from URI "'+URI+'"',e);
67                                                         }
68                                                 });
69                                         });
70                                 });
71                         });
72                 });
73         });
74 });
75
76 console.log('Ok: '+ok+' Fail: '+fail);
77
78 if(fail) {
79         process.exit(1);
80 }