rspec-grape 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -4
- data/lib/rspec/grape/exceptions.rb +8 -0
- data/lib/rspec/grape/methods.rb +34 -4
- data/lib/rspec/grape/utils.rb +7 -2
- data/lib/rspec/grape/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 344cd46b986acfa1c98f7df3419a1a51864c78c3
|
4
|
+
data.tar.gz: 2afd9b00e7aca8e40e585ec4ed8f91ba699107f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd7def411a10fc6144d71b73e3bdd4f5e8dcac2dfd06870504a576c6b4a0a7ada075de768dd4c0fa4719310abb64bfb8cda02d80cebbab7b33f944d6ab09ae24
|
7
|
+
data.tar.gz: 2d85a9763c394be30c67320ea9e3ed10a0db18cc17c52624f0f28108bb29c29721eb03091e890c681e0659952c83c2e3f5fcfd649051d57550f703206edbd239
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Rspec::Grape
|
1
|
+
# Rspec::Grape [![Build Status](https://travis-ci.org/ktimothy/rspec-grape.svg?branch=master)](https://travis-ci.org/ktimothy/rspec-grape) [![Gem Version](https://badge.fury.io/rb/rspec-grape.svg)](https://badge.fury.io/rb/rspec-grape)
|
2
2
|
|
3
3
|
This gem is a set of spec helpers, that will help to test [Grape](https://github.com/ruby-grape/grape) APIs easily. The usual approach to test apis, as [official documentation shows](https://github.com/ruby-grape/grape#rspec), is:
|
4
4
|
|
@@ -87,6 +87,20 @@ expect_endpoint_not_to receive(:dont_help)
|
|
87
87
|
|
88
88
|
Note that under the hood those methods use `Grape::Endpoint.before_each`, as suggested by [documentation](https://github.com/ruby-grape/grape#stubbing-helpers). Thanks to [Jon Rowe](https://github.com/JonRowe) for the idea.
|
89
89
|
|
90
|
+
### Inline parameters
|
91
|
+
|
92
|
+
When you define some parameters in url like
|
93
|
+
```ruby
|
94
|
+
get '/url/with/:param'
|
95
|
+
```
|
96
|
+
you can use `parameterized_api_url` helper. You can set parameter values via `:api_params` or expliciltly pass them to helper. The result will be url with parameter names substituted with actual values:
|
97
|
+
```ruby
|
98
|
+
parameterized_api_url(param: 'defined') # '/url/with/defined'
|
99
|
+
```
|
100
|
+
|
101
|
+
If some parameters are not set, method will raise `RSpec::Grape::UrlNotSetException`.
|
102
|
+
Note that `call_api` helper will use parameterized_url to generate url to be called.
|
103
|
+
|
90
104
|
### Additional spec helpers
|
91
105
|
|
92
106
|
It is also possible to use two methods in your specs: `api_url` and `api_method`. The former returns url from spec description, while the latter returns http method.
|
@@ -99,9 +113,6 @@ send(api_method, api_url)
|
|
99
113
|
|
100
114
|
Note that you do not need to `include Rack::Test::Methods` as they are already included by gem.
|
101
115
|
|
102
|
-
## TODO
|
103
|
-
|
104
|
-
* Support urls with params: `/api/test/:id`
|
105
116
|
|
106
117
|
## Development
|
107
118
|
|
@@ -3,5 +3,13 @@ module RSpec
|
|
3
3
|
class NoEndpointDescription < StandardError
|
4
4
|
def message; 'Endpoint description like \'METHOD /path/to/endpoint\' is not found!'; end
|
5
5
|
end
|
6
|
+
|
7
|
+
class UrlParameterNotSet < StandardError
|
8
|
+
def message; 'Url parameter is not defined!'; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class UrlIsNotParameterized < StandardError
|
12
|
+
def message; 'Url has no parameters'; end
|
13
|
+
end
|
6
14
|
end
|
7
15
|
end
|
data/lib/rspec/grape/methods.rb
CHANGED
@@ -8,17 +8,39 @@ module RSpec
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def api_method
|
11
|
-
api_endpoint_description.split(' ').first.downcase.to_sym
|
11
|
+
@api_method ||= api_endpoint_description.split(' ').first.downcase.to_sym
|
12
12
|
end
|
13
13
|
|
14
14
|
def api_url
|
15
|
-
api_endpoint_description.split(' ').last
|
15
|
+
@api_url ||= api_endpoint_description.split(' ').last
|
16
|
+
end
|
17
|
+
|
18
|
+
def parameterized_api_url(params = nil)
|
19
|
+
raise RSpec::Grape::UrlIsNotParameterized unless parameterized_url?
|
20
|
+
|
21
|
+
params ||= resolve_params(params)
|
22
|
+
|
23
|
+
url = api_url.dup
|
24
|
+
names = RSpec::Grape::Utils.url_param_names(api_url)
|
25
|
+
names.each do |name|
|
26
|
+
raise RSpec::Grape::UrlParameterNotSet unless params.has_key?(name.to_sym)
|
27
|
+
|
28
|
+
url[":#{name}"] = params[name].to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
url
|
16
32
|
end
|
17
33
|
|
18
34
|
def call_api(params = nil)
|
19
|
-
params ||=
|
35
|
+
params ||= resolve_params(params)
|
20
36
|
|
21
|
-
|
37
|
+
if parameterized_url?
|
38
|
+
url = parameterized_api_url(params)
|
39
|
+
else
|
40
|
+
url = api_url
|
41
|
+
end
|
42
|
+
|
43
|
+
self.send(api_method, url, params)
|
22
44
|
end
|
23
45
|
|
24
46
|
def expect_endpoint_to(matcher)
|
@@ -35,6 +57,14 @@ module RSpec
|
|
35
57
|
def api_endpoint_description
|
36
58
|
@api_endpoint_description ||= RSpec::Grape::Utils.find_endpoint_description(self.class)
|
37
59
|
end
|
60
|
+
|
61
|
+
def parameterized_url?
|
62
|
+
api_url =~ /\/:/
|
63
|
+
end
|
64
|
+
|
65
|
+
def resolve_params(params = nil)
|
66
|
+
params || self.respond_to?(:api_params) ? api_params : {}
|
67
|
+
end
|
38
68
|
end
|
39
69
|
end
|
40
70
|
end
|
data/lib/rspec/grape/utils.rb
CHANGED
@@ -2,10 +2,11 @@ module RSpec
|
|
2
2
|
module Grape
|
3
3
|
module Utils
|
4
4
|
HTTP_METHODS = %w[GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK]
|
5
|
-
|
5
|
+
DESCRIPTION_REGEXP = /(#{HTTP_METHODS.join('|')}) \/.*/
|
6
|
+
PARAMS_REGEXP = /(\/:([^\/]*))/
|
6
7
|
|
7
8
|
def self.is_description_valid?(description)
|
8
|
-
!!(description =~
|
9
|
+
!!(description =~ DESCRIPTION_REGEXP)
|
9
10
|
end
|
10
11
|
|
11
12
|
def self.find_endpoint_description(klass)
|
@@ -18,6 +19,10 @@ module RSpec
|
|
18
19
|
|
19
20
|
ancestors.last.description
|
20
21
|
end
|
22
|
+
|
23
|
+
def self.url_param_names(url)
|
24
|
+
url.scan(PARAMS_REGEXP).map { |a| a[1].to_sym }
|
25
|
+
end
|
21
26
|
end
|
22
27
|
end
|
23
28
|
end
|
data/lib/rspec/grape/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-grape
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timothy Kovalev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-test
|