wt-rspec-grape 0.0.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7c7ff180932d3ab13b7ce3d2bcf2f83c5e1854b96bc5d449cd96371b662252c2
4
+ data.tar.gz: 6d30fd19262e4f1b2cfcefd9dfffd5eab343231d647fcb0b7c02255b17d45b25
5
+ SHA512:
6
+ metadata.gz: 6c43ae2cca1a7e1fd1b09d04f7f93610efc8f6a978d8514ec384c95fc83bf3fe69f25c06e0e52f141fccda5a4151878df2afce836f01a8a58ed99e271cff0343
7
+ data.tar.gz: 0d849a85b81ecc246ccc72421ad6f852199330123c5e508f435f9d4214ef56eab1ec284b4ac890990c50e929239d216da7fe17fcb713cfd3a906733f203a8b02
@@ -0,0 +1,22 @@
1
+ name: build
2
+ on:
3
+ push:
4
+ branches:
5
+ - '*'
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ matrix:
12
+ ruby-version: ['3.1', '3.3']
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - name: Set up Ruby ${{ matrix.ruby-version }}
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby-version }}
19
+ - name: Install dependencies
20
+ run: bundle install
21
+ - name: Run tests
22
+ run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.1
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-grape.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Timothy Kovalev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # Rspec::Grape
2
+
3
+ Originally a fork of https://github.com/ktimothy/rspec-grape.
4
+
5
+ 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:
6
+
7
+ ```ruby
8
+ context 'GET /api/v1/test' do
9
+ it 'returns 200' do
10
+ get '/api/v1/test'
11
+ expect(last_response.status).to eq(200)
12
+ end
13
+ end
14
+ ```
15
+
16
+ Here you describe context as `GET /api/v1/test`, then you have to repeat url and method in example: `get '/api/v1/test'`. But what if you don't have to?
17
+
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'rspec-grape'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install rspec-grape
34
+
35
+ ## Usage
36
+
37
+ ### Conventions
38
+
39
+ Gem's behaviour is based on some conventions:
40
+ * `described_class` should point to your API class
41
+ * examples should be grouped by endpoints
42
+ * group of endpoint specs shoud be described as 'HTTP_METHOD /api/path'
43
+
44
+ In order to have helpers available in examples, you need to add `type: :api` metadata:
45
+ ```ruby
46
+ describe MyAPI, type: :api do
47
+ ```
48
+ Or use a symbol:
49
+ ```ruby
50
+ describe MyAPI, :api do
51
+ ```
52
+
53
+ ### Basic usage
54
+
55
+ This gem provides the `call_api` helper method. It automatically reads endpoint url and method from context description, allowing you to avoid duplication and write a shorter spec:
56
+
57
+ ```ruby
58
+ context 'GET /api/v1/test' do
59
+ it 'returns 200' do
60
+ expect(call_api.status).to eq(200)
61
+ end
62
+ end
63
+ ```
64
+
65
+ ### Passing request params
66
+
67
+ Params can be passed to `call_api` method:
68
+
69
+ ```ruby
70
+ call_api({foo: :bar})
71
+ ```
72
+
73
+ ### Stubbing API helpers
74
+
75
+ rspec-grape provides two methods to stub API helpers: `expect_endpoint_to` and `expect_endpoint_not_to`. You can easily write:
76
+
77
+ ```ruby
78
+ expect_endpoint_to receive(:help_me)
79
+ expect_endpoint_not_to receive(:dont_help)
80
+ ```
81
+
82
+ 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.
83
+
84
+ ### Inline parameters
85
+
86
+ When you define some parameters in url like
87
+ ```ruby
88
+ get '/url/with/:param'
89
+ ```
90
+ you can use `parameterized_api_url` helper to generate full url. Pass parameters as hash. The result will be url with parameter names substituted with actual values:
91
+ ```ruby
92
+ parameterized_api_url(param: 'defined') # '/url/with/defined'
93
+ ```
94
+
95
+ If some parameters are not set, method will raise `RSpec::Grape::UrlNotSetException`.
96
+ Note that `call_api` helper will use parameterized_url to generate url to be called.
97
+
98
+ ### Nested descriptions
99
+
100
+ You may need to define nested descriptions of endpoint when you are using inline url parameters:
101
+ ```ruby
102
+ describe 'GET /inline/:param' do
103
+ describe 'GET /inline/false' do
104
+ ...
105
+ end
106
+
107
+ describe 'GET /inline/true' do
108
+ ...
109
+ end
110
+ end
111
+ ```
112
+
113
+ In this case `api_url` will point to inner description, `/inline/false` and `/inline/true` consequently. If you set all inline parameters in description, there is no need to pass parameters to `call_api`.
114
+
115
+ ### Additional spec helpers
116
+
117
+ 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.
118
+
119
+ You can always use them, as `call_api` methods does:
120
+
121
+ ```ruby
122
+ send(api_method, api_url)
123
+ ```
124
+
125
+ Note that you do not need to `include Rack::Test::Methods` as they are already included by gem.
126
+
127
+
128
+ ## Development
129
+
130
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
131
+
132
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
133
+
134
+ ## Contributing
135
+
136
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ktimothy/rspec-grape.
137
+
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
142
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ module RSpec
2
+ module Grape
3
+ class NoEndpointDescription < StandardError
4
+ def message; 'Endpoint description like \'METHOD /path/to/endpoint\' is not found!'; end
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
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ module RSpec
2
+ module Grape
3
+ module Methods
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ self.described_class
8
+ end
9
+
10
+ def api_method
11
+ @api_method ||= api_endpoint_description.split(' ').first.downcase.to_sym
12
+ end
13
+
14
+ def api_url
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 ||= {}
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
32
+ end
33
+
34
+ def call_api(params = nil)
35
+ params ||= {}
36
+
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)
44
+ end
45
+
46
+ def expect_endpoint_to(matcher)
47
+ ::Grape::Endpoint.before_each { |endpoint| expect(endpoint).to matcher }
48
+ end
49
+
50
+ def expect_endpoint_not_to(matcher)
51
+ ::Grape::Endpoint.before_each { |endpoint| expect(endpoint).not_to matcher }
52
+ end
53
+
54
+
55
+ private
56
+
57
+ def api_endpoint_description
58
+ @api_endpoint_description ||= RSpec::Grape::Utils.find_endpoint_description(self.class)
59
+ end
60
+
61
+ def parameterized_url?
62
+ api_url =~ /\/:/
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ module RSpec
2
+ module Grape
3
+ module Utils
4
+ HTTP_METHODS = %w[GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK]
5
+ DESCRIPTION_REGEXP = /(#{HTTP_METHODS.join('|')}) \/.*/
6
+ PARAMS_REGEXP = /(\/:([^\/]*))/
7
+
8
+ def self.is_description_valid?(description)
9
+ !!(description =~ DESCRIPTION_REGEXP)
10
+ end
11
+
12
+ def self.find_endpoint_description(klass)
13
+ ancestors = klass.ancestors.select { |a| a < RSpec::Core::ExampleGroup }
14
+ ancestors = ancestors.select do |a|
15
+ is_description_valid?(a.description)
16
+ end
17
+
18
+ raise RSpec::Grape::NoEndpointDescription unless ancestors.any?
19
+
20
+ ancestors.first.description
21
+ end
22
+
23
+ def self.url_param_names(url)
24
+ url.scan(PARAMS_REGEXP).map { |a| a[1].to_sym }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module Grape
3
+ VERSION = "0.0.5"
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ require 'rspec/core'
2
+ require 'rack/test'
3
+
4
+ require 'rspec/grape/exceptions'
5
+ require 'rspec/grape/utils'
6
+ require 'rspec/grape/methods'
7
+
8
+ RSpec.configure do |config|
9
+ config.include RSpec::Grape::Methods, type: :api
10
+ config.include RSpec::Grape::Methods, :api
11
+
12
+ after = Proc.new do
13
+ ::Grape::Endpoint.before_each nil
14
+ end
15
+
16
+ config.after(:each, :api, &after)
17
+ config.after(:each, type: :api, &after)
18
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec/grape/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wt-rspec-grape"
8
+ spec.version = Rspec::Grape::VERSION
9
+ spec.authors = ["Ivan Tarapon"]
10
+ spec.email = ["ivan.tarapon@wetravel.com"]
11
+
12
+ spec.summary = %q{A set of helpers, which make grape api specs shorter.}
13
+ spec.homepage = "https://github.com/wetravel-com/rspec-grape"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_runtime_dependency "rack-test", "~> 2.0"
20
+ spec.add_runtime_dependency "rspec-core", "~> 3.0"
21
+
22
+ spec.add_development_dependency "bundler", "~> 2.0"
23
+ spec.add_development_dependency "rake", "~> 13.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "grape"
26
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wt-rspec-grape
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Tarapon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack-test
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: grape
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - ivan.tarapon@wetravel.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/build.yml"
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/rspec/grape/exceptions.rb
114
+ - lib/rspec/grape/methods.rb
115
+ - lib/rspec/grape/utils.rb
116
+ - lib/rspec/grape/version.rb
117
+ - lib/wt-rspec-grape.rb
118
+ - rspec-grape.gemspec
119
+ homepage: https://github.com/wetravel-com/rspec-grape
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.5.9
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: A set of helpers, which make grape api specs shorter.
142
+ test_files: []