My Request Body is so long. How do you handle it while doing API testing via Cypress - node.js

My request body for an endpoint is so long:
1st question: I read that we can use some request.body.js file for storing our request body and then call it where ever we need it. But unfortunately, I could not find any sample framework/tutorial to learn it.
2nd question: in my project the properties of the request body (especially names of properties) are not exactly matching with the response body that is gaven in the Swagger document. What can be the reason? What would be your approach?
I would appreciate it if you could help me to ridd off the question in the best possible way. Thank you!

It's quite straight forward, take a look at this login example:
cy.fixture('users.json').then((userdata) => {
cy.request({
method: 'POST',
url: <auth_url>,
form: true,
body: userdata
});
});
You can export this as cypress function and then have it available in all your test spec files.
users.json file in fixtures folder looks like this:
{
"username": "...",
"password": "..."
}
Hope that answers the first question at least.

Related

Node native Fetch Upload Issue With Multer

I'm trying to migrate one of my systems from RequestJS to the new (currently experimental) fetch feature of Node 17.5+ but I'm having issues with the uploading of files, I'm hoping someone can assist.
At the receiving end I have Multer which is doing a simple upload.single('document') which always returns an empty object, so I'm fairly sure the way I'm sending the data is wrong.
In order to send the request, I'm doing the below (this is not the exact code, it's just to give an idea):
var requestOptions = {
headers: {'Content-Type': 'multipart/form-data'},
formData: {
document: {
value: _bufferData,
options: {
filename: _fileName,
contentType: _mimeType
}
}
}
};
Then I'm obviously sending the request using:
fetch('http://myurl.com/fileUpload', requestOptions).then(....);
I'm guessing new fetch API doesn't process the formData property the same way requestJS does. Does anyone know how I should be doing this?
Thanks.
Update: I've investigated further and the issue appears to be due to the content-disposition header missing the filename. I'm not sure how Multer creates this header but I assume its from the formdata, where the filename is clearly being passed in.

Is there a way to delete the response to an interaction (discord.js)

I recently started converting my commands to slash commands. I successfully did that, but I'm missing one functionality that I can't seem to find how to do. I want to delete the response I've given to an interaction (so the callback), and if possible after a certain amount of time (but that isn't required for my Discord bot).
For example, my callback is posted like this:
client.api.interactions(interaction.id, interaction.token).callback.post({
data: {
type: 4,
data: {
content: 'Hello World!'
}
}
})
And I want to delete that response immediately (or timed, but not in this example).
In the Discord docs (this page) is standing that it is possible, but I don't know how exactly to do that.
The specific code I already tried is this, but when the code is executed I'm getting the METHOD NOT ALLOWED error:
client.api.interactions(interaction.id, interaction.token).callback.delete()
I hope you can help me, thanks in advance.
Sorry for my bad English 😉
you can just fetch this :
https://discordapp.com/api/webhooks/{APP ID}/{Interaction Token}/messages/#original
Like That :
fetch("https://discordapp.com/api/webhooks/123456789012345678/${interaction.token}/messages/#original",
{
method: "DELETE"
}
);
https://discord.com/developers/docs/interactions/slash-commands#delete-original-interaction-response

How do I use https.request to perform a get request?

I've been trying to use https.request() to do a perform a get request, but I don't know how I would it. '
The current code I have is
const url="https://api.openweathermap.org/data/2.5/weather?q="+query+"&appid="+apiKey+"&units="+unit;
https.get(url,function(response){
However I would like to use https.request because I am trying to understand the concept, and the code I got so far is
var options={
method: "GET",
hostname:"api.openweathermap.org",
path: "/data/2.5/weather?q="+query+"&appid="+apiKey+"&units="+unit,
auth: "Eric:"+apikey
}
https.request(options,function(response){
I don't know why the http basic authentication did't work as the query string, I would like to learn about the differences. Thank you!

Microsoft Graph API PUT OneDrive/SharePoint

Trying to post a file to a subfolder of the Shared Documents folder. I thought I had the correct syntax down, but I keep getting StatusCode 400 Bad Request.
https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH:/filename.xlsx:/content
where "filename" is the actual filename.
I know a GET to the following lists the subfolder:
https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH
Request is going out as:
{Method: PUT, RequestUri: 'https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH:/', Version: 2.0, Content: <null>, Headers:
{
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub...BXS_cSg1CcZHj5Q
}}
Seems like it is dropping part of the request to me.
First, https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives and https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com/drives will return the same results, we prefer the second one.
I have never sucessful run the following API:
/drives/{drive-id}/items/{parent-id}:/{filename}:/content
But based on my test, the following API works well:
/v1.0/me/drive/root:/Test/Test1.txt:/content
or
/v1.0/me/drives/driveid/root:/Test/Test1.txt:/content

nodejs/mocha/nock - mocking an entire html response

How can I mock an entire HTML body response for my tests?
I'm using nodejs/mocha/nock.
With nock I can mock JSON responds just fine, for example:
nock('http://myapp.iriscouch.com')
.get('/users/1')
.reply(200, {_id: "123ABC", _rev: "946B7D1C", username: 'pgte'});
I used curl -o to fetch the html I want for the mock, so I have it already in a file - but I don't see how can I pass an HTML file to nock (or something else).
Thanks.
First fetch the HTML content of your test file and put it in a string (using fs.readFile for example)
after that you can do:
nock('http://myapp.iriscouch.com').
get('/users/1').
reply(200, yourFileContent);
This is what worked out for me in the past :)
If you'd like, you can specify the content type explicitly, since you specify the body as a string this will effectively let you mock any non-binary response easily:
nock('http://myapp.iriscouch.com').
get('/users/1').
reply(200, yourFileContent, {'content-type': 'text/html'});
If you want a more general approach, I've asked a more general question about a similar issue and got some interesting responses.

Resources