XHR is not being logged when request is being made using fetchJson, NextJS - xmlhttprequest

We are using the NextJS framework in which a request is being made using 'fetchJson'. But, the issue is that the XHR request is not being logged by cypress.
Below is the logs displayed in the 'Network' - the address request is being made and it received 200. The logic in the programming is that when I enter Pincode that matches length 6, the fetchJson method is being called which makes the request.
Here is my code for my test case:
it("Validate Error message for PinCode ",function(){
cy.server({
method: "POST"
});
cy.route("POST","/api/address").as("mypin");
cy.get("form").within(()=>{
cy.get("[name='clientStreetNo']").type("Street");
cy.get("[name='clientStreetName']").type("StreetName");
cy.get("[name='clientArea']").type("are");
cy.get("[name='clientPinCode']").type("500082");
Here is a screenshot of test execution; it shows address request being made, in the network tab.
I have tried multiple ways to make cypress log the xhr request but it done;t log. How can i make the request log?

Related

In a vue3 vite project, use axios post method to server, but the browser console netwrk show that it is a get method

enter image description here
I use thi axios request to post my server
enter image description here
From the picture, you can see that I have set the router to response post request.
When I send a request, the server response well, so that we can sure it is a post request.
However, when I use the browser console network to check this request, it shows that it is a get request.
enter image description here
BTW,I config a proxy server in vite.config.js to solve the CORS, the request is send by the proxy server to my express server. I dont know whether it matter the problem.
Thanks for any response from this post.
For supplement, this is my request function which was encapsulated to create axios.
enter image description here

How to check HTTP status code in Apache configuration

Is it possible to check Http status code in Apache configuration as %{REQUEST_STATUS} for instance?
There is no such thing as a "request status", and no way for a server to interact with a browser in the middle of serving an error message.
HTTP is not an interactive protocol; the browser sends a request, the server sends a response, and that's it. So if the browser sends a request, and the application crashes, the server can send a response with 500 and the error details, or a response with 401 requesting the user to log in. Either way, that's the end of the conversation.
When it receives a 401 response, the browser can't say "here's the login details, carry on with the current request", it has to make a new request. It can make an identical request, which might reproduce the error message; but the original error message is gone.
If the requirement is to show a different amount of detail to different users, you need some notion of optional authentication, so that the server can decide immediately whether to include the error details or not, without an extra round-trip to the browser. I would suggest either:
Have a list of IP addresses which the application can check against; if the IP address of the request is in the list, include the error details.
Have a custom login system where you can authenticate and set a "session cookie" in the browser. If the user has an active session cookie, include the error details.

Writing tests with http request without response

I need to prepare a Java test (citrus framework) which initial step is sending a http request. Unfortunately my app under tests does not reply to this http request with anything while my testing framework expects to have a response and generates an error otherwise. The best way to deal with such a situation which came to my mind is to use some kind of a proxy between my testing framework and actual application which will forward the test request to the actual application and reply back to the testing framework with OK status not waiting for the response from app.
Does it make a sense? How could I prepare such a proxy assuming that my tests are to be running with maven invocation?
I see following options:
Fire and forget: send the Http request (using the fork mode on the send operation in Citrus) and do not care for the response at all. Just leave out the receive message action to ignore the response in Citrus.
Expect the timeout: Send the Http request and use the receive timeout action to verify that the client does not receive a response in the given time
Assert/catch the timeout exception: Use the assert or catch action in Citrus to handle the timeout exception when sending the http request
Personally I would go for the option #2 where you send the Http request and verify that there is no response for a given amount of time. This makes sure that the actual behavior of your application to not send any response does not change over time.

How to check Hubot log output in test script

I'm writing a test for my Hubot (which acts as a Slack bot). Triggered by certain Slack messages, the bot sends an HTTP POST request to a separate Rails app. How can I check in my test script that the HTTP request has been sent? My guess was that I should check the contents of the robot.logger (please let me know if there's a better way) - but if so, how can I access the log in the test?
Hubot script (basically, it informs the Rails app about a user who is leaving the office to take a break):
module.exports = (robot) ->
robot.respond /off to lunch/i, (res) ->
res.reply('Later alligator')
robot.logger.info "This user is going on lunch break: #{res.message.user.id}"
data = JSON.stringify({
slack_user_id: res.message.user.id
})
robot.http(process.env.RAILS_APP_URL + '/break')
.header('Content-Type', 'application/json')
.post(data) (err, resp, body) ->
if err
robot.logger.info "Encountered an error. #{err}"
res.reply('Sorry, there was an error recording your break time')
else
robot.logger.info 'Successfully sent HTTP POST request to Rails app'
Log output when I execute this script:
INFO This user is going on lunch break: [SLACK_USER_ID]
INFO Successfully sent HTTP POST request to Rails app
As I mentioned above, I'd like to check in my test script that the HTTP request was sent, by asserting that the log is going to include the message 'Successfully sent HTTP POST request to Rails app'. However, I don't know how to access the Hubot's log in my test. I thought it would have something to do with process.stdout because the bot logs to stdout, but I couldn't get it to work.
Test script:
Helper = require('hubot-test-helper')
helper = new Helper('../scripts/break-start.coffee')
request = require('request')
expect = require('chai').expect
nock = require('nock')
describe 'bot responds to user message and sends ', ->
beforeEach ->
# Set up the room before running the test.
#room = helper.createRoom()
# Set up a request interceptor.
nock(process.env.RAILS_APP_URL)
.post('/break', { slack_user_id: 'bob' })
.reply(200)
afterEach ->
# Tear down the room after the test to free up the listener.
#room.destroy()
context 'user sends lunch message', ->
beforeEach ->
#room.user.say('bob', '#hubot Off to lunch')
it 'responds to users who are off to lunch', ->
expect(#room.messages).to.eql [
['bob', '#hubot Off to lunch']
['hubot', '#bob Later alligator']
# I want to do something like this:
# expect(robot.log).include('Successfully sent HTTP POST request to Rails app')
Of course, I can see in the console log when I run the test that the HTTP request is being sent, but I'd also like to assert it so that the test fails if the request is not sent.
Log output when test is executed:
INFO This user is going on lunch break: bob
✓ responds to users who are off to lunch
INFO Successfully sent HTTP POST request to Rails app
Thank you for any help in advance.
I wouldn't advise writing tests depending on logs. The log is a side-effect of the program. If you change the log output, the tests will fail, even though the functionality is still correct.
Instead you should use a library to mock out and check if the http request was performed. Actually making the request would be a side-effect, and again shouldn't be done in your tests (what if you cause excessive load on an external service due to tests running?
You are already using the nock library to catch the request. It can also be used to check if the request was made (see the expectations docs from the nock repo).
Here is an example using the requestScope.done() from nock in your test.
it 'sends the break request to the rails server', ->
# capture the request to the rails app
railsRequest = nock(process.env.RAILS_APP_URL)
.post('/break', { slack_user_id: 'bob' })
.reply(200)
# make the request and wait for it to be completed
await #room.user.say('bob', '#hubot Off to lunch')
# check that the request was made
railsRequest.done()
I'm using await to ensure the function call which should make the request is completed before testing for it. If you're not familiar with await, you can move the check (railsRequest.done()) into a .then() promise handler on the #room.user.say(...) call instead.
Promise version:
Regarding your comment, here is the promisified version. You need to pass .then a function. If you pass it .then request.done() then the request.done() expectation will be executed immediately and its result will be passed as the promise callback. Either wrap it in another function (see below) or remove the parenthesis (.then request.done). But be careful with the second option. If your promise returns a value, this will be passed to the callback. As it's from a library, this may cause unexpected behaviour - that's why I would suggest the first option:
it 'sends the break request to the rails server', ->
# capture the request to the rails app
railsRequest = nock(process.env.RAILS_APP_URL)
.post('/break', { slack_user_id: 'bob' })
.reply(200)
# make the request and wait for it to be completed
#room.user.say('bob', '#hubot Off to lunch').then ->
# check that the request was made
railsRequest.done()

what does router.get and router.post do?

Im developing a node application with express.
In my users.js file in the routes folder, i have 3 routes /register, /authenticate, and /profile route. what does router.post do? and what does router.get do?
router.post() refers to POST requests and router.get() referes to GET request.
The difference between the two is that a GET request, is requesting data from a specified source and a POST request submits data to a specified resource to be processed.
For example when you load a sign up page, that is a GET request as you are requesting data from the server and when you submit that form it's a POST request as your inputted data will be processed and assorted into a database, etc.
router.post() and router.get() refer to POST and GET requests respectively. When your app is sent an HTTP POST request at the specified address, the post method is what will fire. The same for GET.