forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_spec.rb
252 lines (207 loc) · 7.23 KB
/
path_spec.rb
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
require 'spec_helper'
module Grape
describe Path do
describe '#initialize' do
it 'remembers the path' do
path = Path.new('/:id', anything, anything)
expect(path.raw_path).to eql('/:id')
end
it 'remembers the namespace' do
path = Path.new(anything, '/users', anything)
expect(path.namespace).to eql('/users')
end
it 'remebers the settings' do
path = Path.new(anything, anything, foo: 'bar')
expect(path.settings).to eql(foo: 'bar')
end
end
describe '#mount_path' do
it 'is nil when no mount path setting exists' do
path = Path.new(anything, anything, {})
expect(path.mount_path).to be_nil
end
it 'is nil when the mount path is nil' do
path = Path.new(anything, anything, mount_path: nil)
expect(path.mount_path).to be_nil
end
it 'splits the mount path' do
path = Path.new(anything, anything, mount_path: %w(foo bar))
expect(path.mount_path).to eql(%w(foo bar))
end
end
describe '#root_prefix' do
it 'is nil when no root prefix setting exists' do
path = Path.new(anything, anything, {})
expect(path.root_prefix).to be_nil
end
it 'is nil when the mount path is nil' do
path = Path.new(anything, anything, root_prefix: nil)
expect(path.root_prefix).to be_nil
end
it 'splits the mount path' do
path = Path.new(anything, anything, root_prefix: 'hello/world')
expect(path.root_prefix).to eql(%w(hello world))
end
end
describe '#uses_path_versioning?' do
it 'is false when the version setting is nil' do
path = Path.new(anything, anything, version: nil)
expect(path.uses_path_versioning?).to be false
end
it 'is false when the version option is header' do
path = Path.new(
anything,
anything,
version: 'v1',
version_options: { using: :header }
)
expect(path.uses_path_versioning?).to be false
end
it 'is true when the version option is path' do
path = Path.new(
anything,
anything,
version: 'v1',
version_options: { using: :path }
)
expect(path.uses_path_versioning?).to be true
end
end
describe '#namespace?' do
it 'is false when the namespace is nil' do
path = Path.new(anything, nil, anything)
expect(path.namespace?).to be nil
end
it 'is false when the namespace starts with whitespace' do
path = Path.new(anything, ' /foo', anything)
expect(path.namespace?).to be nil
end
it 'is false when the namespace is the root path' do
path = Path.new(anything, '/', anything)
expect(path.namespace?).to be false
end
it 'is true otherwise' do
path = Path.new(anything, '/world', anything)
expect(path.namespace?).to be true
end
end
describe '#path?' do
it 'is false when the path is nil' do
path = Path.new(nil, anything, anything)
expect(path.path?).to be nil
end
it 'is false when the path starts with whitespace' do
path = Path.new(' /foo', anything, anything)
expect(path.path?).to be nil
end
it 'is false when the path is the root path' do
path = Path.new('/', anything, anything)
expect(path.path?).to be false
end
it 'is true otherwise' do
path = Path.new('/hello', anything, anything)
expect(path.path?).to be true
end
end
describe '#path' do
context 'mount_path' do
it 'is not included when it is nil' do
path = Path.new(nil, nil, mount_path: '/foo/bar')
expect(path.path).to eql '/foo/bar'
end
it 'is included when it is not nil' do
path = Path.new(nil, nil, {})
expect(path.path).to eql('/')
end
end
context 'root_prefix' do
it 'is not included when it is nil' do
path = Path.new(nil, nil, {})
expect(path.path).to eql('/')
end
it 'is included after the mount path' do
path = Path.new(
nil,
nil,
mount_path: '/foo',
root_prefix: '/hello'
)
expect(path.path).to eql('/foo/hello')
end
end
it 'uses the namespace after the mount path and root prefix' do
path = Path.new(
nil,
'namespace',
mount_path: '/foo',
root_prefix: '/hello'
)
expect(path.path).to eql('/foo/hello/namespace')
end
it 'uses the raw path after the namespace' do
path = Path.new(
'raw_path',
'namespace',
mount_path: '/foo',
root_prefix: '/hello'
)
expect(path.path).to eql('/foo/hello/namespace/raw_path')
end
end
describe '#suffix' do
context 'when using a specific format' do
it 'accepts specified format' do
path = Path.new(nil, nil, {})
allow(path).to receive(:uses_specific_format?) { true }
allow(path).to receive(:settings) { { format: :json } }
expect(path.suffix).to eql('(.json)')
end
end
context 'when path versioning is used' do
it "includes a '/'" do
path = Path.new(nil, nil, {})
allow(path).to receive(:uses_specific_format?) { false }
allow(path).to receive(:uses_path_versioning?) { true }
expect(path.suffix).to eql('(/.:format)')
end
end
context 'when path versioning is not used' do
it "does not include a '/' when the path has a namespace" do
path = Path.new(nil, 'namespace', {})
allow(path).to receive(:uses_specific_format?) { false }
allow(path).to receive(:uses_path_versioning?) { true }
expect(path.suffix).to eql('(.:format)')
end
it "does not include a '/' when the path has a path" do
path = Path.new('/path', nil, {})
allow(path).to receive(:uses_specific_format?) { false }
allow(path).to receive(:uses_path_versioning?) { true }
expect(path.suffix).to eql('(.:format)')
end
it "includes a '/' otherwise" do
path = Path.new(nil, nil, {})
allow(path).to receive(:uses_specific_format?) { false }
allow(path).to receive(:uses_path_versioning?) { true }
expect(path.suffix).to eql('(/.:format)')
end
end
end
describe '#path_with_suffix' do
it 'combines the path and suffix' do
path = Path.new(nil, nil, {})
allow(path).to receive(:path) { '/the/path' }
allow(path).to receive(:suffix) { 'suffix' }
expect(path.path_with_suffix).to eql('/the/pathsuffix')
end
context 'when using a specific format' do
it 'might have a suffix with specified format' do
path = Path.new(nil, nil, {})
allow(path).to receive(:path) { '/the/path' }
allow(path).to receive(:uses_specific_format?) { true }
allow(path).to receive(:settings) { { format: :json } }
expect(path.path_with_suffix).to eql('/the/path(.json)')
end
end
end
end
end