usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
25.39k stars 1.16k forks source link

Scripts methods 'req.body' and 'req.getBody()' returns string instead js object. #2939

Closed SPIRIT-00 closed 2 days ago

SPIRIT-00 commented 3 weeks ago

I have checked the following:

Describe the bug

If body defined as json in body tab. Methods 'req.body' and 'req.getBody()' can return body as string instead js object. That broke scripts and tests. I think it depends on body length.

I prepared two cases that logs in console what i mean. First case returns body as string < -- unexpected behaivor. Second case returns body as js object < -- all ok.

.bru file to reproduce the bug

Case 1: <-- unexpected behaivor.

meta {
  name: Returns as string
  type: http
  seq: 1
}

post {
  url: {{TestServerApi}}
  body: json
  auth: none
}

auth:basic {
  username: 
  password: 
}

auth:bearer {
  token: 
}

body:json {
  {
    "name": "test0029",
    "label": "Создание эскиза",
    "allowedFor": {
      "roles": ["Painter"],
      "users": [],
      "jobTitles": []
    },
    "allowedBy": {
      "roles": ["Manager"],
      "users": [],
      "jobTitles": []
    },
    "statuses": {
      "created": { "label": "Создана", "endStatus": false },
      "seted": { "label": "Назначена", "endStatus": false },
      "working": { "label": "В работе", "endStatus": false },
      "paused": { "label": "На паузе", "endStatus": false },
      "awaitInfo": { "label": "Уточнение ТЗ", "endStatus": false },
      "constructorApprove": { "label": "Согласование с конструктором", "endStatus": false },
      "approvedByConstructor": { "label": "Одобрено конструктором", "endStatus": false },
      "managerApprove": { "label": "Согласование с менеджером", "endStatus": false },
      "clientApprove": { "label": "Согласование с клиентом", "endStatus": false },
      "done": { "label": "Завершена", "endStatus": true },
      "declinedByClient": { "label": "Клиент отказался", "endStatus": true },
      "sketchVariantDone": { "label": "Вариант эскиза завершён", "endStatus": true },
      "cancelled": { "label": "Отменена", "endStatus": true }
    },
    "taskConsist": {}
  }
}

script:pre-request {
  let awaitBody = req.body ? typeof req.body == 'string' ? JSON.parse(req.body) : req.body : null;
  console.log(req.body)
  console.log(awaitBody)
}

Case 2: < -- all ok

meta {
  name: Returns as object
  type: http
  seq: 2
}

post {
  url: {{TestServerApi}}
  body: json
  auth: none
}

auth:basic {
  username: 
  password: 
}

auth:bearer {
  token: 
}

body:json {
  {
    "name": "sketchApproveConstructor",
    "label": "Согласование эскиза конструктором",
    "allowedFor": {
      "roles": ["Constructor", "MainConstructor"],
      "users": [],
      "jobTitles": []
    },
    "allowedBy": {
      "roles": [],
      "users": [],
      "jobTitles": []
    },
    "statuses": {
      "seted": { "label": "Назначена", "endStatus": false },
      "approved": { "label": "Согласовано", "endStatus": true },
      "notApproved": { "label": "Не согласовано", "endStatus": true }
    },
    "taskConsist": {}
  }
}

script:pre-request {
  let awaitBody = req.body ? typeof req.body == 'string' ? JSON.parse(req.body) : req.body : null;
  console.log(req.body)
  console.log(awaitBody)
}

Screenshots/Live demo link

Case 1: <-- unexpected behaivor. изображение Case 2: < -- all ok изображение

SPIRIT-00 commented 3 weeks ago

Info from discord:

изображение изображение

helloanoop commented 2 days ago

As per discussion on Discord with @SPIRIT-00 , This is no longer reproducible. Hence closing this issue.