we have small wrapper client logging framework to enable javascript client logging.We have exposed different function like LogError,LogWarn,LogInfo in Javascript.
JavaScriptLogger.LogError(exception)
{
postAjax(exception);
WriteConsoleLog(exception)
}
When ever user logs a error using JavaScriptLogger.LogError(exception),two things get executed:
1. postAjax(exception) : An ajax post is made to specified server URL.
2. WriteConsoleLog(exception) : Writes an exception Console.Error/Console.Warn/Console.Log,depending upon errortype passed by user.
Now I wanna have a unit test cases for LogError,LogWarn,LogInfo function using Qunit framework.
Could somebody provide me a suggestion how to start and how to go about.
Regards,
SCP
Well, first off, I hope you understand that someone could flood your server logs this way, and hopefully you have something in place to prevent this.
Second, you would want to mock that Ajax call so that you aren't relying on your server to work (eliminating one source of potential errors). You could use something like Mockjax for that.
After that, you would want to probably run all of those tests in an asyncTest call (versus a simple test) since the Ajax call is still supposed to be asynchronous.
http://api.qunitjs.com/asyncTest/
(PS Sorry if my formatting is bad, typing this on my phone.)
Related
So I am not sure if this is possible or if there is a better way to perform this task, but I am attempting to create a single ajax connection to a server-side script that basically relays function calls to the page by constantly listening to a named pipe. So, for example, I could do something like:
# echo "foo(param1,param2)" > /path/to/pipe/gui
which would pass the 'foo' function to the page to be executed. The ajax connection needs to be permanent basically (controlled via timeout?) or by using an occasional 'ping' to keep it open.
Currently I have a basic test, but I am not sure how to process each received call in ajax. I am using the readyState==3 to simple call an alert() with each passed string from the named pipe, but it seems to always return everything submitted. So if I have done:
# echo 'hello' > /path/to/pipe/gui
It will show an alert popup with 'hello' in it. If I then do:
# echo 'world' > /path/to/pipe/gui
It will then show an alert popup with:
hello
world
Again, I am not sure if there is a better way to approach this or even if there is some library already built to handle something like this. Any help would be appreciated!
UPDATE:
The comments say to use websockets and socket.io (which I think uses websockets), but I don't want to add a bunch of servers. Any solutions need to keep standard webbrowsers and web servers.
I am reading a file continuously after a some time as
setInterval(function(){
$.getJSON("json/someFile.json", function(data){
// Some code
});
}, 5000);
I am reading this file continuously after a delay as it is getting updated in other part of the code. I want to avoid using setInterval().
Is there any way, by which I will be able to know that the file is updated and read it only when it is updated.
Firstly, setInterval is a native JavaScript method. It does not come from jQuery. Second what you've done is called polling. Meaning that you request some information periodically in order to keep it up to date. The alternative is using a WebSockets. Websockets are a two way connection between the client and the server, which can both push and receive messages. This way, you can send a socket message to the client whenever the file is updated in the backend.
I'm assuming you're talking about client side code. Then no: there is no way to "watch" a json file like you could have a file watcher in "regular" applications. You need either:
Interval-based checking as you're doing now. However, as suggested in comments by #George, you might be better off if you use setTimeout and only re-fire the Ajax request in specific situations (e.g. on success, perhaps not on failures); With your current approach the function may run on the interval, but if it takes longer than the interval timing to respond you get a build-up of requests;
Websockets (potentially with fallback to something like long-polling), perhaps using another library for that + the server-side part of this solution;
No other way I'm afraid.
As a footnote, this hasn't got much to do with jQuery. First, the setInterval is not of jQuery but a regular window function, and second the problem of "watching" a file isn't specific to how you're doing the Ajax call (you're using jQuery, but you could use another lib for it too).
I'm running a NodeJS server which is sending notifications to the clients when somebody does something, for example, when a client deletes a row from a grid, Socket.io informs the rest of the clients that a row got deleted.
In that example, I could add something like actionType: rowdeleted to the socket.io message and then just detect the actionType on the client side and refresh the grid. Anyways, the problem is that there can be infinite number of actions (and new ones can be added), so I can't code a function for each action type on the client side.
Then I thought maybe I can send some code via socket.io and make the client run it, but I'm not sure if that is the best way for doing what I want. Also, how are the clients going to run that code? Via eval?
I'm open to any suggestion :)
Have you considered something similar, but not as eval. You clearly must have the code to execute somewhere, be it on the server side. Why not create a way to let the client know what script/code/action to get and execute it.
I have used something similar out of a similar need. The action type referenced a script in a specific path on my server (/js/actions/ACTION.js). Upon getting the command to run the action, the client would check if it has the action, if not, it would go get the action. After that it would run the action on the script. RequireJS is good for this kind of thing. It will keep track of what actions you have and what actions you don't have. It will also make sure to get the action if it doesn't have it before it run some function that needs it.
eval is evil (c)
so I can't code a function for each action type on the client side.
there's no point emiting events from server if they wont be handled on the client(s)
have a client handle funcion for each type of event your server is emiting.
Otherwise bind on all events and handle then
I mean, there are some developer/other tools that let change variables in Javascript enviroment and so on. Consider I have an AJAX request. So could user, for instance, manipulate variables I send with AJAX request so that they see something they are not used to see? Or, for instance, manipulate value of <select> so that it is other, not like in options given. I hope you understand what I mean.
But how to implement AJAX in this case? Can client create his own request? So just send error back if request was not like it should be?
I send with AJAX request so that they see something they are not used to see?
YES!
You can't rely on client side scripting for security!
Check out:
Fiddler
Firebug
After your edit:
Can client create his own request?
Yes.
So just send error back if request was not like it should be?
Yes.
It's not "your" Javascript code. It's a bunch of characters you send to the client in the hopes that it will do something useful with it.
Javascript doesn't "work" by default. Everything that happens happens because the client did it. So yes, the client affects everything about the workings of your Javascript code.
I've created a Python script that monitors a logfile for changes (like tail -f) and displays it on a console. I would like to access the output of the Python script in a webbrowser. What would I need to create this? I was thinking about using Django and jQuery. Any tips or examples are greatly appreciated.
First create a python script that monitors the log file for changes. If you only need this for debugging - testing purposes, then it is an overkill to use Django or another web framework. It is very easy to implement Http Web server functionality using sockets. Whenever an Http GET request is coming, serve only the difference from the different request. In order to achieve this you need to store in memory the status of every request coming (e.g.number of last line in the file).
The jQuery part is actually quite easy. Set up a timer with setTimeout function. Something like this will do:
function doUpdate() {
$.ajax({type: "GET", url : tailServiceUrl,
success: function (data) {
if (data.length > 4)
{
// Data are assumed to be in HTML format
// Return something like <p/> in case of no updates
$("#logOutputDiv").append(data);
}
setTimeout("doUpdate()", 2000);
}});
}
setTimeout("doUpdate()", 2000);
You can also create callbacks for error and timeout to report a problem with the server.
I don't have any Python or Django experience but I'd assume you can make a system call like tail in Python and relay the details.
From there, I'd use a jQuery .ajax() call with a javascript setInterval() loop to your Python script and output the results to a div on the web page. Overall a pretty simple solution.
In this instance, you really wouldn't need to use an open tail -f system call because the nature of the JS setInterval() method, the Python script will be called over and over again until the JS clearInterval() method is called. You'll aggregate your script details in either Python or JS depending where you want to do the work. I'd suggestion Python since you'd have more robust features at your fingertips and you would send less data via the AJAX call. Theoretically, there probably shouldn't be too much logic needed in the jQuery code on the front end. Just display the data.
http://api.jquery.com/jQuery.ajax/
http://www.w3schools.com/jsref/met_win_setinterval.asp
Why don't you output the data to a HTML file? You could run a cron job to run your script which would in turn spurt out a HTML file which could be accesses from the browser.
The most voted answer works ok, but there is a more agnostic way to do this.
You can use https://github.com/mthenw/frontail
Just install it and invoke it with the files that you want to watch.
frontail /var/log/syslog /var/log/another_log
then visit http://127.0.0.1:9001
I hope this can help others.