Open ignaciolarranaga opened 7 years ago
Hi @ignaciolarranaga, first of all, thank you very much for the report! I was able to identify multiple problems.
application/json
are not supported (yet). That's why in the expected
section of the Dredd output, you can't even see the text/csv
content-type header. This would be a feature request.{"type":"file"}
in the response (yet). Dredd uses JSON Schema to validate the response and this is a Swagger extension to the spec. Linking relevant docs (also mentioned in the Data Types section). Since this is something specific to a certain format, I think Dredd shouldn't be the one dealing with it. The Swagger adapter should take care of the specifics and pass on something which looks like raw HTTP request-response pair and which Dredd already understands - cc @apiaryio/adt This would be a feature request.Can't validate real media type 'text/plain' against expected media type 'application/schema+json'
, which doesn't make any sense. Dredd should handle the situation more gracefully and provide you with the information something you use isn't supported and cannot be tested. This is a bug in Dredd's user interface.Just dropped by for a +1
produces/consumes other than application/json are not supported (yet)
This would be really useful to have, since even though most APIs are json, it's not uncommon to see something else at some endpoints (like gpx xml for example). I would think an early version of this feature would be easy to get going with plain text matching as a fallback.
I'll add a plus 1 as well.
What would be cool in the interim is if Dredd CAN'T validate against non application/json return bodies that it simply doesn't TRY TO.
While it would be great to have html, plain text and/or csv validation, it would still be useful if dredd could simply validate that it got a response with the expected content-type header and call that a pass for any content-type other than application/json. Right now I get failures for all my APIs.
Happy to provide an example API blueprint and dredd output if it would be helpful.
@jpjpjp Dredd can validate non-JSON payload when using API Blueprint. If the payload isn't JSON-like, the validation is done by comparing the payloads 1 to 1 as a plaintext.
@honzajavorek is there the possibility of yaml support for OpenAPI?
@MikeRalphson Which one do you mean?
I'm not familiar with Gavel, but 3. is what I mean. Yes, I believe the YAML representation should be converted to a JSON object and validated with JSON Schema.
@MikeRalphson Hmm. I think you're first to have such feature request. Would you be willing to file it on https://github.com/apiaryio/gavel.js/issues? Gavel.js is the validation engine Dredd uses under the hood. It is basically a tool, which takes an expected and real HTTP transactions and then provides a validation verdict.
Related: apiaryio/fury-adapter-swagger#143
As follow-up to https://github.com/apiaryio/dredd/issues/883#issuecomment-329151438:
I am having a soap API . I tried to validate the soap API having the content type : text/xml (POST method.) I have prepared a swagger file without providing the example request body in the file . I stored my request body which is in xml format in a file and passed the content to the request body using below code var requestBody = fs.readFileSync('RequestBody.xml','utf8'); transaction.body=requestBody; in hook file @before But when i execute it , it is throwing me the the exception "syntaxerror unexpected token in json at position 0 " Is it possible to validate a soap api in dredd?
One thing is API using XML content types in its payloads. That should be definitely testable by Dredd.
Another thing is SOAP API. While in theory SOAP should be testable by Dredd, because it's still just plain old HTTP, I think the nature of SOAP API will make it very difficult with Dredd for you and I would personally advise against it. There are many other tools for working with SOAP APIs and checking their correctness.
From the error you mention it seems Dredd is trying to validate the XML payload as JSON. Could you share the relevant part of your Swagger? If you want to skip the validation, check these docs.
@honzajavorek I havent passed the payload in the swagger . I have given an empty request body , haven't specified anything other than the basepath , content type and header for the request.
Actually my payload somewhat looks like below <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
@sajantha I tried to reproduce your issue, without success. Moreover, I think it's not related at all to the original issue. Following example is working for me correctly, npm test
passes.
app.js
const fs = require('fs');
const express = require('express');
const app = express()
app.get('/foo', (req, res) => {
res.header('content-type', 'application/xml');
res.send(`<xml>
<title>ugly SOAP XML</title>
<envelope>
<envelope>
<envelope>
<envelope>data</envelope>
</envelope>
</envelope>
</envelope>
</xml>
`);
});
app.listen(3000, () =>
console.log('Listening on http://localhost:3000')
)
hooks.js
const fs = require('fs');
const hooks = require('hooks');
hooks.before('/foo > GET > 200', (transaction, done) => {
transaction.expected.body = fs.readFileSync('soap.xml', { encoding: 'utf-8' });
done();
});
soap.xml
<xml>
<title>ugly SOAP XML</title>
<envelope>
<envelope>
<envelope>
<envelope>data</envelope>
</envelope>
</envelope>
</envelope>
</xml>
package.json
{
"name": "dredd-xml",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "dredd app.yaml http://localhost:3000 \"--server=node app.js\" --hookfiles=hooks.js"
},
"license": "ISC",
"devDependencies": {
"dredd": "^5.1.8"
}
}
app.yaml
swagger: "2.0"
info:
version: "1.0"
title: "app.js API"
schemes:
- http
produces:
- application/xml; charset=utf-8
paths:
/foo:
get:
responses:
200:
description: my SOAP response
As I mentioned, I wouldn't recommend testing SOAP API by Dredd. Even the example I posted tests the response as a plaintext, which means even change in the (insignificant) white space would break the build. That's probably not something you want to do. Since gavel.js does not support XML directly, you'd have to implement smarter comparison of the XML documents in the hooks yourself.
In case you want to continue discussing your topic, please file a separate issue. Thanks!
Note: The OpenAPI 2 adapter 0.21.0 introduces better support for file
in HTTP requests, but it still doesn't seem to work well for HTTP responses.
Describe your problem
On running the validation of an endpoint with the following spec:
I got this error:
What command line options do you use?
What's your
dredd --version
output?Does
dredd --level=debug
uncover something?Nop.