-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
bugfix: Handle media type json a bit friendlier #1876
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
base: main
Are you sure you want to change the base?
Changes from all commits
b131235
ae22a8a
9ea31c3
b468936
d5986d1
21bbcae
139f9da
af3f0fb
797e043
baa670d
68f0cb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'base64' | ||
| require 'cucumber/formatter/backtrace_filter' | ||
| require 'json' | ||
|
|
||
| require 'cucumber/formatter/backtrace_filter' | ||
| require 'cucumber/query' | ||
|
|
||
| module Cucumber | ||
|
|
@@ -55,7 +56,7 @@ def initialize(config) | |
| config.on_event :undefined_parameter_type, &method(:on_undefined_parameter_type) | ||
| end | ||
|
|
||
| def attach(src, media_type, filename) | ||
| def attach(src, media_type, filename, streamed_file) | ||
| attachment_data = { | ||
| test_step_id: @current_test_step_id, | ||
| test_case_started_id: @current_test_case_started_id, | ||
|
|
@@ -64,13 +65,12 @@ def attach(src, media_type, filename) | |
| timestamp: time_to_timestamp(Time.now) | ||
| } | ||
|
|
||
| if media_type&.start_with?('text/') | ||
| attachment_data[:content_encoding] = Cucumber::Messages::AttachmentContentEncoding::IDENTITY | ||
| attachment_data[:body] = src | ||
| else | ||
| body = src.respond_to?(:read) ? src.read : src | ||
| if streamed_file | ||
| attachment_data[:content_encoding] = Cucumber::Messages::AttachmentContentEncoding::BASE64 | ||
| attachment_data[:body] = Base64.strict_encode64(body) | ||
| attachment_data[:body] = Base64.strict_encode64(src) | ||
| else | ||
| attachment_data[:content_encoding] = Cucumber::Messages::AttachmentContentEncoding::IDENTITY | ||
| attachment_data[:body] = src.is_a?(Hash) ? src.to_json : src | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the surrounding code, I don't think this implementation is correct. The attachment encoding is currently derived from the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In pseudo code:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into the CCK for this. And for the CCK it wants to present JSON bodies as un-encoded.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The determination of encoding isn't attached to the media type but rather the type of the source. This is matters because creating the the You can see this in the step definitions. For all https://github.com/cucumber/compatibility-kit/blob/b96ee7298b719b52cc8d9d0be3aa95b7dac20109/devkit/samples/attachments/attachments.ts#L5 While for the So I could do The first attachment would have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a breakout I need to do then. Because this was just a patch to ensure that someone passing in a hash can represent it as a nice setup (Which it currently doesn't do anywhere).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just thinking about it, we could invert this. And write something like
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the media type you'll be patching holes forever. For example what if you attach a hash with I do think the quick fix would be to only support But I don't know enough Ruby to help you here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't know what
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dug into this code, it's untouched in about 7+ years. I think I'll do the bit I thought of just now. I'll fix it in here so it's done once and for all. |
||
| end | ||
|
|
||
| message = Cucumber::Messages::Envelope.new(attachment: Cucumber::Messages::Attachment.new(**attachment_data)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the behaviour of the function doesn't seem to guarantee a valid message is produced in every scenario.
And asking the user of the API for this decision doesn't seem to be quite right either.
For example it look like it is possible to attach a byte stream with identity encoding by passing
streamed_file = true.Is there a way you can detect streamed files from their type or duck-type?
Then you can write: