Skip to content
Browse files

Adding unit tests for HTTP, with mocking

  • Loading branch information...
1 parent a3ad6c8 commit 3a5a263a862ede29d5dd09c7c9162a923f374c91 @bbyars committed Jun 3, 2011
Showing with 56 additions and 0 deletions.
  1. +3 −0 TODO.md
  2. +53 −0 server/test/unit/protocols/http/stubbing.test.js
View
3 TODO.md
@@ -3,6 +3,9 @@
* Remove string.format for something more javascript like
* Pass console.log around, allows using different logger
* Move the (path || '/') logic to repository
+* Fix run_tests to run protocol tests in nested directories
+ * Allow passing 'unit' on command line and have it run all unit tests
+* Add mocking to testExtensions with a way of viewing actual arguments
## Build
* Pretty up github release page
View
53 server/test/unit/protocols/http/stubbing.test.js
@@ -0,0 +1,53 @@
+'use strict';
+
+require('testExtensions');
+
+var testCase = require('nodeunit').testCase,
+ createRepository = require('repository').create,
+ stubs = require('protocols/http/stubbing');
+
+function mock() {
+ var wasCalled = false,
+ actualArguments = [];
+
+ var stubFunction = function () {
+ wasCalled = true;
+ actualArguments = Array.prototype.slice.call(arguments);
+ };
+
+ stubFunction.wasCalled = function () {
+ return wasCalled;
+ };
+
+ stubFunction.wasCalledWith = function () {
+ var args = Array.prototype.slice.call(arguments);
+
+ return wasCalled &&
+ JSON.stringify(actualArguments) === JSON.stringify(args);
+ };
+
+ return stubFunction;
+}
+
+exports['HTTP Stubbing'] = testCase({
+ setUp: function (callback) {
+ this.request = {};
+ this.response = {
+ writeHead: mock(),
+ write: mock(),
+ end: mock()
+ };
+ callback();
+ },
+
+ 'should have reasonable defaults': function (test) {
+ var stubber = stubs.create();
+
+ stubber.middleware(this.request, this.response);
+
+ test.ok(this.response.writeHead.wasCalledWith(200, { 'Connection': 'close' }));
+ test.ok(this.response.write.wasCalledWith(''));
+ test.ok(this.response.end.wasCalled());
+ test.done();
+ }
+});

0 comments on commit 3a5a263

Please sign in to comment.
Something went wrong with that request. Please try again.