-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Find a way how to work with binary files in hooks #617
Comments
@aledeg can you give more information about the http request you are trying to send?? What the type of file this is, content type of the req should be, etc. |
@ddelnano here is the information you wanted.
Here is the actual image used for the tests: I know that my api is working because I've tested it with behat and manually. It seems limited to dredd so far. |
@honzajavorek Any insight about that issue? |
@aledeg We're still missing a good canonical example for sending/testing binary data in Dredd using hooks. It's on my roadmap to prepare related examples, write tests for them so we're sure Dredd really works the way we recommend and put them to documentation. Related issues: #52 #87 and maybe even more. From what you shared I can't tell exactly whether it's PHP hook handler issue, Dredd issue or issue in your hooks. Please try to investigate further what exactly might be happening so we can take a closer look.
|
@honzajavorek I've made some more tests. Here is what I've got now.
dry-run: null
hookfiles:
- tests/Hook.php
language: vendor/bin/dredd-hooks-php
sandbox: false
server: 'bin/console server:start 127.0.0.1:8888 --force --quiet --env=test'
server-wait: 3
init: true
custom: {}
names: false
only: []
reporter:
- apiary
- dot
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
color: true
level: complete
timestamp: false
silent: false
path: []
hooks-worker-timeout: 50000
hooks-worker-connect-timeout: 15000
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 1000
hooks-worker-term-timeout: 50000
hooks-worker-term-retry: 500
hooks-worker-handler-host: localhost
hooks-worker-handler-port: 61321
blueprint: [test.apib]
endpoint: 'http://127.0.0.1:8888/app_dredd.php'
Hooks::before('Test picture', function (&$transaction) {
$transaction->request->body = file_get_contents('dredd/default.jpg');
});
### Add picture [POST /picture]
+ Request (image/*)
+ Response 204 For what I understand, it looks like dredd is expecting JSON in the request. But obviously, I didn't provide some since I am pushing an image. @ddelnano I think that |
@aledeg the hooks server always JSON encodes the payloads fyi. https://github.com/ddelnano/dredd-hooks-php/blob/master/src/Server.php#L111-L119 So because of that I would expect Dredd to still need to decode the JSON but instead it needs to understand how to treat that data. Is the point you highlighted where Dredd decodes the json initially from the hooks server? |
@ddelnano I wasn't aware of that. I am not sure I understand your question. Could you please explain what you mean? |
@aledeg what I mean is from the sample hookfile you showed where you add the picture contents to the request body the hook server json encodes your data in this key. Dredd is then going to take that data and decode the json, which I would assume is correct since I would expect dredd to need to decode the json to get the original image content back. So I am wondering if that area you identified in Dredd's source is the where Dredd decodes the json from the hooks server since my thinking would be it still needs to do that json decoding to get the original image content back. If that is the area where Dredd decodes it maybe the way the text is encoded in the original json is the problem. |
@ddelnano I see what you mean. |
This is an interesting situation I didn't realise previously. You modify the transaction in PHP hooks by setting body to a binary data. PHP hooks handler then encodes the transaction to JSON message to send it to Dredd over a socket. Dredd listens on socket, receives the JSON message, decodes this message, and then applies the changes to transaction according to what it received in the JSON message. This error seems to happen when Dredd is receiving the JSON message from socket. The error message you posted makes me think Dredd receives incomplete JSON message from the socket. How is that possible? From what I see in the Coffee code, Dredd recognises This is my hypothesis on what happens, but I didn't perform any experiments to confirm my thoughts. Also, if this is really the case, I don't think there's an easy solution. In general, I would say Dredd isn't really ready to do any image comparison. It's something we didn't really try much nor have working examples prepared for. Currently, the standard way dealing with binary responses would be to skip comparison of the body payload and let Dredd to judge only headers, parameters, status code, etc. That should be possible by hooks, but as I mentioned, needs some more tests, docs, and maybe fixes, since reportedly people are a bit grappling with it: #52 #87 ... |
Once #906 is done, we should add following tests and examples into docs, which work with binary responses:
|
Reminder: work in #836 should be considered once we're working on binary files support. |
I don't know if I should open that ticket here or here.
I am trying to change the request body with the content of a file with a hook, but it does not work.
I have the following error:
This is what I have in my hook:
Is this the good way of doing that?
Ping @ddelnano
The text was updated successfully, but these errors were encountered: