最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress request : empty array in body - Stack Overflow

programmeradmin1浏览0评论

I'm finding myself in some troubles while testing my API with Cypress. (I'm using version 2.1.0)

I am sending a request to my endpoint, and want to verify how it is reacting when I am sending an empty array as a parameter. The problem is that somehow, Cypress must be parsing the body I am giving him, and removing the empty array.

My code is the following :

cy.request({
    method: 'PUT',
    url,
    form: true,
    body: {
        name: 'Name',
        subjects: []
    }
})
.then((response) => {
    expect(response.body).to.have.property('subjects');
    const { subjects } = response.body;
    expect(subjects.length).to.eq(0);
});

// API receives only the parameter name, and no subjects

When I am sending an empty array of subjects, the endpoint will delete all the associated subjects, and return the object with an empty array of subjects. It is working as it should, and my software in use is working as it should.

When Cypress is sending this request, the endpoint does not receive the parameter subjects. Which is for me a very different thing : I should not touch the subjects in this case.

Is there a way to avoid this "rewriting" by Cypress and send the body as I write it ?

I'm finding myself in some troubles while testing my API with Cypress. (I'm using version 2.1.0)

I am sending a request to my endpoint, and want to verify how it is reacting when I am sending an empty array as a parameter. The problem is that somehow, Cypress must be parsing the body I am giving him, and removing the empty array.

My code is the following :

cy.request({
    method: 'PUT',
    url,
    form: true,
    body: {
        name: 'Name',
        subjects: []
    }
})
.then((response) => {
    expect(response.body).to.have.property('subjects');
    const { subjects } = response.body;
    expect(subjects.length).to.eq(0);
});

// API receives only the parameter name, and no subjects

When I am sending an empty array of subjects, the endpoint will delete all the associated subjects, and return the object with an empty array of subjects. It is working as it should, and my software in use is working as it should.

When Cypress is sending this request, the endpoint does not receive the parameter subjects. Which is for me a very different thing : I should not touch the subjects in this case.

Is there a way to avoid this "rewriting" by Cypress and send the body as I write it ?

Share Improve this question asked Aug 10, 2018 at 13:25 hellimachellimac 2911 gold badge8 silver badges15 bronze badges 3
  • Just as a quick sanity check, if you go into the network tab in Chrome after this test has run, does the request that is generated contain subjects: []? – Joshua Wade Commented Aug 10, 2018 at 13:29
  • No, that is exactly the problem, the content of the body sent is only name=Name – hellimac Commented Aug 10, 2018 at 14:10
  • 1 This sounds like something that could be considered a bug, or unexpected behavior at the very least. I would suggest submitting this as a bug on Github. – Joshua Wade Commented Aug 10, 2018 at 14:31
Add a ment  | 

1 Answer 1

Reset to default 4

The test works when setting form: false.

it.only('PUTs a request', () => {
  const url = 'http://localhost:3000/mythings/2'
  cy.request({
      method: 'PUT',
      url: url,
      form: false,
      body: {
        name: 'Name',
        subjects: []
      }
    })
    .then((response) => {
      expect(response.body).to.have.property('subjects');
      const {
        subjects
      } = response.body;
      expect(subjects.length).to.eq(0);
    });
})

I set up a local rest server with json-server to check out the behavior.

If I try to PUT a non-empty array with form: true

cy.request({
    method: 'PUT',
    url: url,
    form: true,
    body: {
      name: 'Name',
      subjects: ['x']
    }
  })

looking at db.json after the test has run, I see the item index migrating into the key,

"mythings": [
    {
        "name": "Name",
        "subjects[0]": "x",
        "id": 2
    }
],

so perhaps form means simple properties only.

Changing to form: false gives a proper array

{
  "mythings": [
    {
      "name": "Name",
      "subjects": ['x'],
      "id": 2
    }
  ],
}

which can then be emptied out by posting an empty array.

发布评论

评论列表(0)

  1. 暂无评论