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

feat(rest): print curl #4396

Merged
merged 4 commits into from
Jun 19, 2024
Merged

feat(rest): print curl #4396

merged 4 commits into from
Jun 19, 2024

Conversation

kobenguyent
Copy link
Collaborator

@kobenguyent kobenguyent commented Jun 13, 2024

Motivation/Description of the PR

  • That's very high chance you need to share the request for debugging purpose, and curl would be the cheapest and fastest way for that purpose. Hence, printing the curl request in console logs would benefit us.
    › [CURL Request] curl --location --request POST https://httpbin.org/post -H "X-API-Key: apiKey-onRequest" -H "Content-Type: application/json" -d '{"_overheadLength":161,"_valueLength":0,"_valuesToMeasure":[{"fd":null,"path":"/Users/t/Desktop/projects/codeceptjs-rest-demo/src/fixtures/test_image.png","flags":"r","mode":438,"end":null,"bytesRead":0,"_readableState":{"state":6176,"highWaterMark":65536,"buffer":{"head":null,"tail":null,"length":0},"length":0,"pipes":[],"flowing":false,"errored":null,"defaultEncoding":"utf8","awaitDrainWriters":null,"decoder":null,"encoding":null},"_events":{},"_eventsCount":3}],"writable":false,"readable":true,"dataSize":0,"maxDataSize":2097152,"pauseStreams":true,"_released":false,"_streams":["----------------------------807986065015258526651649\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"test_image.png\"\r\nContent-Type: image/png\r\n\r\n",{"source":{"fd":null,"path":"/Users/t/Desktop/projects/codeceptjs-rest-demo/src/fixtures/test_image.png","flags":"r","mode":438,"end":null,"bytesRead":0,"_readableState":{"state":6176,"highWaterMark":65536,"buffer":{"head":null,"tail":null,"length":0},"length":0,"pipes":[],"flowing":false,"errored":null,"defaultEncoding":"utf8","awaitDrainWriters":null,"decoder":null,"encoding":null},"_events":{},"_eventsCount":3},"dataSize":0,"maxDataSize":null,"pauseStream":true,"_maxDataSizeExceeded":false,"_released":false,"_bufferedEvents":[{"0":"pause"}],"_events":{},"_eventsCount":1},null],"_currentStream":null,"_insideLoop":false,"_pendingNext":false,"_boundary":"--------------------------807986065015258526651649"}'

Applicable helpers:

  • REST

Type of change

  • 🚀 New functionality

Checklist:

  • Tests have been added
  • Documentation has been added (Run npm run docs)
  • Lint checking (Run npm run lint)
  • Local tests are passed (Run npm test)

@kobenguyent
Copy link
Collaborator Author

ready for testing: codeceptjs@3.6.4-beta.2

Copy link
Contributor

@DavertMik DavertMik left a comment

Choose a reason for hiding this comment

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

Looks really good to me
I checked alternative implementation at https://github.com/anthonygauthier/axios-curlirize/blob/master/src/lib/CurlHelper.js and I think your is better yet minimal.

However, I can assume it doesn't cover all cases.

Should we inform users somehow on that?

Or I have a better idea, ignore CURL output if request is not JSON. I don't think we need to fit for all cases

}

if (request.data) {
curl += `-d '${JSON.stringify(request.data)}'`;
Copy link
Contributor

Choose a reason for hiding this comment

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

What if data is not JSON? But encoded form?

Copy link
Collaborator Author

@kobenguyent kobenguyent Jun 17, 2024

Choose a reason for hiding this comment

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

that won't be a problem with encoded form, the curl will look like this.

    › [CURL Request] curl --location --request POST https://httpbin.org/post -H "X-API-Key: apiKey-onRequest" -H "Content-Type: application/json" -d '{"_overheadLength":161,"_valueLength":0,"_valuesToMeasure":[{"fd":null,"path":"/Users/t/Desktop/projects/codeceptjs-rest-demo/src/fixtures/test_image.png","flags":"r","mode":438,"end":null,"bytesRead":0,"_events":{},"_readableState":{"highWaterMark":65536,"buffer":[],"bufferIndex":0,"length":0,"pipes":[],"awaitDrainWriters":null},"_eventsCount":3}],"writable":false,"readable":true,"dataSize":0,"maxDataSize":2097152,"pauseStreams":true,"_released":false,"_streams":["----------------------------917245260724904416987426\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"test_image.png\"\r\nContent-Type: image/png\r\n\r\n",{"source":{"fd":null,"path":"/Users/t/Desktop/projects/codeceptjs-rest-demo/src/fixtures/test_image.png","flags":"r","mode":438,"end":null,"bytesRead":0,"_events":{},"_readableState":{"highWaterMark":65536,"buffer":[],"bufferIndex":0,"length":0,"pipes":[],"awaitDrainWriters":null},"_eventsCount":3},"dataSize":0,"maxDataSize":null,"pauseStream":true,"_maxDataSizeExceeded":false,"_released":false,"_bufferedEvents":[{"0":"pause"}],"_events":{},"_eventsCount":1},null],"_currentStream":null,"_insideLoop":false,"_pendingNext":false,"_boundary":"--------------------------917245260724904416987426"}'

@kobenguyent
Copy link
Collaborator Author

Looks really good to me I checked alternative implementation at https://github.com/anthonygauthier/axios-curlirize/blob/master/src/lib/CurlHelper.js and I think your is better yet minimal.

However, I can assume it doesn't cover all cases.

Should we inform users somehow on that?

Or I have a better idea, ignore CURL output if request is not JSON. I don't think we need to fit for all cases

updated as proposed!

@kobenguyent kobenguyent merged commit 0852fa9 into 3.x Jun 19, 2024
13 checks passed
@kobenguyent kobenguyent deleted the feat-print-curl branch June 19, 2024 07:36
@kobenguyent kobenguyent mentioned this pull request Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants