Unit test framework for web client-side code

- Also make formatSize nicer and test it.

Now there's no excuse! ;)
This commit is contained in:
Aldo Cortesi 2015-01-02 15:29:51 +13:00
parent b14b4ace25
commit 80339aef93
6 changed files with 91 additions and 55 deletions

View File

@ -127,7 +127,7 @@ class WebMaster(flow.FlowMaster):
self.app = app.Application(self, self.options.wdebug) self.app = app.Application(self, self.options.wdebug)
if options.rfile: if options.rfile:
try: try:
print(self.load_flows_file(options.rfile)) self.load_flows_file(options.rfile)
except flow.FlowReadError, v: except flow.FlowReadError, v:
self.add_event( self.add_event(
"Could not read flow file: %s"%v, "Could not read flow file: %s"%v,

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,19 @@
{ {
"name": "mitmproxy", "name": "mitmproxy",
"private": true, "private": true,
"scripts": {
"test": "jest ./src/js"
},
"jest": {
"testPathDirs": [
"./src/js"
],
"testPathIgnorePatterns": [
"/node_modules/",
"testutils.js"
],
"testDirectoryName": "tests"
},
"dependencies": { "dependencies": {
"jquery": "", "jquery": "",
"lodash": "", "lodash": "",
@ -38,7 +51,6 @@
"vinyl-buffer": "", "vinyl-buffer": "",
"vinyl-source-stream": "", "vinyl-source-stream": "",
"vinyl-transform": "", "vinyl-transform": "",
"gulp-peg": "" "gulp-peg": ""
} }
} }

View File

@ -1,3 +0,0 @@
QUnit.test("example test", function (assert) {
assert.ok(true);
});

15
web/src/js/tests/utils.js Normal file
View File

@ -0,0 +1,15 @@
jest.dontMock("jquery");
jest.dontMock("../utils");
describe("utils", function () {
var utils = require("../utils");
it("formatSize", function(){
expect(utils.formatSize(1024)).toEqual("1kb");
expect(utils.formatSize(0)).toEqual("0");
expect(utils.formatSize(10)).toEqual("10b");
expect(utils.formatSize(1025)).toEqual("1.0kb");
expect(utils.formatSize(1024*1024)).toEqual("1mb");
expect(utils.formatSize(1024*1024+1)).toEqual("1.0mb");
});
});

View File

@ -23,14 +23,20 @@ for (var i = 65; i <= 90; i++) {
var formatSize = function (bytes) { var formatSize = function (bytes) {
var size = bytes; if (bytes === 0)
var prefix = ["B", "KB", "MB", "GB", "TB"]; return "0";
var i = 0; var prefix = ["b", "kb", "mb", "gb", "tb"];
while (Math.abs(size) >= 1024 && i < prefix.length - 1) { for (var i = 0; i < prefix.length; i++){
i++; if (Math.pow(1024, i + 1) > bytes){
size = size / 1024; break;
} }
return (Math.floor(size * 100) / 100.0).toFixed(2) + prefix[i]; }
var precision;
if (bytes%Math.pow(1024, i) === 0)
precision = 0;
else
precision = 1;
return (bytes/Math.pow(1024, i)).toFixed(precision) + prefix[i];
}; };