DarkShield / daProxy

Proxy boxes
0 stars 0 forks source link

Finish Testing Proxy Internals #57

Closed ProZachJ closed 10 years ago

ProZachJ commented 10 years ago

Need tests for DB writing and Inspectors to be much more robust.

Needed unit tests

mattjay commented 10 years ago

Not terribly sure how to attack the last 2 on the list @ProZachJ. I think the cookie behavior is sufficiently tested now. I'm getting weird failing tests because the response codes are messed up while I'm on GoGo. I suspect its GoGo's fault on the plane here but will double check on the ground unless you beat me to it.

ProZachJ commented 10 years ago

proxy.spec.js is an end to end test that actually starts and send requests to the proxy. We need to get a better set of unit test to be able to test individual functions within our proxy code so that implementing blocking will be easier to tackle.

This callback is likely going to get longer, See https://github.com/DarkShield/daProxy/issues/61#issuecomment-43162511, We probably need to write it as a named function so that we can test it. As a general practice we should ban inline callbacks completely. They need to be named so we can test them.

https://github.com/DarkShield/daProxy/blob/darkbouncer/lib/proxyserver.js#L9 https://github.com/DarkShield/daProxy/blob/darkbouncer/lib/proxyserver.js#L35 https://github.com/DarkShield/daProxy/blob/darkbouncer/lib/proxyserver.js#L39

This override should probably be a named function as well:

https://github.com/DarkShield/daProxy/blob/darkbouncer/lib/proxyserver.js#L19-L26

To test functions that are internal (private) to other functions we can use rewire so that they don't have to be exported (made public).

https://github.com/jhnns/rewire

Here is vjoita's post where he tried to accomplish this capability in another way.

http://howtonode.org/testing-private-state-and-mocking-deps

ProZachJ commented 10 years ago

using rewire will mean our code would need to be structured as follows:

//Includes
var something = require('something');

//private functions
var myfunction = function (){};

//public function(s)
module.exports = function myPublicFunction (){
  //do some stuff
  myfunction();

  //or ansyc example
  setInterval(myfunction,100);

  /*to be more testable public functions that don't return 
  should return getters for important internals
  in general though injecting and returning arguments 
  should be preferred to operating on globals.*/
  var internal = {} || function(){};
  this.getInternal = function(){return internal};

  return this
 }
var public = require('thefileabove');
//call the function in your test
var called = public();
assert(called.internal !== undefined);
ProZachJ commented 10 years ago

In this way we can mock any of our private functions when testing the public functions.

var myPublicFunction = rewire("thefileabove");
myPublicFunction._set_({
  //mock the something module 
  something: function(){},
  //mock myfunction
  myfunction: function(){}
});

Since rewire returns a unique instance every-time you call it this can easily occur anywhere in jasmine tests where we find it useful without worrying about module collisions.

We can also test the private functions:

var myPublicFunction = rewire("thefileabove");
var myfunction = myPublicFunction._get_("myfunction");
ProZachJ commented 10 years ago

This will help too so we don't have to manually mock out requests and responses all the time:

https://www.npmjs.org/package/mocks

mattjay commented 10 years ago

@ProZachJ I think this is done? Not sure if all that we did covers the last 2 checkboxes. Should just close this and create a new issue for the one last function we haven't tested.

ProZachJ commented 10 years ago

Agreed we need an issue for a more permanent set of of targets for our e2e tests.