UNPKG

3.52 kBJavaScriptView Raw
1/* global describe, it, before, beforeEach, after, afterEach */
2var should = require('should');
3
4var state = require('state');
5
6// Load the state machine definition
7var Uri = require('../state/uri');
8
9describe('URI state machine', function() {
10 // plain javascript object
11 var uri = {};
12
13 before(function() {
14 uri = new Uri();
15 });
16
17 it('makes objects stateful', function() {
18 should.exist(uri.state);
19 uri.state.should.be.Function;
20 });
21
22 it('should have Initial state', function() {
23 uri.state().name.should.equal('Initial');
24 });
25
26 it('should not be able to go Url', function() {
27 // officially sanction method
28 uri.state().change('Url');
29 uri.state().name.should.equal('Initial');
30
31 });
32 it('should not be able to go Port', function() {
33 // more convenient alias
34 uri.state().go('Port');
35 uri.state().name.should.equal('Initial');
36
37 });
38 it('should be able to go File', function() {
39 // magic shorthand. not recommended
40 uri.state('-> File');
41 uri.state().name.should.equal('File');
42 });
43});
44
45describe('change states with input', function() {
46 var uri = null;
47
48 // wrap the object with a state machine
49 beforeEach(function() {
50 uri = new Uri();
51 });
52
53 it('should consider no input a filename', function() {
54 uri.set();
55 uri.state().name.should.equal('File');
56 });
57
58 it('should handle ports', function() {
59 uri.set(3000);
60 uri.state().name.should.equal('Port');
61 });
62
63
64 it('should handle urls', function() {
65 uri.set('http://api.example.com/v2');
66 uri.state().name.should.equal('Url');
67 });
68
69
70 it('should handle filenames', function() {
71 uri.set('/tmp/example.sock');
72 uri.state().name.should.equal('File');
73 });
74
75});
76
77describe('request urls', function() {
78 var uri = null;
79
80 // wrap the object with a state machine
81 beforeEach(function() {
82 uri = new Uri();
83 });
84
85 it('sockets should give valid urls', function() {
86 uri.set();
87 var filename = uri.getFilename();
88 var socket = 'http://unix:' + filename + ':/';
89 var url = require('url');
90 var _url = url.parse(socket);
91 uri.requestUrl().should.equal(socket);
92 uri.requestUrl('path').should.equal(socket + 'path');
93 uri.requestUrl('/path').should.equal(socket + 'path');
94 });
95
96 it('ports should give valid urls', function() {
97 uri.set(3000);
98 var portUrl = 'http://0.0.0.0:3000';
99
100 uri.requestUrl().should.equal(portUrl + '/');
101 uri.requestUrl('path').should.equal(portUrl + '/path');
102 uri.requestUrl('/path').should.equal(portUrl + '/path');
103 });
104
105
106 it('urls should give valid urls', function() {
107 var urlUrl = 'http://test.example.com:3200/api/test';
108 uri.set(urlUrl);
109
110 uri.requestUrl().should.equal(urlUrl + '/');
111 uri.requestUrl('path').should.equal(urlUrl + '/path');
112 uri.requestUrl('/path').should.equal(urlUrl + '/path');
113 });
114
115});
116
117describe('listen urls', function() {
118 var uri = null;
119
120 // wrap the object with a state machine
121 beforeEach(function() {
122 uri = new Uri();
123 });
124
125 it('sockets should give valid urls', function() {
126 uri.set();
127 var filename = uri.getFilename();
128 uri.listenUrl()[0].should.equal(filename);
129 });
130
131 it('ports should give valid urls', function() {
132 uri.set(3000);
133 uri.listenUrl()[0].should.equal(3000);
134 });
135
136
137 it('urls should give valid urls', function() {
138 var urlUrl = 'http://test.example.com:3200/api/test';
139 uri.set(urlUrl);
140 uri.listenUrl()[0].should.equal(3200);
141 uri.listenUrl()[1].should.equal('test.example.com');
142 });
143
144
145});