Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array unintentionally gets converted to object with supertest

the array bellow (query.conditions) gets converted to an object somehow, any idea why and how I can prevent it ?

The request:

          supertest(options.url)
            .get('/api/action')
            .expect(200)
            .query({
              conditions: 
                [
                  { 'user' : user._id },
                  { 'type' : 14 },
                  { 'what' : 4 },
                ]
            })

What the server gets:

{
  "conditions": {
    "user": "5592cc851f3febd016dae920",
    "type": "14",
    "what": "4"
  }
}
like image 483
coiso Avatar asked Sep 10 '25 01:09

coiso


1 Answers

There seem to be issues with query string serialization in superagent (which is used by supertest).

To work around that, you can use qs.stringify() on your data:

var qs = require('qs');

...

supertest(options.url)
  .get('/api/action')
  .expect(200)
  .query(qs.stringify({
    conditions: 
      [
        { 'user' : user._id },
        { 'type' : 14 },
        { 'what' : 4 },
      ]
  }))

(if at all possible, it might be more appropriate to POST JSON instead, though)

like image 95
robertklep Avatar answered Sep 12 '25 15:09

robertklep