abramenal / cypress-file-upload

File upload testing made easy
https://npm.im/cypress-file-upload
MIT License
496 stars 89 forks source link

[Bug] The payload is malformed after upload XML file #337

Closed manuel-neto-maitha closed 2 years ago

manuel-neto-maitha commented 2 years ago

Current behavior:

In my application, when uploading an XML file, manually, the request payload is formed correctly, and the upload is successful. The payload is similar with the json bellow, the application get the XML file and convert the file to one base64 string, and the string is invited in the payload.

{
  "id": 11,
  "xml": "77u/PG5mZVByb2M"
}

However, when the upload is doing by the automated test with cypress-file-upload, the request payload is malformed, and the upload fails, because the string 'data:application/xml;base64' is added in the request, glued with the file content in base64 string.

{
  "arrangementId": 11,
  "invoiceXml": "data:application/xml;base64,77u/PG5mZVByb2MgeG1sbnM9Imh0dHA6Ly93d3c"
}

Desired behavior:

I need the payload to be formed correctly, without 'data:application/xml;base64' in the init of the string.

Steps to reproduce: (app code and test code)

  1. My code to upload the XML is that:
    cy.get('input[type="file"]').attachFile(`My.xml`)
    cy.contains('Processar').click()

    Versions

    Ubuntu 20.04 Chrome 95 Cypress 7.2.0

abramenal commented 2 years ago

Hi, thanks for submitting the issue!

Can you please try setting the encoding explicitly? I am not sure which one will work here, but you can probably try it out and let me know which one works. Later I can update implementation to support this out of the box.

cy.get('input[type="file"]').attachFile({ filePath: 'My.xml', encoding: 'base64' })

If base64 does not work, here is the list of supported encodings: https://github.com/abramenal/cypress-file-upload/blob/v5.0.8/lib/file/constants.js#L1. Note that you should use object value, not the key.

Looking forward your feedback!

manuel-neto-maitha commented 2 years ago

Helo @abramenal thanks for the feedback.

I tried what you suggested but it didn't work. It seems to me that the problem is not in the encoding. The web application endpoint converts the file to a base64 string after uploading correctly. The dot is the snippet "data:application/xml;base64," which should not be present in the string.

abramenal commented 2 years ago

It can also be that mimeType that is internally determined by the plugin somehow affects processing logic. Maybe you can try something with that?

eg

cy.get('input[type="file"]').attachFile({ filePath: 'My.xml', mimeType: 'application/xml' })

Or on the contrary, set it to text/plain so the converter should output it as is

manuel-neto-maitha commented 2 years ago

@abramenal with mimeType: 'text/plain' works good, was solved.

The request stay with snipet 'data:text/plain;base64' in the init, but with text/plain, the POST method, returns 200.

Thank you for responses

abramenal commented 2 years ago

Since this is specific to your setup, there is nothing I can do on plugin side. However feel free to reopen if you think there is something wrong.