Comments (6)
@amcaplan is it possible to use the to_query
rails method?
from apivore.
No. to_query
converts a key-value pair into key=value
for use in a URL query string. CGI::escape
is for a single String. It's also in the Ruby standard library, already used in any Rails app, so I'm not sure why we'd not want to use it.
from apivore.
@amcaplan This would be a breaking change as as any string already escaped would be doubly escaped and then fail. It could be considered for a later Apivore version.
Personally, I don't see:
it { is_expected.to validate(:get, '/resources/{CGI.escape(name)}', 200, params) }
as illegible. It is also explicit that there can be things in name
that need special treatment. Often a significant understanding of code can be found by seeing how the tests are written.
There are also many ways to bury and refactor the above into Rails-like URL helpers, thus:
def resources_path(name)
"/resources/#{CGI.escape(name)}"
end
it { is_expected.to validate(:get, resources_path(name), 200, params) }
and many other options.
from apivore.
Breaking change
from apivore.
@gwshaw I think I understood what you said, but I don't think it was expressed clearly. The validation URL has to exactly match what the Swagger spec has. So if Swagger has a URL '/resources/{name}'
, the URL to validate has to be '/resources/{name}'
exactly, with name
set in params
. If you try:
it { is_expected.to validate(:get, '/resources/escaped+name', 200, {})
the endpoint to validate won't be recognized by Apivore.
Instead, you have to do:
let(:params) {{ 'name' => 'string with spaces') }}
it { is_expected.to validate(:get, '/resources/{name}', 200, params) }
which of course breaks, because Apivore tries the URI /resources/string with spaces
, which isn't a valid URI.
I'm still pretty sure you're right, so I've rephrased a bit.
For posterity, here's my understanding:
it { is_expected.to validate(:get, '/resources/{name}', 200, params) }
fails ifparams['name']
is a non-URL-safe String.- I suggested calling
CGI::escape
on URL params to make sure they're URL-safe. - @gwshaw pointed out that if you already escaped it (e.g.,
params['name'] = 'string+with+spaces'
), it'll be re-escaped ('string%2Bwith%2Bspaces'
), which would be a breaking change. - Hence, the recommended solution in this case is to escape it yourself:
let(:params) {{ 'name' => CGI.escape("string with spaces") }}
it { is_expected.to validate(:get, '/resources/{name}', 200, params) }
Is that right?
from apivore.
Sorry, I misinterpreted your code and my examples were then incorrect, but yes, you need to escape it yourself as you show in your example.
from apivore.
Related Issues (20)
- Support multiple methods for loading a swagger document. HOT 3
- Apivore is not being released to RubyGems in a while HOT 3
- Responds matches should support reference objects HOT 1
- Unknown/unsupported Swagger version to validate against: HOT 2
- Errors on non-JSON responses
- Uninitialized constant Apivore HOT 2
- using apivore in seperate test framework
- Suggestion: remove .ruby-version
- signed user?
- OpenAPI 3.0
- You are setting a key that conflicts with a built-in method Apivore::Swagger#version HOT 1
- Providing query and path parameters? HOT 1
- Is it still maintained ? HOT 1
- Does not handle empty response case
- redistribution request HOT 1
- How to handle response? HOT 1
- apivore >= 1.6.1 is incompatible with rails >= 6.1.7.7
- Fails to pull swagger json HOT 1
- Is this project still being maintained? HOT 1
- Yaml support? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apivore.