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
|
require 'support'
require 'mustermann/extension'
require 'sinatra/base'
require 'rack/test'
describe Mustermann::Extension do
include Rack::Test::Methods
subject :app do
Sinatra.new do
set :environment, :test
register Mustermann
end
end
it 'sets up the extension' do
app.should be_a(Mustermann::Extension)
end
context 'uses Sinatra-style patterns by default' do
before { app.get('/:slug(.:extension)?') { params[:slug] } }
example { get('/foo') .body.should be == 'foo' }
example { get('/foo.') .body.should be == 'foo.' }
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/a%20b') .body.should be == 'a b' }
end
describe :except do
before { app.get('/auth/*', except: '/auth/login') { 'ok' } }
example { get('/auth/dunno').should be_ok }
example { get('/auth/login').should_not be_ok }
end
describe :capture do
context 'global' do
before do
app.set(:pattern, capture: { ext: %w[png jpg gif] })
app.get('/:slug(.:ext)?') { params[:slug] }
end
example { get('/foo.bar').body.should be == 'foo.bar' }
example { get('/foo.png').body.should be == 'foo' }
end
context 'route local' do
before do
app.set(:pattern, nil)
app.get('/:id', capture: /\d+/) { 'ok' }
end
example { get('/42').should be_ok }
example { get('/foo').should_not be_ok }
end
context 'global and route local' do
context 'global is a hash' do
before do
app.set(:pattern, capture: { id: /\d+/ })
app.get('/:id(.:ext)?', capture: { ext: 'png' }) { ?a }
app.get('/:id', capture: { id: 'foo' }) { ?b }
app.get('/:id', capture: :alpha) { ?c }
end
example { get('/20') .body.should be == ?a }
example { get('/20.png') .body.should be == ?a }
example { get('/foo') .body.should be == ?b }
example { get('/bar') .body.should be == ?c }
end
context 'global is not a hash' do
before do
app.set(:pattern, capture: /\d+/)
app.get('/:slug(.:ext)?', capture: { ext: 'png' }) { params[:slug] }
app.get('/:slug', capture: :alpha) { 'ok' }
end
example { get('/20.png').should be_ok }
example { get('/foo.png').should_not be_ok }
example { get('/foo').should be_ok }
example { get('/20.png') .body.should be == '20' }
example { get('/42') .body.should be == '42' }
example { get('/foo') .body.should be == 'ok' }
end
end
end
describe :pattern do
describe :except do
before { app.get('/auth/*', pattern: { except: '/auth/login' }) { 'ok' } }
example { get('/auth/dunno').should be_ok }
example { get('/auth/login').should_not be_ok }
end
describe :capture do
context 'route local' do
before do
app.set(:pattern, nil)
app.get('/:id', pattern: { capture: /\d+/ }) { 'ok' }
end
example { get('/42').should be_ok }
example { get('/foo').should_not be_ok }
end
context 'global and route local' do
context 'global is a hash' do
before do
app.set(:pattern, capture: { id: /\d+/ })
app.get('/:id(.:ext)?', pattern: { capture: { ext: 'png' }}) { ?a }
app.get('/:id', pattern: { capture: { id: 'foo' }}) { ?b }
app.get('/:id', pattern: { capture: :alpha }) { ?c }
end
example { get('/20') .body.should be == ?a }
example { get('/20.png') .body.should be == ?a }
example { get('/foo') .body.should be == ?b }
example { get('/bar') .body.should be == ?c }
end
context 'global is not a hash' do
before do
app.set(:pattern, capture: /\d+/)
app.get('/:slug(.:ext)?', pattern: { capture: { ext: 'png' }}) { params[:slug] }
app.get('/:slug', pattern: { capture: :alpha }) { 'ok' }
end
example { get('/20.png').should be_ok }
example { get('/foo.png').should_not be_ok }
example { get('/foo').should be_ok }
example { get('/20.png') .body.should be == '20' }
example { get('/42') .body.should be == '42' }
example { get('/foo') .body.should be == 'ok' }
end
end
end
describe :greedy do
context 'default' do
before { app.get('/:name.:ext') { params[:name] }}
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/foo.bar.baz') .body.should be == 'foo.bar' }
end
context 'enabled' do
before { app.get('/:name.:ext', pattern: { greedy: true }) { params[:name] }}
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/foo.bar.baz') .body.should be == 'foo.bar' }
end
context 'disabled' do
before { app.get('/:name.:ext', pattern: { greedy: false }) { params[:name] }}
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/foo.bar.baz') .body.should be == 'foo' }
end
context 'global' do
before do
app.set(:pattern, greedy: false)
app.get('/:name.:ext') { params[:name] }
end
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/foo.bar.baz') .body.should be == 'foo' }
end
end
describe :space_matches_plus do
context 'default' do
before { app.get('/foo bar') { 'ok' }}
example { get('/foo%20bar') .should be_ok }
example { get('/foo+bar') .should be_ok }
end
context 'enabled' do
before { app.get('/foo bar', pattern: { space_matches_plus: true }) { 'ok' }}
example { get('/foo%20bar') .should be_ok }
example { get('/foo+bar') .should be_ok }
end
context 'disabled' do
before { app.get('/foo bar', pattern: { space_matches_plus: false }) { 'ok' }}
example { get('/foo%20bar') .should be_ok }
example { get('/foo+bar') .should_not be_ok }
end
context 'global' do
before do
app.set(:pattern, space_matches_plus: false)
app.get('/foo bar') { 'ok' }
end
example { get('/foo%20bar') .should be_ok }
example { get('/foo+bar') .should_not be_ok }
end
end
describe :uri_decode do
context 'default' do
before { app.get('/&') { 'ok' }}
example { get('/&') .should be_ok }
example { get('/%26') .should be_ok }
end
context 'enabled' do
before { app.get('/&', pattern: { uri_decode: true }) { 'ok' }}
example { get('/&') .should be_ok }
example { get('/%26') .should be_ok }
end
context 'disabled' do
before { app.get('/&', pattern: { uri_decode: false }) { 'ok' }}
example { get('/&') .should be_ok }
example { get('/%26') .should_not be_ok }
end
context 'global' do
before do
app.set(:pattern, uri_decode: false)
app.get('/&') { 'ok' }
end
example { get('/&') .should be_ok }
example { get('/%26') .should_not be_ok }
end
end
end
describe :type do
describe :identity do
before do
app.set(:pattern, type: :identity)
app.get('/:foo') { 'ok' }
end
example { get('/:foo').should be_ok }
example { get('/foo').should_not be_ok }
end
describe :rails do
before do
app.set(:pattern, type: :rails)
app.get('/:slug(.:extension)') { params[:slug] }
end
example { get('/foo') .body.should be == 'foo' }
example { get('/foo.') .body.should be == 'foo.' }
example { get('/foo.bar') .body.should be == 'foo' }
example { get('/a%20b') .body.should be == 'a b' }
end
describe :shell do
before do
app.set(:pattern, type: :shell)
app.get('/{foo,bar}') { 'ok' }
end
example(nil, skip: true) { get('/foo').should be_ok }
example(nil, skip: true) { get('/bar').should be_ok }
end
describe :simple do
before do
app.set(:pattern, type: :simple)
app.get('/(a)') { 'ok' }
end
example { get('/(a)').should be_ok }
example { get('/a').should_not be_ok }
end
describe :simple do
before do
app.set(:pattern, type: :template)
app.get('/foo{/segments*}{.ext}') { "%p %p" % [params[:segments], params[:ext]] }
end
example { get('/foo/a.png').should be_ok }
example { get('/foo/a').should_not be_ok }
example { get('/foo/a.png').body.should be == '["a"] "png"' }
example { get('/foo/a/b.png').body.should be == '["a", "b"] "png"' }
end
end
context 'works with filters' do
before do
app.before('/auth/*', except: '/auth/login') { halt 'auth required' }
app.get('/auth/login') { 'please log in' }
end
example { get('/auth/dunno').body.should be == 'auth required' }
example { get('/auth/login').body.should be == 'please log in' }
end
end
|