Skip to content

HTTP::Request#to_json reads file content if body is a File#12

Merged
kalinon merged 1 commit intospoved:masterfrom
cyangle:handle_io_body_in_to_json
Sep 8, 2022
Merged

HTTP::Request#to_json reads file content if body is a File#12
kalinon merged 1 commit intospoved:masterfrom
cyangle:handle_io_body_in_to_json

Conversation

@cyangle
Copy link
Contributor

@cyangle cyangle commented May 30, 2022

If the request body is a file, we want to include the file content in the json string.
Without this, File#to_s returns the object id which is random for each test run.

Another approach is to override File#to_s

@kalinon

The same test fails without the change:

$ crystal spec spec/vcr_spec.cr:30
F

Failures:

  1) VCR HTTP::Request#to_json returns a json string with IO body
     Failure/Error: request.to_json.should eq("{\"method\":\"POST\",\"host\":null,\"resource\":\"http://httpbin.org/post\",\"headers\":{\"Content-Type\":[\"text/plain\"]},\"body\":\"hello crystal\\n\",\"query_params\":{}}")

       Expected: "{\"method\":\"POST\",\"host\":null,\"resource\":\"http://httpbin.org/post\",\"headers\":{\"Content-Type\":[\"text/plain\"]},\"body\":\"hello crystal\\n\",\"query_params\":{}}"
            got: "{\"method\":\"POST\",\"host\":null,\"resource\":\"http://httpbin.org/post\",\"headers\":{\"Content-Type\":[\"text/plain\"]},\"body\":\"#<File:0x7fe3a4c5cee0>\",\"query_params\":{}}"

@kalinon kalinon merged commit 3614ba1 into spoved:master Sep 8, 2022
@cyangle cyangle deleted the handle_io_body_in_to_json branch September 25, 2022 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants