~hacktivista/leanweb

ref: 122ee2c848ec24bdd2f6c88172fb2cc1a1203959 leanweb/test/route_test.rb -rw-r--r-- 6.6 KiB
122ee2c8Felix Freeman Nested Controller#render 4 months ago
                                                                                
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
# frozen_string_literal: true

# Copyright 2022 Felix Freeman <libsys@hacktivista.org>
#
# This file is part of "LeanWeb" and licensed under the terms of the Hacktivista
# General Public License version 0.1 or (at your option) any later version. You
# should have received a copy of this license along with the software. If not,
# see <https://hacktivista.org/licenses/>.

require 'rack'
require 'test_helper'

# Route tests.
class RouteTest < Minitest::Test
  def test_new__path_only
    route = LeanWeb::Route.new(path: '/')
    assert_equal(
      ['/', 'GET', %w[main MainController index_get], true],
      route_to_array(route)
    )
  end

  def test_new__action_string
    route = LeanWeb::Route.new(path: '/', action: 'action')
    assert_equal(
      %w[main MainController action],
      Array(route.action)
    )
  end

  def test_new__action_tuple
    route = LeanWeb::Route.new(path: '/', action: { 'controller' => 'action' })
    assert_equal(
      %w[controller ControllerController action],
      Array(route.action)
    )
  end

  def test_new__action_file_only_hash
    route = LeanWeb::Route.new(path: '/', action: { file: 'file' })
    assert_equal(
      %w[file FileController index_get],
      Array(route.action)
    )
  end

  def test_new__action_controller_only_hash
    route = LeanWeb::Route.new(path: '/', action: { controller: 'controller' })
    assert_equal(
      %w[main controller index_get],
      Array(route.action)
    )
  end

  def test_new__action_action_only_hash
    route = LeanWeb::Route.new(path: '/', action: { action: 'action' })
    assert_equal(
      %w[main MainController action],
      Array(route.action)
    )
  end

  def test_new__action_full_hash
    route = LeanWeb::Route.new(
      path: '/',
      action: { file: 'file', controller: 'controller', action: 'action' }
    )
    assert_equal(
      %w[file controller action],
      Array(route.action)
    )
  end

  def test_new__all_params
    route = LeanWeb::Route.new(
      path: '/path', method: 'POST', action: 'path_post', static: false
    )
    assert_equal(
      ['/path', 'POST', %w[main MainController path_post], false],
      route_to_array(route)
    )
  end

  def test_path_basename__simple
    route = LeanWeb::Route.new(path: '/simple_path')
    assert_equal('simple_path', route.path_basename)
  end

  def test_path_basename__nested
    route = LeanWeb::Route.new(path: '/a/nested/path')
    assert_equal('path', route.path_basename)
  end

  def test_path_basename__index
    route = LeanWeb::Route.new(path: '/')
    assert_equal('index', route.path_basename)
  end

  def test_path_basename__nested_index
    route = LeanWeb::Route.new(path: '/nested/')
    assert_equal('index', route.path_basename)
  end

  def test_respond__method_no_params
    write_controller(
      'Rack::Response.new("default_action").finish'
    ) do |action|
      route = LeanWeb::Route.new(path: '/', action: action)
      assert_equal(
        [200, {}, ['default_action']],
        route.respond(Rack::Request.new(env('/')))
      )
    end
  end

  def test_respond__method_positional_params
    write_controller(
      'Rack::Response.new("#{param1} #{param2}").finish', # rubocop: disable all
      'positional_get(param1, param2)'
    ) do |action|
      route = LeanWeb::Route.new(
        path: %r{^/method/pos/([^/]+)/(.+)$},
        action: action
      )
      assert_equal(
        [200, {}, ['dynamic route']],
        route.respond(Rack::Request.new(env('/method/pos/dynamic/route')))
      )
    end
  end

  def test_respond__method_keyword_params
    write_controller(
      'Rack::Response.new("#{named1} #{named2}").finish', # rubocop: disable all
      'named_get(named1:, named2:)'
    ) do |action|
      route = LeanWeb::Route.new(
        path: %r{^/method/named/(?<named1>[^/]+)/(?<named2>.+)$},
        action: action
      )
      assert_equal(
        [200, {}, ['dynamic route']],
        route.respond(Rack::Request.new(env('/method/named/dynamic/route')))
      )
    end
  end

  def test_respond__proc
    route = LeanWeb::Route.new(
      path: '/',
      action: -> { Rack::Response.new('proc_response').finish }
    )
    assert_equal(
      [200, {}, ['proc_response']],
      route.respond(Rack::Request.new(env('/')))
    )
  end

  def test_respond__proc_positional_params
    route = LeanWeb::Route.new(
      path: %r{^/proc/positional/([^/]+)/(.+)$},
      action: lambda do |param1, param2|
        Rack::Response.new("#{param1} #{param2}").finish
      end
    )
    assert_equal(
      [200, {}, ['dynamic route']],
      route.respond(Rack::Request.new(env('/proc/positional/dynamic/route')))
    )
  end

  def test_respond__proc_keyword_params
    route = LeanWeb::Route.new(
      path: %r{^/proc/named/(?<named1>[^/]+)/(?<named2>.+)$},
      action: lambda do |named1:, named2:|
        Rack::Response.new("#{named1} #{named2}").finish
      end
    )
    assert_equal(
      [200, {}, ['dynamic route']],
      route.respond(Rack::Request.new(env('/proc/named/dynamic/route')))
    )
  end

  def test_seed_path__hash
    route = LeanWeb::Route.new(path: %r{^/(?<named1>[^/]+)/(?<named2>.+)$})
    assert_equal(
      '/hash/path',
      route.seed_path({ named1: 'hash', named2: 'path' })
    )
  end

  def test_seed_path__array
    route = LeanWeb::Route.new(path: %r{^/([^/]+)/(.+)$})
    assert_equal('/hash/path', route.seed_path(%w[hash path]))
  end

  protected

  def route_to_array(route)
    [route.path, route.method, Array(route.action), route.static]
  end

  def random_word(length = 10)
    chars = [*('a'..'z')]
    word = ''
    length.times { word += chars[rand(chars.size)] }
    word
  end

  # Writes a controller on a random file and opens a block for working with it.
  # @param response [String] Code to be evaluated inside action.
  # @param action [String] Defaults to random word. You can use params too.
  # @param controller [String] Defaults to random word.
  # @yieldparam [Hash] ready-to-use Route action hash.
  # @yieldparam [Tempfile] this controller file.
  def write_controller(
    response, action = random_word, controller = random_word.capitalize
  )
    FileUtils.mkdir_p(LeanWeb::CONTROLLER_PATH)
    Tempfile.create(['', '.rb'], LeanWeb::CONTROLLER_PATH) do |file|
      file.write(
        <<~RUBY
          require 'leanweb'
          require 'rack'
          class #{controller} < LeanWeb::Controller
            def #{action}
              #{response}
            end
          end
        RUBY
      )
      file.rewind
      yield({ file: File.basename(file.path, '.rb'),
              controller: controller,
              action: action.sub(/\(.*/, '') }, file)
    end
  end
end