filiphric / cypress-plugin-api

Cypress plugin to show your API information in the Cypress UI runner
ISC License
250 stars 33 forks source link

TypeError When API response type not json / Content-Type : text/html #123

Open fredysiswanto opened 1 year ago

fredysiswanto commented 1 year ago

Hi Filip, I'm getting a TypeError when trying to request api with a response non JSON type (Content-Type : text/html; charset=UTF-8). if I try use cy.request my code run normally. Let me know if you need anything else! thank you.

image image image

use version 2.1.1 image

samirf-stubben-edge commented 1 year ago

This seems to be the same issue reported on https://github.com/filiphric/cypress-plugin-api/issues/102 I'm facing it as well

iepoch commented 7 months ago

A work around if you go into the Cypress-plugin-api node modules there is a support.js file. While this is just something that will work until updated. It does work. You will see

: r 
  .replace(/&/g, '&')

To fix it do the follwing

: r.toString() 
  .replace(/&/g, '&')

image

The workaround will work and hopefully someone can fix it I couldn't find the error in the code just in the minified file.

iepoch commented 6 months ago

After much testing and playing with the tool the issue steams from there is a an ArrayBuffer returned. And PrismJs doesn't know how to handle the arraybuffer it only knows how to get the object and parse it into a JSON. Or take a string and make it into a JSON response.

I went through the code and here is what I did to get Prismjs to see the arraybuffer. NOTE: I am not that good at coding so someone who understands this more maybe able to tell what we can do to make it bit better on the response side but at least this is returning response body. Also this only displays the decodeText of the body not the entire response. As you would need to push this into a object that could be turned into a JSON Response. Currently isValidJSON is used while there is a isValidHtml but the valid html is always a arrayBuffer when using the following code: `

cy.fixture(fileName, 'binary')
.then(file => Cypress.Blob.binaryStringToBlob(file, 'text/html'))
.then(blob => {

  const httpOptions = {
    method: 'POST',
    url: `/html`,
    headers: {
      'content-type': 'multipart/form-data',
    },
    body: formData,
    failOnStatusCode: false,
  };

  cy.api(httpOptions).then(response => {
    const dec = new TextDecoder();
    response.body = dec.decode(response.body);
    expect(response.status, 'POST Response Status').to.equal(200);
    cy.log(response.body)
  });

});

` Tried a Simple fix below not elegant image

The Result does show in the response image