wt-rspec-grape 0.2.0 → 0.4.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 +24 -0
- data/lib/rspec/grape/methods.rb +11 -2
- data/lib/rspec/grape/version.rb +1 -1
- data/lib/wt-rspec-grape.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 557b54745719600189a9b4ae344430fc6c426ed0fd9b72a63307a5c707c47faf
|
|
4
|
+
data.tar.gz: eb71e02ea97135769162b523699e4092cf9ee37bd5ef7447e9806f9adb11cee2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '083d403ab5d91ccab4bac5b98013f5ceba01b35b45a5bfd63f7e4b353fe1497a294a585207ccd271ec7af0c5564307a5a323cf571cb94515353c2306d54afdc7'
|
|
7
|
+
data.tar.gz: ecaa68b2d9dcb7641383ca8ab7572664f995300b40182cce7ede30a08b7305767dae113ecc106a317d60cc65b6a40a69787f5046393c2ace502be8aa8a3b3068
|
data/README.md
CHANGED
|
@@ -50,6 +50,24 @@ Or use a symbol:
|
|
|
50
50
|
describe MyAPI, :api do
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### Root API
|
|
54
|
+
|
|
55
|
+
By default requests are sent to `described_class`. Since Grape 3.3, mounted APIs no longer inherit helpers, rescue handlers and prefixes from the API they are mounted into, so specs usually need to hit the root API instead. Configure it once in your spec helper:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
RSpec.configure do |config|
|
|
59
|
+
config.grape_app = API
|
|
60
|
+
end
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Any Rack app works, e.g. `Rails.application`. It can also be overridden per example group with metadata:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
describe MyAPI, type: :api, grape_app: OtherAPI do
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Defining `app` (or `let(:app)`) in an example group still takes precedence.
|
|
70
|
+
|
|
53
71
|
### Basic usage
|
|
54
72
|
|
|
55
73
|
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:
|
|
@@ -70,6 +88,12 @@ Params can be passed to `call_api` method:
|
|
|
70
88
|
call_api({foo: :bar})
|
|
71
89
|
```
|
|
72
90
|
|
|
91
|
+
Body params are sent as JSON. When params contain a `Rack::Test::UploadedFile` (at any nesting level), they are sent as `multipart/form-data` instead:
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
call_api(file: Rack::Test::UploadedFile.new('spec/fixtures/doc.pdf', 'application/pdf'))
|
|
95
|
+
```
|
|
96
|
+
|
|
73
97
|
### Stubbing API helpers
|
|
74
98
|
|
|
75
99
|
rspec-grape provides two methods to stub API helpers: `expect_endpoint_to` and `expect_endpoint_not_to`. You can easily write:
|
data/lib/rspec/grape/methods.rb
CHANGED
|
@@ -4,7 +4,7 @@ module RSpec
|
|
|
4
4
|
include Rack::Test::Methods
|
|
5
5
|
|
|
6
6
|
def app
|
|
7
|
-
self.described_class
|
|
7
|
+
self.class.metadata[:grape_app] || RSpec.configuration.grape_app || described_class
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def api_method
|
|
@@ -41,7 +41,7 @@ module RSpec
|
|
|
41
41
|
url = api_url
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
-
if %w[GET HEAD DELETE].include?(api_method.to_s.upcase) || params.empty?
|
|
44
|
+
if %w[GET HEAD DELETE].include?(api_method.to_s.upcase) || params.empty? || file_upload?(params)
|
|
45
45
|
self.send(api_method, url, params)
|
|
46
46
|
else
|
|
47
47
|
self.send(api_method, url, {}, { 'CONTENT_TYPE' => 'application/json', input: params.to_json })
|
|
@@ -66,6 +66,15 @@ module RSpec
|
|
|
66
66
|
def parameterized_url?
|
|
67
67
|
api_url =~ /\/:/
|
|
68
68
|
end
|
|
69
|
+
|
|
70
|
+
def file_upload?(value)
|
|
71
|
+
case value
|
|
72
|
+
when Rack::Test::UploadedFile then true
|
|
73
|
+
when Hash then value.values.any? { |v| file_upload?(v) }
|
|
74
|
+
when Array then value.any? { |v| file_upload?(v) }
|
|
75
|
+
else false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
69
78
|
end
|
|
70
79
|
end
|
|
71
80
|
end
|
data/lib/rspec/grape/version.rb
CHANGED
data/lib/wt-rspec-grape.rb
CHANGED