Skip to content
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

fix(browser): allow save source as raw content #121

Merged
merged 1 commit into from
Feb 21, 2023

Conversation

welcoMattic
Copy link
Contributor

@welcoMattic welcoMattic commented Feb 20, 2023

Fix: #120

We can now save source as raw, and make assertions on file like zip or any other binary format.

NOTE: we could improve assertions on the zip file itself by requiring ext-zip and use \ZipArchive class to open zip.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misunderstood the issue (didn't realize you actually wanted to save a raw zip file).

As an alternative to the $raw parameter, what do you think about only adding the meta-information if content type is text/*?

This way the saved file would always be valid to open in your OS's explorer. docx/xlsx/whatever

This would no longer add this meta-information to json so I'm not sure about this one... (@nikophil, wdyt?)

src/Browser/KernelBrowser.php Outdated Show resolved Hide resolved
@welcoMattic
Copy link
Contributor Author

@kbond I agree, we can base the raw process on the content type. For json, as the content type is application/json the metadata should be added, I'll write a test for it

@kbond
Copy link
Member

kbond commented Feb 20, 2023

Sounds good! For the $raw parameter, you can either remove it entirely (can always add later) or make it ?bool $raw = null. When null, we make it true if content-type is text/* or application/json.

@nikophil
Copy link
Member

not sure about the parameter if we implement the suggestion based on content type: if we have something like application/zip we'd never want the metadata to be added

@welcoMattic welcoMattic force-pushed the fix-save-source branch 3 times, most recently from 06864f9 to 5ddaf68 Compare February 21, 2023 09:15
@welcoMattic
Copy link
Contributor Author

I've addressed all your comments @kbond @nikophil 😉

Copy link
Member

@nikophil nikophil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good for me, except the small comment I suggested

src/Browser/Session.php Outdated Show resolved Hide resolved
@welcoMattic
Copy link
Contributor Author

I rewrite the code to put the metadata prepended to the content under a condition via a global flag named BROWSER_SOURCE_DEBUG (set to false by default).

The only exception to this, is the call of source() method from dump() method, here we force the metadata to be prepended.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should forgo the global env variable for now.

Correct me if I'm wrong but when using the PHPUnit extension, it will no longer output the meta info in the generated files. I believe this is useful to help debug what path/headers caused the problem.

I don't believe there is any harm in always having the metadata in text/* files. My only question was for .json files as this doesn't output valid json... I am thinking this is ok as I believe most people would open .json files in a text editor/ide.

To summarize, I think we should always add metadata for text/* and application/json content types. All others should be the raw values.

src/Browser/KernelBrowser.php Show resolved Hide resolved
@welcoMattic
Copy link
Contributor Author

I'm actually writing tests, and I expect to be able to compare files to saved sources from Browser (zip, html, jpg, json). As I need to compare saved sources to static files I have in my tests folder, I would like to disable completely the metadata prepended to the sources.

But, in another hand, I would like to enable those if I need to debug something.

@nikophil WDYT about this general flag?

@kbond
Copy link
Member

kbond commented Feb 21, 2023

I would like to disable completely the metadata prepended to the sources.

Got it, didn't realize you had a real use-case.

I'm sort of still thinking a ?bool $raw = null parameter opposed to a global config. Would that work for your use-case?

@kbond
Copy link
Member

kbond commented Feb 21, 2023

not sure about the parameter if we implement the suggestion based on content type: if we have something like application/zip we'd never want the metadata to be added

Agreed. What I am suggesting is to, by default, only add metadata to html,xml,plain-text/json content types.

@welcoMattic
Copy link
Contributor Author

To make a decision, I think we must retrieve the original reason of the metadata presence? Was there a specific reason?

@kbond
Copy link
Member

kbond commented Feb 21, 2023

It's purpose is to make debugging x/html/json errors easier when:

  1. Running $browser->dump()/dd() (shows the metadata in the console)
  2. When using the PHPUnit extension to output files after a failure/error. You can view these files in your console/explorer or make available to view in your CI (github actions)

(2) above is I suppose the only hard reason to actually write the files with the metadata included. If you are calling ->saveSource() yourself, you likely do not want the metadata.

@welcoMattic
Copy link
Contributor Author

As it's for failure/error cases, even in CI, I think the BROWSER_SOURCE_DEBUG env var is quite appropriate, no?

Set to false most of the time, and toggle it to true in case of failure/error, if you need to debug some saved source files?

@kbond
Copy link
Member

kbond commented Feb 21, 2023

Yes, that's a fair point. I still think any non-text/json content should never have metadata added as it corrupts the file.

But I'm fine rolling with this as is for now as, by default, this won't happen anymore.

@welcoMattic
Copy link
Contributor Author

I see your point, so I can also add a condition (in addition to $sourceDebug) to make a check on content-type, to never add metadata except for a fixed whitelist of content-type (which could grow up through time), is it ok?

@kbond
Copy link
Member

kbond commented Feb 21, 2023

Sure, we'll have to be loose with the check I think: str_contains('text/', $contentType) || str_contains('json', $contentType)

@welcoMattic
Copy link
Contributor Author

Finally, I've added 2 conditions to writing metadata : sourceDebug config flag (via env var) and checking response header to determine if is text content or not.

Beside, we decided to never prepend source content with metadata if PantherBrowser is used (as we can't check on response headers with Panther response).

@kbond kbond merged commit d64b773 into zenstruck:1.x Feb 21, 2023
@kbond
Copy link
Member

kbond commented Feb 21, 2023

Thanks @welcoMattic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Improve file saving
3 participants