Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rtfx-glob opened this issue Mar 3, 2021 · 1 comment

Comments

@rtfx-glob
Copy link

Chai-http version: "4.3.0"

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

@keithamus keithamus transferred this issue from chaijs/chai Mar 3, 2021
@rtfx-glob
Copy link
Author

rtfx-glob commented Mar 3, 2021

I have edited the JS class on @types/superagent/index.d.ts and I've added this:

In the method interface Response extends NodeJS.ReadableStream

I have added this: send: any;

ant in the interface method interface SuperAgent<Req extends SuperAgentRequest> extends stream.Stream

I have added this: send(url: string, callback?: CallbackHandler): Req;

With that kind of implementation I have solved this isue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant