galago-router 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +35 -1
- data/lib/galago/router.rb +15 -16
- data/lib/galago/router/dsl.rb +1 -0
- data/lib/galago/router/path.rb +6 -3
- data/lib/galago/router/route.rb +20 -14
- data/lib/galago/router/version.rb +1 -1
- data/spec/galago/features/head_spec.rb +52 -0
- data/spec/galago/features/options_spec.rb +36 -0
- data/spec/galago/router/path_spec.rb +6 -8
- data/spec/galago/router/route_spec.rb +93 -53
- data/spec/galago/router/version_spec.rb +1 -1
- data/spec/galago/router_spec.rb +50 -32
- data/spec/spec_helper.rb +1 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cddf3cc990d2f88225ab9f7090d8675ea46b86d
|
4
|
+
data.tar.gz: 5bfc2cea67b7cb5ece9b4dcba8c645fc0bfb300c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e09b70383c09dd9f224fbf1915165281aa5af5eb25108edc5d331bc8cdc0d62d3a66aec4ecfdd31027282706912ccc548921de6f8ad59805110bc699dc22ccce
|
7
|
+
data.tar.gz: 7fea16179a6dcc3e70a1e07bd828c1d9deeeffa18ea0c73d927d4fe7dca4fae360bf18cfd59a06fc9324e2b1f6b7657ca0a222dc71b2861c07458dbe393ae852
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Galago Router
|
2
2
|
|
3
|
-
|
3
|
+
Simple, efficient routing for rack applications.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -40,6 +40,40 @@ end
|
|
40
40
|
run router
|
41
41
|
```
|
42
42
|
|
43
|
+
## Routes
|
44
|
+
|
45
|
+
### OPTIONS
|
46
|
+
|
47
|
+
OPTIONS endpoints are automatically defined for each resource provided. The
|
48
|
+
response will contain an ALLOW header listing the request methods the resource
|
49
|
+
supports.
|
50
|
+
|
51
|
+
### HEAD
|
52
|
+
|
53
|
+
HEAD endpoints are automatically defined for each resource that supports GET.
|
54
|
+
|
55
|
+
|
56
|
+
## Environment
|
57
|
+
|
58
|
+
The router adds information about the route that was called to the environment.
|
59
|
+
All requests will have the following keys added:
|
60
|
+
|
61
|
+
| Key | Example Value |
|
62
|
+
| :------------------- | :------------ |
|
63
|
+
| galago_router.path | '/users/:id' |
|
64
|
+
| galago_router.params | { id: 42 } |
|
65
|
+
|
66
|
+
|
67
|
+
## Responses
|
68
|
+
|
69
|
+
### 405 Method Not Allowed
|
70
|
+
|
71
|
+
In the event a path is requested with an unsupported method, the router will return a method not allowed response.
|
72
|
+
This consists of a 405 status code and an 'Allow' header listing the valid request methods.
|
73
|
+
|
74
|
+
More information: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
75
|
+
|
76
|
+
|
43
77
|
## Contributing
|
44
78
|
|
45
79
|
1. Fork it
|
data/lib/galago/router.rb
CHANGED
@@ -18,25 +18,27 @@ module Galago
|
|
18
18
|
attr_reader :routes
|
19
19
|
|
20
20
|
def initialize(&block)
|
21
|
-
@routes =
|
22
|
-
routes[request_method] = []
|
23
|
-
routes
|
24
|
-
end
|
25
|
-
|
21
|
+
@routes = []
|
26
22
|
Router::DSL.new(self, block) if block_given?
|
27
23
|
end
|
28
24
|
|
29
25
|
def add_route(request_method, path, application)
|
30
|
-
route =
|
31
|
-
|
26
|
+
if route = find_route_by_raw_path(path)
|
27
|
+
route.add_endpoint(request_method, application)
|
28
|
+
else
|
29
|
+
route = Route.new(path)
|
30
|
+
route.add_endpoint(request_method, application)
|
31
|
+
@routes << route
|
32
|
+
end
|
32
33
|
end
|
33
34
|
|
34
35
|
def has_route?(request_method, path)
|
35
|
-
find_route(
|
36
|
+
route = find_route(path)
|
37
|
+
route && route.allowed_methods.include?(request_method)
|
36
38
|
end
|
37
39
|
|
38
40
|
def call(env)
|
39
|
-
if route = find_route(env['
|
41
|
+
if route = find_route(env['PATH_INFO'])
|
40
42
|
route.call(env)
|
41
43
|
else
|
42
44
|
Rack::Response.new("Not Found", 404).finish
|
@@ -45,15 +47,12 @@ module Galago
|
|
45
47
|
|
46
48
|
private
|
47
49
|
|
48
|
-
def find_route(
|
49
|
-
routes
|
50
|
-
route = routes.detect { |route| route.recognizes_path?(path) }
|
50
|
+
def find_route(path)
|
51
|
+
routes.find { |route| route.recognizes_path?(path) }
|
51
52
|
end
|
52
53
|
|
53
|
-
def
|
54
|
-
routes.
|
55
|
-
raise RequestMethodInvalid.new(request_method)
|
56
|
-
end
|
54
|
+
def find_route_by_raw_path(raw_path)
|
55
|
+
routes.find { |route| route.path.to_s == raw_path }
|
57
56
|
end
|
58
57
|
|
59
58
|
end
|
data/lib/galago/router/dsl.rb
CHANGED
data/lib/galago/router/path.rb
CHANGED
@@ -24,11 +24,14 @@ module Galago
|
|
24
24
|
@path_parameters ||= @path.scan(/\:(\w+)/).flatten
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def params_from(env)
|
28
28
|
request = Rack::Request.new(env)
|
29
|
+
params = request.params
|
29
30
|
|
30
31
|
if path_params = identify_params_in_path(request.path)
|
31
|
-
|
32
|
+
params.merge(path_params)
|
33
|
+
else
|
34
|
+
params
|
32
35
|
end
|
33
36
|
end
|
34
37
|
|
@@ -49,7 +52,7 @@ module Galago
|
|
49
52
|
|
50
53
|
def identify_params_in_path(path)
|
51
54
|
if match = regex.match(path)
|
52
|
-
named_parameters.zip(match.captures)
|
55
|
+
Hash[named_parameters.zip(match.captures)]
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
data/lib/galago/router/route.rb
CHANGED
@@ -2,15 +2,19 @@ module Galago
|
|
2
2
|
class Router
|
3
3
|
class Route
|
4
4
|
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :path
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
@request_method = request_method.to_s.upcase
|
7
|
+
def initialize(path)
|
9
8
|
@path = Router::Path.new(path)
|
10
|
-
@
|
9
|
+
@endpoints = { 'OPTIONS' => options }
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_endpoint(request_method, endpoint)
|
13
|
+
@endpoints[request_method] = endpoint
|
14
|
+
end
|
11
15
|
|
12
|
-
|
13
|
-
|
16
|
+
def allowed_methods
|
17
|
+
@endpoints.keys.sort
|
14
18
|
end
|
15
19
|
|
16
20
|
def recognizes_path?(request_path)
|
@@ -18,21 +22,23 @@ module Galago
|
|
18
22
|
end
|
19
23
|
|
20
24
|
def call(env)
|
21
|
-
@path.
|
22
|
-
|
25
|
+
env['galago_router.path'] = @path.to_s
|
26
|
+
env['galago_router.params'] = @path.params_from(env)
|
27
|
+
|
28
|
+
endpoint = @endpoints.fetch(env['REQUEST_METHOD'], method_not_allowed)
|
29
|
+
endpoint.call(env)
|
23
30
|
end
|
24
31
|
|
25
32
|
private
|
26
33
|
|
27
|
-
def
|
28
|
-
|
34
|
+
def method_not_allowed
|
35
|
+
->(env) { [405, { 'Allow' => allowed_methods.join(', ') }, []] }
|
29
36
|
end
|
30
37
|
|
31
|
-
def
|
32
|
-
|
33
|
-
raise Router::RequestMethodInvalid.new(request_method)
|
34
|
-
end
|
38
|
+
def options
|
39
|
+
->(env) { [200, { 'Allow' => allowed_methods.join(', ') }, []] }
|
35
40
|
end
|
41
|
+
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Galago
|
4
|
+
describe "Routes" do
|
5
|
+
describe "HEAD" do
|
6
|
+
|
7
|
+
let(:app) do
|
8
|
+
Galago::Router.new do
|
9
|
+
get '/users', to: ->(env) { Rack::Response.new('get').finish }
|
10
|
+
put '/users/:id', to: ->(env) { Rack::Response.new('put').finish }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when a resource responds to GET requests" do
|
15
|
+
it "adds a HEAD request to the resource" do
|
16
|
+
head '/users'
|
17
|
+
expect(last_response.status).to eql 200
|
18
|
+
end
|
19
|
+
|
20
|
+
it "has no response body" do
|
21
|
+
head '/users'
|
22
|
+
expect(last_response.body).to eql ''
|
23
|
+
end
|
24
|
+
|
25
|
+
it "knows how large the content body would have been" do
|
26
|
+
head '/users'
|
27
|
+
|
28
|
+
content_length = last_response.headers['Content-Length']
|
29
|
+
expect(content_length).to eql 'get'.length.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
it "has the same headers as the GET request" do
|
33
|
+
get '/users'
|
34
|
+
get_headers = last_response.headers
|
35
|
+
|
36
|
+
head '/users'
|
37
|
+
head_headers = last_response.headers
|
38
|
+
|
39
|
+
expect(get_headers).to eql head_headers
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when a resource does not respond to GET requests" do
|
44
|
+
it "returns a 405 method not allowed" do
|
45
|
+
head '/users/1'
|
46
|
+
expect(last_response.status).to eql 405
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Galago
|
4
|
+
describe "Routes" do
|
5
|
+
describe "OPTIONS" do
|
6
|
+
let(:app) do
|
7
|
+
Galago::Router.new do
|
8
|
+
get '/users', to: lambda { |env| [200, {}, ['get']] }
|
9
|
+
post '/users', to: lambda { |env| [200, {}, ['post']] }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when the resource has been defined" do
|
14
|
+
it "returns 200" do
|
15
|
+
options '/users'
|
16
|
+
expect(last_response.status).to eql 200
|
17
|
+
end
|
18
|
+
|
19
|
+
it "lists which request methods have been defined" do
|
20
|
+
options '/users'
|
21
|
+
|
22
|
+
allow_header = last_response.headers['Allow']
|
23
|
+
expect(allow_header).to eql 'GET, HEAD, OPTIONS, POST'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when the resource has not been defined" do
|
28
|
+
it "returns 404" do
|
29
|
+
options '/users/42'
|
30
|
+
expect(last_response.status).to eql 404
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -61,19 +61,18 @@ module Galago
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
describe "#
|
64
|
+
describe "#params_from" do
|
65
65
|
let(:env) do
|
66
|
-
{ 'rack.input' =>
|
66
|
+
{ 'rack.input' => '' }
|
67
67
|
end
|
68
68
|
|
69
69
|
it "adds the params" do
|
70
70
|
env['PATH_INFO'] = '/users/21/posts/42'
|
71
71
|
|
72
72
|
path = Router::Path.new('/users/:user_id/posts/:id')
|
73
|
-
path.
|
73
|
+
params = path.params_from(env)
|
74
74
|
|
75
|
-
|
76
|
-
expect(request.params).to eql({
|
75
|
+
expect(params).to eql({
|
77
76
|
'user_id' => '21',
|
78
77
|
'id' => '42'
|
79
78
|
})
|
@@ -83,10 +82,9 @@ module Galago
|
|
83
82
|
env['PATH_INFO'] = '/users'
|
84
83
|
|
85
84
|
path = Router::Path.new('/users')
|
86
|
-
path.
|
85
|
+
params = path.params_from(env)
|
87
86
|
|
88
|
-
|
89
|
-
expect(request.params).to be_empty
|
87
|
+
expect(params).to be_empty
|
90
88
|
end
|
91
89
|
end
|
92
90
|
|
@@ -1,88 +1,128 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'pry'
|
2
3
|
|
3
4
|
module Galago
|
4
5
|
describe Router::Route do
|
5
6
|
context "#call" do
|
6
7
|
let(:env) do
|
7
|
-
{ '
|
8
|
+
{ 'rack.input' => '' }
|
8
9
|
end
|
9
10
|
|
10
11
|
let(:action) do
|
11
12
|
lambda { |env| "foo" }
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
context "when the request method is allowed" do
|
16
|
+
it "adds the named params to the env" do
|
17
|
+
env['PATH_INFO'] = '/users/bob'
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
route = Router::Route.new('/users/:name')
|
20
|
+
route.add_endpoint('GET', action)
|
20
21
|
|
21
|
-
|
22
|
-
route = Router::Route.new('GET', '/bar', action)
|
23
|
-
expect(action).to receive(:call).with(env)
|
22
|
+
route.call(env)
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
extracted_params = env['galago_router.params']
|
25
|
+
expect(extracted_params).to eql('name' => 'bob')
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
it "adds the path to the env" do
|
29
|
+
route = Router::Route.new('/foo/:id')
|
30
|
+
route.add_endpoint('GET', action)
|
31
|
+
|
32
|
+
route.call(env)
|
33
|
+
|
34
|
+
expect(env['galago_router.path']).to eql '/foo/:id'
|
34
35
|
end
|
35
36
|
|
36
|
-
it "
|
37
|
-
Router::
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
it "calls the route's action" do
|
38
|
+
route = Router::Route.new('/bar')
|
39
|
+
route.add_endpoint('GET', action)
|
40
|
+
|
41
|
+
expect(action).to receive(:call).with(env)
|
42
|
+
|
43
|
+
env['REQUEST_METHOD'] = 'GET'
|
44
|
+
route.call(env)
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
|
-
context "
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
context "when the request method is not allowed" do
|
49
|
+
let(:route) do
|
50
|
+
route = Router::Route.new('/foo')
|
51
|
+
route.add_endpoint('GET', lambda { |env| 'foo' })
|
52
|
+
route.add_endpoint('PUT', lambda { |env| 'foo' })
|
53
|
+
route
|
48
54
|
end
|
49
55
|
|
50
|
-
it "
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
56
|
+
it "has a 405 status code" do
|
57
|
+
env['REQUEST_METHOD'] = 'DELETE'
|
58
|
+
response = route.call(env)
|
55
59
|
|
56
|
-
|
57
|
-
|
58
|
-
path = Router::Path.new('/users')
|
59
|
-
expect(path).to have(0).named_parameters
|
60
|
+
status = response[0]
|
61
|
+
expect(status).to eql(405)
|
60
62
|
end
|
61
63
|
|
62
|
-
it "
|
63
|
-
|
64
|
-
|
64
|
+
it "sets the 'Allow' header" do
|
65
|
+
env['REQUEST_METHOD'] = 'DELETE'
|
66
|
+
response = route.call(env)
|
67
|
+
|
68
|
+
headers = response[1]
|
69
|
+
expect(headers['Allow']).to eql 'GET, OPTIONS, PUT'
|
65
70
|
end
|
66
71
|
end
|
72
|
+
end
|
67
73
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
74
|
+
context "allowed_methods" do
|
75
|
+
let(:route) { Router::Route.new('/users/:id/posts') }
|
76
|
+
|
77
|
+
it "allows OPTIONS by default" do
|
78
|
+
expect(route.allowed_methods).to eql ['OPTIONS']
|
73
79
|
end
|
74
80
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
}.to raise_error Router::ActionInvalid
|
79
|
-
end
|
81
|
+
it "has an allowed method for each endpoint" do
|
82
|
+
route.add_endpoint('POST', anything)
|
83
|
+
route.add_endpoint('PUT', anything)
|
80
84
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
85
|
+
expect(route.allowed_methods).to eql ['OPTIONS', 'POST', 'PUT']
|
86
|
+
end
|
87
|
+
|
88
|
+
it "lists the allowed methods in alphabetical order" do
|
89
|
+
route.add_endpoint('POST', anything)
|
90
|
+
route.add_endpoint('DELETE', anything)
|
91
|
+
route.add_endpoint('GET', anything)
|
92
|
+
|
93
|
+
allowed_methods = route.allowed_methods
|
94
|
+
expect(allowed_methods).to eql ['DELETE', 'GET', 'OPTIONS', 'POST']
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "#recognizes?" do
|
99
|
+
it "recognizes a paths with no named parameters" do
|
100
|
+
path = Router::Path.new('/users')
|
101
|
+
expect(path).to be_recognizes('/users')
|
102
|
+
end
|
103
|
+
|
104
|
+
it "recognizes a path with named parameters" do
|
105
|
+
path = Router::Path.new('/users/:id')
|
106
|
+
expect(path).to be_recognizes('/users/32')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "#named_parameters" do
|
111
|
+
it "has none when no segments start with a ':'" do
|
112
|
+
path = Router::Path.new('/users')
|
113
|
+
expect(path).to have(0).named_parameters
|
114
|
+
end
|
115
|
+
|
116
|
+
it "returns segments starting with a ':'" do
|
117
|
+
path = Router::Path.new('/accounts/:account_id/users/:id')
|
118
|
+
expect(path.named_parameters).to eql ['account_id', 'id']
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "path" do
|
123
|
+
it "remembers the path" do
|
124
|
+
route = Router::Route.new('/foo')
|
125
|
+
expect(route.path.to_s).to eql '/foo'
|
86
126
|
end
|
87
127
|
end
|
88
128
|
end
|
data/spec/galago/router_spec.rb
CHANGED
@@ -12,11 +12,11 @@ module Galago
|
|
12
12
|
delete '/foo' , to: lambda { |env| 'bar' }
|
13
13
|
end
|
14
14
|
|
15
|
-
expect(router).to have_route(
|
16
|
-
expect(router).to have_route(
|
17
|
-
expect(router).to have_route(
|
18
|
-
expect(router).to have_route(
|
19
|
-
expect(router).to have_route(
|
15
|
+
expect(router).to have_route('GET', '/foo')
|
16
|
+
expect(router).to have_route('POST', '/foo')
|
17
|
+
expect(router).to have_route('PATCH', '/foo')
|
18
|
+
expect(router).to have_route('PUT', '/foo')
|
19
|
+
expect(router).to have_route('DELETE', '/foo')
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'adds the namespace to the route' do
|
@@ -32,13 +32,13 @@ module Galago
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
expect(router).to have_route(
|
36
|
-
expect(router).to have_route(
|
35
|
+
expect(router).to have_route('GET', '/foo')
|
36
|
+
expect(router).to have_route('POST', '/foo')
|
37
37
|
|
38
|
-
expect(router).to have_route(
|
39
|
-
expect(router).to have_route(
|
40
|
-
expect(router).to have_route(
|
41
|
-
expect(router).to have_route(
|
38
|
+
expect(router).to have_route('GET', '/foo/1')
|
39
|
+
expect(router).to have_route('PATCH', '/foo/2')
|
40
|
+
expect(router).to have_route('PUT', '/foo/3')
|
41
|
+
expect(router).to have_route('DELETE', '/foo/4')
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'can have multiple namespaces' do
|
@@ -57,10 +57,10 @@ module Galago
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
expect(router).to have_route(
|
61
|
-
expect(router).to have_route(
|
62
|
-
expect(router).to have_route(
|
63
|
-
expect(router).to have_route(
|
60
|
+
expect(router).to have_route('GET', '/foo')
|
61
|
+
expect(router).to have_route('GET', '/foo/bar')
|
62
|
+
expect(router).to have_route('GET', '/foo/bar/1')
|
63
|
+
expect(router).to have_route('GET', '/hello')
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -71,48 +71,43 @@ module Galago
|
|
71
71
|
|
72
72
|
it "stores the route" do
|
73
73
|
router = Router.new
|
74
|
-
router.add_route(
|
74
|
+
router.add_route('GET', '/users', rack_app)
|
75
75
|
|
76
|
-
expect(router).to have_route(
|
77
|
-
end
|
78
|
-
|
79
|
-
it "raises an error when an invalid http verb is provided" do
|
80
|
-
router = Router.new
|
81
|
-
|
82
|
-
expect { router.add_route(:foo, '/foo', rack_app)
|
83
|
-
}.to raise_error Router::RequestMethodInvalid
|
76
|
+
expect(router).to have_route('GET', '/users')
|
84
77
|
end
|
85
78
|
end
|
86
79
|
|
87
80
|
describe "has_route?" do
|
88
81
|
it "returns true when the route has been added" do
|
89
82
|
router = Router.new
|
90
|
-
router.add_route(
|
83
|
+
router.add_route('POST', '/users', lambda {})
|
91
84
|
|
92
|
-
expect(router).to have_route(
|
85
|
+
expect(router).to have_route('POST', '/users')
|
93
86
|
end
|
94
87
|
|
95
88
|
it "returns true when the route has a path param" do
|
96
89
|
router = Router.new
|
97
|
-
router.add_route(
|
90
|
+
router.add_route('GET', '/users/:id', lambda {})
|
98
91
|
|
99
|
-
expect(router).to have_route(
|
92
|
+
expect(router).to have_route('GET', '/users/42')
|
100
93
|
end
|
101
94
|
|
102
95
|
it "returns false when the route has not been added" do
|
103
96
|
router = Router.new
|
104
|
-
router.add_route(
|
97
|
+
router.add_route('POST', '/users', lambda {})
|
105
98
|
|
106
|
-
expect(router).not_to have_route(
|
99
|
+
expect(router).not_to have_route('GET', '/users')
|
107
100
|
end
|
108
101
|
end
|
109
102
|
|
110
103
|
describe "#call" do
|
111
104
|
it "calls the rack app when the route is found" do
|
112
|
-
router = Router.new
|
113
|
-
|
105
|
+
router = Router.new do
|
106
|
+
get '/foo', to: lambda { |env| [200, {}, 'bar'] }
|
107
|
+
end
|
114
108
|
|
115
109
|
response = router.call({
|
110
|
+
'rack.input' => '',
|
116
111
|
'REQUEST_METHOD' => 'GET',
|
117
112
|
'PATH_INFO' => '/foo'
|
118
113
|
})
|
@@ -124,6 +119,7 @@ module Galago
|
|
124
119
|
router = Router.new
|
125
120
|
|
126
121
|
response = router.call({
|
122
|
+
'rack.input' => '',
|
127
123
|
'REQUEST_METHOD' => 'GET',
|
128
124
|
'PATH_INFO' => '/bar'
|
129
125
|
})
|
@@ -134,5 +130,27 @@ module Galago
|
|
134
130
|
end
|
135
131
|
end
|
136
132
|
|
133
|
+
describe "responses" do
|
134
|
+
context "method not allowed" do
|
135
|
+
let(:app) do
|
136
|
+
Router.new do
|
137
|
+
get '/foo', to: lambda { |env| [200, {}, ['foo']] }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'sets the status code to 405' do
|
142
|
+
post '/foo'
|
143
|
+
expect(last_response.status).to eql(405)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "sets the 'Allow' header to the allowed methods" do
|
147
|
+
post '/foo'
|
148
|
+
|
149
|
+
allow = last_response.headers.fetch('Allow')
|
150
|
+
expect(allow).to eql 'GET, HEAD, OPTIONS'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
137
155
|
end
|
138
156
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: galago-router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Karayusuf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -116,6 +116,8 @@ files:
|
|
116
116
|
- lib/galago/router/path.rb
|
117
117
|
- lib/galago/router/route.rb
|
118
118
|
- lib/galago/router/version.rb
|
119
|
+
- spec/galago/features/head_spec.rb
|
120
|
+
- spec/galago/features/options_spec.rb
|
119
121
|
- spec/galago/router/path_spec.rb
|
120
122
|
- spec/galago/router/route_spec.rb
|
121
123
|
- spec/galago/router/version_spec.rb
|
@@ -146,6 +148,8 @@ signing_key:
|
|
146
148
|
specification_version: 4
|
147
149
|
summary: ''
|
148
150
|
test_files:
|
151
|
+
- spec/galago/features/head_spec.rb
|
152
|
+
- spec/galago/features/options_spec.rb
|
149
153
|
- spec/galago/router/path_spec.rb
|
150
154
|
- spec/galago/router/route_spec.rb
|
151
155
|
- spec/galago/router/version_spec.rb
|