Chai-http [POST] does not allow to send an Array on bodyParameters (API Testing & Javascript)

201 Views Asked by At

I have found this kind of weird behavior in my test case.

Let's recreate the scenario

I do have 2 classes:

API.ts -> which refers to the class where I'm creating the requests.

API-op.ts -> which refers to the class where I'm validating through asserts, expects etc.

Context: There are some dependencies between the methods. I mean, If I want to publish I need first an ID, that ID becomes from a method called postCreateId. Once I have the response body, I'm using that ID to be passed on my postPublishArt method

So.

I have this method on API.ts

public postPublishArt(token: any, Id: any) {
    return chai.request(URLBASE)
        .post(URI)
        .set({ Authorization: "Bearer " + token })
        .send({
            id: Id
        })
}

So, I'm calling that method on API-op.ts like this:

public async postPublish() {
        var accessToken = undefined;
        return this.chaiClass.generateToken()
            .then(async (response) => {
                accessToken = response.body.AccessToken;
                return this.chaiArt.postCreateId(accessToken)
                    .then(async (response) => {
                        let id = response.body.id;
                        let array: Array<string> = [id];
                        return this.chaiArt.postPublishArt(accessToken, array)
                            .then(async (response) => {
                                expect(response).to.have.status(201);
                                return response;
                            }).catch(err => {
                                return console.log('Failure', err);
                            });
                    });
            });
    }

on Postman the bodyparameter needs to be like this:

{
      "ids": 
          [
"d2164919-730a-5fe2-924d-5c14cf36e38c"
]
      
}

Pay attention at square brackets because the body parameter requires that ids keys MUST BE array.

What is the error: Like I need just ONE id passed as an array, once I convert that variable and it is sent to the POST method. the POST method is showing up this error:

text:'{"errors":[{"code":"unexpected-exception","details":{"statusCode":500,"error":"invalid input syntax for type timestamp: \\"0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN\\""}}]}'

I think that issue is about chai-http when you send a POST with ARRAY but just passing one parameter.

Let me know if I'm wrong

1

There are 1 best solutions below

0
Blackwidow On

ANSWER: There is an issue that sugerAgent is not calling back the send method...

see below the issue reported and how it is solved: https://github.com/chaijs/chai-http/issues/290