Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Dredd testing of APIary calls returning an image / PNG #87

Closed
imp-partner-test opened this issue Aug 4, 2014 · 16 comments · Fixed by #1094
Closed

Dredd testing of APIary calls returning an image / PNG #87

imp-partner-test opened this issue Aug 4, 2014 · 16 comments · Fixed by #1094

Comments

@imp-partner-test
Copy link

Dredd tests of APIary calls returning an image / PNG fail. Is it possible to check not the content of the returned image but just that there is an image not null as response?
Here is the output of my test:

[31mfail [39m: GET /imp-endpoint/api/V1/Resources?left_lower_lat=52.482780222078205&left_lower_lng=13.3154296875&right_upper_lng=13.359375&right_upper_lat=52.50953477032729&width=250&heigth=250 duration: 1176ms
[31mfail [39m: body: Real and expected data does not match.
[32mrequest [39m:
body:
headers:
accept: image/png
User-Agent: Dredd/0.3.7 (Linux 3.5.0-52-generic; ia32)
Authorization: Basic ZG1wOmRtcEAxMjM0
uri: /imp-endpoint/api/V1/Resources?left_lower_lat=52.482780222078205&left_lower_lng=13.3154296875&right_upper_lng=13.359375&right_upper_lat=52.50953477032729&width=250&heigth=250
method: GET

[31mexpected [39m:
headers:
content-type: image/png
body:
�PNG
statusCode: 200

[31mactual [39m:
statusCode: 200
headers:
x-powered-by: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)
server: GlassFish Server Open Source Edition 3.1.2.2
access-control-allow-origin: *
access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
content-type: image/png
content-length: 26572
date: Tue, 15 Jul 2014 09:00:26 GMT
body:
�PNG
IHDR � ?1g�IDATx^��

http://stackoverflow.com/questions/24824867/dredd-testing-of-apiary-calls-returning-an-image-png

@honzajavorek
Copy link
Contributor

Related: #52

@Almad
Copy link

Almad commented Aug 22, 2014

Thanks for the report. We plan to actually support binary files properly in the blueprint, but until it happens, it's true that we should provide a way for the api to pass the validations

@jaydiablo
Copy link

Has there been any updates with regards to this?

The workaround mentioned in #52 doesn't seem to work any longer, but I haven't seen any mention of binary responses being supported in the API Blueprint format either.

Am I missing something, or is this still not supported?

Thanks!

@jaydiablo
Copy link

Well, another workaround, which is working for me, is to modify the transaction in a preValidation hook:

hooks.beforeValidation("Machines > Machines collection > Get Machines", function (transaction) {
    transaction.real.body = '';
});

Probably not ideal, but only solution I've been able to come up with so far.

@honzajavorek
Copy link
Contributor

@jaydiablo I think that would be a preferred workaround as of now. Not sure how the original workaround scenario is handled now, but I'd say hooks are the way to go and should work even in the future.

Regarding out of the box support for binary files, this all boils down to apiaryio/api-blueprint#20, i.e. support for external assets in the API Blueprint language. Feel free to subscribe to the issue or contribute with ideas/opinions.

API Blueprint has a public roadmap and at the time of writing this post, the "External Assets" is number 6 top priority.

@leompeters
Copy link

+1 to @jaydiablo with Content-Length header > 1

@leompeters
Copy link

As a workaround, you can use:

hooks.beforeValidation('User > Avatar > Get Mine', function (transaction) {
  transaction.expected.headers['content-length'] = 9999;
  transaction.real.body = '';
});

Or even, set content length header on the blueprint, like this:

+ Response 200 (image/png)

    + Headers

        Content-Length: 26572

Now, I am looking for an example to a POST request to send an image... :P

@markfink
Copy link

markfink commented Jul 4, 2016

I use the following DREDD version:

$dredd --version
dredd v1.1.0

@leomperes : I tried to set the content-length like you suggested as a workaround but the problem still shows up in DREDD 😞 I installed DREDD this morning - do I need to use a different version?

body: Real and expected data does not match.
...
body: 
�PNG

@honzajavorek
Copy link
Contributor

@markfink Hi, current version of Dredd, i.e. v1.1.0, should work with the workaround. Do hooks work for you the way they're documented in http://dredd.readthedocs.io/en/latest/hooks/ ?

@markfink
Copy link

markfink commented Jul 4, 2016

@honzajavorek the hook workaround works fine but I can't fix this by providing content-length or other image/png related headers like @leomperes suggested in above post.

@honzajavorek
Copy link
Contributor

@markfink I don't have time right at this moment to test out the thing, but using before hook for setting relevant bodies/headers to the same values for real and expected should generally work, regardless the intention. Also, HTTP headers should be checked just for presence (i.e. whatever headers are in expected, need to be present also in real). If it doesn't work, then it smells like bug 👀

See http://dredd.readthedocs.io/en/latest/how-it-works/#automatic-expectations

@leompeters
Copy link

leompeters commented Jul 5, 2016

Hey @markfink, try to use the code below on your hook to make your expected empty body equals to the real:

switch (transaction.expected.headers['Content-Type']) {
  // case 'text/plain; charset=UTF-8':
  // case 'text/html; charset=utf-8':
  case 'image/png':
    transaction.expected.body =
      transaction.expected.body.replace(/^\s+|\s+$/g, '');
}

Source: http://dredd.readthedocs.io/en/latest/hooks-nodejs/#remove-trailing-newline-character-in-expected-plain-text-bodies

@netmilk
Copy link
Contributor

netmilk commented Jul 8, 2016

Just a note: You don't need to disable the entire test for the action, though. You can just disable validation for some particular response body and let Dredd validate only headers with this simple workaround: #510.

@Pablodotnet
Copy link

Is there a solution for this? I'm having the same problem, my expected response has no body because I have no way to set the image there, and in actual response body it comes with an image like this:
body:
����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 90
��C
�ޣ9�խ#�E�ʹ�h����g�?���s��g���X�{

And more and more digits... So how I can workaround this? What's the trick?

@honzajavorek
Copy link
Contributor

Reminder: work in #836 should be considered once we're working on binary files support.

@honzajavorek
Copy link
Contributor

While this was resolved for a while since the moment we fixed working with empty responses, in #1094 this is going to get proper tests, examples, and docs. Thank you all in participating here! It was really helpful.

honzajavorek added a commit that referenced this issue Jul 25, 2018
Uses Base64 encoding as the serialization, which allows also non-JS
hooks to set request/response bodies to a binary content.

Close #617
Close #87
Close #836
honzajavorek added a commit that referenced this issue Jul 25, 2018
Uses Base64 encoding as the serialization, which allows also non-JS
hooks to set request/response bodies to a binary content.

Close #617
Close #87
Close #836
honzajavorek added a commit that referenced this issue Jul 26, 2018
Uses Base64 encoding as the serialization, which allows also non-JS
hooks to set request/response bodies to a binary content.

Close #617
Close #87
Close #836
artem-zakharchenko pushed a commit that referenced this issue Oct 9, 2019
…-examples-minim

Rewrite detectTransactionExamples to minim
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants