UNPKG

4.6 kBJavaScriptView Raw
1//
2// mongo-mimetype_test.js - tests for mimetype.js module for Mongo Shell 2.2.
3//
4// @author: R. S. Doiel, <rsdoiel@gmail.com>
5// copyright (c) 2012 all rights reserved
6//
7// Released under the Simplified BSD License.
8// See: http://opensource.org/licenses/bsd-license.php
9//
10
11var assert = require('assert'),
12 path = require('path'),
13 harness = require('harness'),
14 v0_0_3, // = require('../lib/v0.0.3').data,
15 mimetype = require('mimetype'),
16 module = {
17 filename: "mongo-mimetype_test.js"
18 };
19
20load("./lib/v0.0.3.js");
21
22var getKeys = function (obj) {
23 var results = [], ky;
24 for (ky in obj) {
25 if (obj.hasOwnProperty(ky)) {
26 results.push(ky);
27 }
28 }
29
30 return results;
31};
32
33// Tests for version 0.0.2
34harness.push({callback: function () {
35 assert.equal(mimetype.lookup("myfile.txt"), 'text/plain', "lookup should return text/plain");
36 assert.equal(mimetype.set('.exotic', 'x-application/experimental'), true, "set should return true.");
37 assert.equal(mimetype.lookup("myfile.exotic"), "x-application/experimental", "lookup should return x-application/experimental");
38 assert.equal(mimetype.del('.exotic'), true, "del() should return true");
39 assert.equal(mimetype.lookup("myfile.exotic"), false, "lookup(myfile.exotic) should return false now");
40 ky_cnt = (getKeys(mimetype.catalog)).length;
41 i = 0;
42 mimetype.forEach(function (ext, mime_type_string) {
43 assert.ok(ext, "Should have an ext");
44 assert.ok(mime_type_string, "Should have a mime_type string");
45 assert.strictEqual(mimetype.catalog[ext], mime_type_string);
46 i += 1;
47 });
48 assert.equal(ky_cnt, i, "i should equal ky_cnt");
49
50 // Test multi-extension set()
51 assert.equal(mimetype.lookup("test.txt1"), false, "Should not have the .txt1 defined yet.");
52 assert.equal(mimetype.lookup("test.txt2"), false, "Should not have the .txt2 defined yet.");
53 assert.equal(mimetype.lookup("test.txt3"), false, "Should not have the .txt3 defined yet.");
54 mimetype.set(".txt1,.txt2,.txt3", "text/plain");
55 assert.equal(mimetype.lookup("test.txt1"), "text/plain", "Should have the .txt1 now.");
56 assert.equal(mimetype.lookup("test.txt2"), "text/plain", "Should have the .txt2 now.");
57 assert.equal(mimetype.lookup("test.txt3"), "text/plain", "Should have the .txt3 now.");
58 assert.equal(mimetype.lookup("this.isNotDefined"), false, "Should not have a mime type defined for this.isNotDefined");
59 assert.equal(mimetype.lookup("this.isNotDefined", false, "text/plain"), "text/plain", "Should not have a mime type defined for this.isNotDefined");
60 assert.equal(mimetype.lookup("this.isNotDefined", true, "text/plain"), "text/plain; charset=UTF-8", "Should have a mime type with charset defined for this.isNotDefined: " + mimetype.lookup("this.isNotDefined", true, "text/plain"));
61 assert.equal(mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"), "text/plain; charset=UTF-8", "this.isNotDefined should be text/plain;charset=UTF-8: " + mimetype.lookup("this.isNotDefined", "UTF-8", "text/plain"));
62
63 assert.equal(mimetype.lookup("README"), "text/plain", "README should return text/plain mime-type.");
64 assert.equal(mimetype.lookup("manifest"), "text/cache-manifest", "manifest should return text/plain mime-type.");
65 harness.completed("tests for version 0.0.2");
66}, label: "tests for version 0.0.2"});
67
68console.log("Testing version 0.0.3 test.");
69// tests for version 0.0.3
70harness.push({callback: function () {
71 var ky, obj_test;
72
73 obj_test = function (i) {
74 var vals, j, testname;
75
76 assert.ok(v0_0_3[i].mime_type, "Missing v0_0_3 index:" + i);
77 if (v0_0_3[i].ext !== undefined) {
78 if (v0_0_3[i].ext.indexOf(" ") > 0) {
79 vals = v0_0_3[i].ext.split(" ");
80 for (j = 0; j < vals.length; j += 1) {
81 testname = ["testname", vals[j]].join(".");
82 assert.equal(
83 mimetype.lookup(testname),
84 v0_0_3[i].mime_type,
85 [testname, mimetype.lookup(testname), '->', v0_0_3[i].mime_type, vals[j], "failed"].join(" "));
86 }
87 } else {
88 testname = ["testname", v0_0_3[i].ext].join(".");
89 assert.equal(
90 mimetype.lookup(testname),
91 v0_0_3[i].mime_type,
92 [testname, v0_0_3[i].mime_type, "failed"].join(" "));
93 }
94 }
95 };
96
97 for (ky in v0_0_3) {
98 if (v0_0_3.hasOwnProperty(ky)) {
99 obj_test(ky);
100 }
101 }
102 harness.completed("tests for version 0.0.3");
103}, label: "tests for version 0.0.3"});
104
105harness.RunIt(path.basename(module.filename), 10);