Mocking response NightWatch + Selenium NodeJS - node.js

Trying to mock a response on my test (there is no mock server since i only have to mock twice)
Im using NightWatch over selenium in reactJS
I need to mock a post and a get request.
the post request is for a known static URL
but the GET one is for a url with a random query inside it like: ?id=example
i managed to mock the post one with NightWatch command .mockNetworkResponse() once, but now its not working for some reason, and the GET one is a bit more complicated since in the url there is a query passed like
?id=example which is random.
For some reason the .mockNetworkResponse() is not able to capture it. Ive tired using nock but i cant seem to figure out how to make it work i think its not even performing the nock action when i call for the request
//pre defined command to perform the search that triggers the post request
browser
.searchSpesific('jfk', 'lax',true);
//the command that used to work but now dosn't
browser
.mockNetworkResponse("https://example.com/search",{
status: 200,
headers:{
'Content-Type': 'UTF-8'
},
body: 'intercepted'
})
the GET request URL looks like this:
https://example.com/search?id=randomIdString

Related

Only return the response status from a axios GET request?

I`m writing tests in playwright where I check if all the links on my web page are valid.
I'm using Axios to get the responses and because I have a lot of links I want to expedite the tests as much as possible, so I want to receive only the status response code from Axios without the response data, is this in any way possible with Axios or maybe to use another library to achieve this?
I used axios.head() request, which only returns the response header and not the data, but had to add to use:
axios options: decompress: false// default true
decompress indicates whether or not the response body should be
decompressed automatically. If set to true will also remove the
'content-encoding' header from the responses objects of all
decompressed responses

Webrtc connectivity test - unknown API key

When we try to run the UDP and Bandwidth tests from the given sample code, it tries to hit a Google API service URL (using ajax call) which looks like "https://networktraversal.googleapis.com/v1alpha/iceconfig?key=".
This service URL needs to be appended with an API_KEY before making an ajax call.
When I tried to run this example https://test.webrtc.org/
It is appending API_KEY to the Base_URL and making an ajax call (POST call) to get some data.
The tests are initiated based on the response data.
Base_URL: "https://networktraversal.googleapis.com/v1alpha/iceconfig?key="
API_KEY: "AIzaSyDX4ctY_VWUm7lDdO6i_-bx7J-CDkxS16I"
Service URL looks like this: "https://networktraversal.googleapis.com/v1alpha/iceconfig?key=AIzaSyDX4ctY_VWUm7lDdO6i_-bx7J-CDkxS16I"
This service gives the response.
All the tests like UDP, Bandwith etc are performed based on the "response.iceServers" Array.
When I tried to hit this service using the same API_KEY from the example, it is throwing an error "Invalid API_KEY from the request URL." So here to access this data we need to have an API_KEY.
Can someone please tell me what API_KEY is this and which one should I use??
Regards.

encode request.query in express

I wrote a simple API to return requset.query in response.
Sample Url:
http://localhost:8082/redirect?requesttype=click&id=79992&redirectto=http://localhost:8081/redirect?name=john&id=123
This returns following response,
but on passing encoded Url things are working as expected(screenshot attached),
http://localhost:8082/redirect?requesttype=click&id=79992&redirectto=http%3A%2F%2Flocalhost%3A8081%2Fredirect%3Fname%3Djohn%26id%3D123
But our API is getting utilized by various customers, is there any method in express.js where I can automatically encode the Url and serve the request as expected.

Error handling in express based on how a resource is called?

I am using express.
I am handling a Get request like below.
router.get('/user', user.getSessionInfo);
On client side I am using Angularjs.
I want to customize my error handling in nodejs based upon how a resource is being called, i.e. when /user is called via an $http call or directly from browser url so that I can return(if an error happens) either a json error obj or an error page(html).
I tried using req.xhr to decide whether the call is an ajax or not but I am getting false in both cases(not getting X-Requested-With header via $http.get)
So to achieve my functionality Can I depend upon
req.headers['accept'];
as from ajax call it will be
"application/json, text/plain, */*"
and when called from the browser url bar it will be
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
Is my way the best or there is a better way of handling it?
There is another way,use query param(dot extension) to decide resource type:
GET /api/user.json
GET /api/user.html
GET /api/user.xml
and it seems than this solution is more complicated to achieve in Express.
req.xhr was removed in angular. Read here why removed and how to get it back
https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d

Response body in Tobi.js?

I'm using https://github.com/LearnBoost/tobi, a browser simulator, to test an external web app. I'd like to be able to see the response body returned in the callback.
I understand the normal way node.js server-side apps show body is via res.on('data'), but my understanding is that inside the toby browser callback the response is now complete. Yet I can't find a res.body, res.data, or anything similar. Nor can I find docs on the topic!
function(error, response, $){
// Headers are there
console.log(response.headers;
// Horrible hack to show body via jquery as response.body and response.data are undefined
console.log($('body').html());
}
Per the above, I can see the document data via jQuery, so it's there. I just can't find it inside response...
I think your jQuery usage is the intended way to use tobi. It consumes the response and gives you a $ to manipulate or examine it.

Resources