summaryrefslogtreecommitdiffstats
path: root/spec/node_spec.rb
blob: 4e9a5bc5a2b3ce97287a9546422c6da864cc337a (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require 'spec_helper'

def clean_static_path
  static_base_path = Node0.app.static_base_path
  Dir.glob(File.join(static_base_path, '**', '*')).each { |f| File.unlink f if File.file? f }
  Dir.glob(File.join(static_base_path, '*')).each { |d| Dir.rmdir d }
  Dir.rmdir static_base_path if File.directory? static_base_path
end

describe Zorglub do
  describe Zorglub::Node do
    before(:all) do
      clean_static_path
    end

    after(:all) do
      clean_static_path
    end

    it "engine should return default Node's engine" do
      expect(Node0.engine).to eq Node0.app.opt(:engine)
    end

    it "layout should return default Node's layout" do
      expect(Node0.layout).to eq Node0.app.opt(:layout)
    end

    it "engine should return class defined Node's engine" do
      expect(Node1.engine).to eq 'engine-1'
      expect(Node3.engine).to eq 'engine-2'
    end

    it "layout should return class defined Node's layout" do
      expect(Node1.layout).to eq 'layout-1'
      expect(Node3.layout).to eq 'layout-2'
    end

    it 'engine should return engine inherited from Node2' do
      expect(Node2.engine).to eq 'engine-1'
    end

    it 'layout should return layout inherited from Node2' do
      expect(Node2.layout).to eq 'layout-1'
    end

    it 'r should build a well formed path' do
      expect(Node1.r(1, 'arg2', 'some')).to eq '/node1/1/arg2/some'
    end

    it 'instance level map should work' do
      r = Node0.my_call '/with_2args/1/2'
      h = YAML.load r[2][0]
      expect(h[:map]).to eq '/node0'
    end

    it 'should return err404 response when no method found' do
      expect(Node0.respond_to?('noresponse')).to be_falsey
      r = Node0.my_call '/noresponse'
      expect(r[2][0]).to eq "Node0 mapped at /node0 can't respond to : noresponse"
      expect(r[0]).to eq 404
    end

    it 'simple method should respond' do
      r = Node0.my_call '/hello'
      expect(r[0]).to eq 200
      expect(r[2][0]).to eq 'world'
    end

    it 'instance level args should work' do
      r = Node0.my_call '/with_2args/1/2'
      h = YAML.load r[2][0]
      expect(h[:args][0]).to eq '1'
      expect(h[:args][1]).to eq '2'
    end

    it 'should raise error when too much arguments' do
      expect { Node0.my_call '/with_2args/1/2/3' }.to raise_error ArgumentError
    end

    it 'layout proc, method level layout and engine definitions should work' do
      r = Node0.my_call '/index'
      expect(r[0]).to eq 200
      h = YAML.load r[2][0]
      ly = File.join Node0.app.layout_base_path, Node0.layout
      vu = File.join Node0.app.view_base_path, Node0.r, 'index'
      expect(h[:path]).to eq ly
      expect(h[:layout]).to eq ly
      expect(h[:view]).to eq vu
    end

    it 'layout proc, method level layout and engine definitions should work' do
      r = Node1.my_call '/index'
      expect(r[0]).to eq 200
      h = YAML.load r[2][0]
      ly = File.join Node1.app.layout_base_path, 'main.spec'
      vu = File.join Node1.app.view_base_path, Node1.r, 'index.spec'
      expect(h[:path]).to eq ly
      expect(h[:layout]).to eq ly
      expect(h[:view]).to eq vu
    end

    it 'before_all hook should work' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.before).to eq 0
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 1
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 2
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 3
    end

    it 'after_all hook should work' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.after).to eq 0
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 1
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 2
      expect(Node3.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 3
    end

    it 'inherited before_all hook should work' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.before).to eq 0
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 1
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 2
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.before).to eq 3
    end

    it 'inherited after_all hook should work' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.after).to eq 0
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 1
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 2
      expect(Node8.my_call_i('/index')).to eq 1
      expect(Node3.after).to eq 3
    end

    it 'should find view and layout and render them' do
      r = Node0.my_call '/do_render'
      expect(r[0]).to eq 200
      expect(r[2][0]).to eq 'layout_start view_content layout_end'
    end

    it 'default mime-type should be text/html' do
      r = Node0.my_call '/index'
      expect(r[1]['Content-type']).to eq 'text/html'
    end

    it 'should be able to override mime-type' do
      r = Node0.my_call '/do_render'
      expect(r[1]['Content-type']).to eq 'text/view'
    end

    it 'should be able to override through rack response mime-type' do
      r = Node0.my_call '/do_content_type'
      expect(r[1]['Content-type']).to eq 'text/mine'
    end

    it 'partial should render correctly' do
      expect(Node0.partial({}, :do_partial, 1, 2)).to eq 'partial_content'
    end

    it 'method level view should work' do
      expect(Node0.partial({}, :other_view)).to eq 'partial_content'
    end

    it 'partial with hooks should be default' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.partial({}, :do_partial, 1, 2)).to eq 'partial_content'
      expect(Node3.before).to eq 1
      expect(Node3.after).to eq 1
    end

    it 'partial without hooks should work' do
      Node3.before = 0
      Node3.after = 0
      expect(Node3.partial({ no_hooks: true }, :do_partial, 1, 2)).to eq 'partial_content'
      expect(Node3.before).to eq 0
      expect(Node3.after).to eq 0
    end

    it 'static pages should be generated' do
      r = Node6.my_call '/do_static'
      expect(r[2][0]).to eq 'VAL 1'
      expect(r[1]['Content-type']).to eq 'text/static'
      r = Node6.my_call '/do_static'
      expect(r[2][0]).to eq 'VAL 1'
      expect(r[1]['Content-type']).to eq 'text/static'
      r = Node6.my_call '/do_static'
      expect(r[2][0]).to eq 'VAL 1'
      expect(r[1]['Content-type']).to eq 'text/static'
      r = Node6.my_call '/no_static'
      expect(r[2][0]).to eq 'VAL 4'
      expect(r[1]['Content-type']).to eq 'text/static'
      r = Node6.my_call '/do_static'
      expect(r[2][0]).to eq 'VAL 1'
      expect(r[1]['Content-type']).to eq 'text/static'
      Node6.static! true, 0.000001
      sleep 0.0001
      r = Node6.my_call '/do_static'
      expect(r[2][0]).to eq 'VAL 6'
      expect(r[1]['Content-type']).to eq 'text/static'
    end

    it 'redirect should work' do
      r = Node0.my_call '/do_redirect'
      expect(r[0]).to eq 302
      expect(r[1]['location']).to eq Node0.r(:do_partial, 1, 2, 3)
    end

    it 'no_layout! should be inherited' do
      expect(Node5.layout).to be_nil
    end

    it 'cli_vals should be inherited and extended' do
      r = Node5.my_call '/index'
      vars = YAML.load r[2][0]
      expect(vars).to eq %w[js0 js1 js3 jsx css0 css1 css2]
      expect(vars[7]).to be_nil
    end

    it 'cli_vals should be extended at method level' do
      r = Node4.my_call '/more'
      vars = YAML.load r[2][0]
      expect(vars).to eq %w[js0 js1 js2]
      expect(vars[3]).to be_nil
    end

    it 'cli_vals should be untouched' do
      r = Node4.my_call '/index'
      vars = YAML.load r[2][0]
      expect(vars).to eq %w[js0 js1]
      expect(vars[2]).to be_nil
      r = Node5.my_call '/index'
      vars = YAML.load r[2][0]
      expect(vars).to eq %w[js0 js1 js3 jsx css0 css1 css2]
      expect(vars[7]).to be_nil
    end

    it 'ext definition and file engine should work' do
      r = Node0.my_call '/xml_file'
      expect(r[2][0]).to eq "<xml>file<\/xml>\n"
      expect(r[1]['Content-type']).to eq 'application/xml'
      r = Node0.my_call '/plain_file'
      expect(r[2][0]).to eq "plain file\n"
      expect(r[1]['Content-type']).to eq 'text/plain'
    end

    it 'no view no layout should work as well' do
      r = Node0.my_call '/no_view_no_layout'
      expect(r[2][0]).to eq 'hello world'
    end

    it 'haml engine should work' do
      Node0.app.opt! :engines_cache_enabled, false
      r = Node0.my_call '/engines/haml'
      expect(r[2][0]).to eq "<h1>Hello <i>world</i></h1>\n"
      Node0.app.opt! :engines_cache_enabled, true
      r = Node0.my_call '/engines/haml'
      expect(r[2][0]).to eq "<h1>Hello <i>world</i></h1>\n"
    end

    it 'sass engine should work' do
      Node0.app.opt! :engines_cache_enabled, true
      r = Node0.my_call '/engines/sass'
      expect(r[2][0]).to eq "vbar{width:80%;height:23px}vbar ul{list-style-type:none}vbar li{float:left}vbar li a{font-weight:bold}\n"
      Node0.app.opt! :engines_cache_enabled, false
      r = Node0.my_call '/engines/sass'
      expect(r[2][0]).to eq "vbar{width:80%;height:23px}vbar ul{list-style-type:none}vbar li{float:left}vbar li a{font-weight:bold}\n"
    end

    it 'view_base_path! should work' do
      r = Node7.my_call '/view_path'
      h = YAML.load r[2][0]
      expect(h[:view]).to eq File.join(Node7.app.opt(:root), 'alt', 'do_render')
    end

    it 'layout_base_path! should work' do
      r = Node7.my_call '/view_path'
      h = YAML.load r[2][0]
      expect(h[:layout]).to eq File.join(Node7.app.opt(:root), 'alt', 'layout', 'default')
    end

    it 'debug out should work' do
      stdout0 = $stdout.dup
      stdouts = StringIO.new
      $stdout = stdouts
      begin
        APP.opt! :debug, true
        Node0.my_call '/hello'
      ensure
        $stdout = stdout0
      end
      expect(stdouts.string.include?('spec/data/view/node0/hello')).to be true
    end
  end
end