UNPKG

4.54 kBJavaScriptView Raw
1//
2// mimetype_test.js - tests for mimetype.js module for NodeJS.
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
17// Tests for version 0.0.2
18harness.push({callback: function () {
19 assert.equal(mimetype.lookup("myfile.txt"), 'text/plain', "lookup should return text/plain");
20 assert.equal(mimetype.set('.exotic', 'x-application/experimental'), true, "set should return true.");
21 assert.equal(mimetype.lookup("myfile.exotic"), "x-application/experimental", "lookup should return x-application/experimental");
22 assert.equal(mimetype.del('.exotic'), true, "del() should return true");
23 assert.equal(mimetype.lookup("myfile.exotic"), false, "lookup(myfile.exotic) should return false now");
24 ky_cnt = Object.keys(mimetype.catalog).length;
25 i = 0;
26 mimetype.forEach(function (ext, mime_type_string) {
27 assert.ok(ext, "Should have an ext");
28 assert.ok(mime_type_string, "Should have a mime_type string");
29 assert.strictEqual(mimetype.catalog[ext], mime_type_string);
30 i += 1;
31 });
32 assert.equal(ky_cnt, i, "i should equal ky_cnt");
33
34 // Test multi-extension set()
35 assert.equal(mimetype.lookup("test.txt1"), false, "Should not have the .txt1 defined yet.");
36 assert.equal(mimetype.lookup("test.txt2"), false, "Should not have the .txt2 defined yet.");
37 assert.equal(mimetype.lookup("test.txt3"), false, "Should not have the .txt3 defined yet.");
38 mimetype.set(".txt1,.txt2,.txt3", "text/plain");
39 assert.equal(mimetype.lookup("test.txt1"), "text/plain", "Should have the .txt1 now.");
40 assert.equal(mimetype.lookup("test.txt2"), "text/plain", "Should have the .txt2 now.");
41 assert.equal(mimetype.lookup("test.txt3"), "text/plain", "Should have the .txt3 now.");
42 assert.equal(mimetype.lookup("this.isNotDefined"), false, "Should not have a mime type defined for this.isNotDefined");
43 assert.equal(mimetype.lookup("this.isNotDefined", false, "text/plain"), "text/plain", "Should not have a mime type defined for this.isNotDefined");
44 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"));
45 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"));
46
47 assert.equal(mimetype.lookup("README"), "text/plain", "README should return text/plain mime-type.");
48 assert.equal(mimetype.lookup("manifest"), "text/cache-manifest", "manifest should return text/plain mime-type.");
49 harness.completed("tests for version 0.0.2");
50}, label: "tests for version 0.0.2"});
51
52// tests for version 0.0.3
53harness.push({callback: function () {
54 Object.keys(v0_0_3).forEach(function (i) {
55 var vals, j, testname;
56
57 assert.ok(v0_0_3[i].mime_type, "Missing v0_0_3 index:" + i);
58 if (v0_0_3[i].ext !== undefined) {
59 if (v0_0_3[i].ext.indexOf(" ") > 0) {
60 vals = v0_0_3[i].ext.split(" ");
61 for (j = 0; j < vals.length; j += 1) {
62 testname = ["testname", vals[j]].join(".");
63 assert.equal(
64 mimetype.lookup(testname),
65 v0_0_3[i].mime_type,
66 [testname, mimetype.lookup(testname), '->', v0_0_3[i].mime_type, vals[j], "failed"].join(" "));
67 }
68 } else {
69 testname = ["testname", v0_0_3[i].ext].join(".");
70 assert.equal(
71 mimetype.lookup(testname),
72 v0_0_3[i].mime_type,
73 [testname, v0_0_3[i].mime_type, "failed"].join(" "));
74 }
75 }
76 });
77 harness.completed("tests for version 0.0.3");
78}, label: "tests for version 0.0.3"});
79
80if (require.main === module) {
81 harness.RunIt(path.basename(module.filename), 10);
82} else {
83 exports.RunIt = harness.RunIt;
84}