Browse files

add test

  • Loading branch information...
1 parent 4730c83 commit 601ec3edd4d53ea5373c2f875486b57f1c6b918c @leetreveil committed Nov 20, 2012
Showing with 41 additions and 17 deletions.
  1. +19 −0 lib/common.js
  2. +1 −16 lib/musicmetadata.js
  3. +2 −1 test/alltests.js
  4. +19 −0 test/test-regress-GH-14.js
View
19 lib/common.js
@@ -1,6 +1,25 @@
var strtok = require('strtok');
var stream = require('stream');
+exports.detectMediaType = function (header) {
+ //default to id3v1.1 if we cannot detect any other tags
+ var tag = 'id3v1';
+ if ('ID3' === header.slice(0, 3)) {
+ tag = 'id3v2';
+ } else if ('ftypM4A' === header.slice(4, 11)) {
+ tag = 'id4';
+ } else if ('ftypmp42' === header.slice(4, 12)) {
+ tag = 'id4'
+ } else if ('OggS' === header.slice(0, 4)) {
+ tag = 'ogg';
+ } else if ('fLaC' === header.slice(0, 4)) {
+ tag = 'flac';
+ } else if ('MAC' === header.slice(0, 3)) {
+ tag = 'monkeysaudio';
+ }
+ return tag;
+}
+
stream.Stream.prototype.onRealEnd = function(callback) {
var called = false;
View
17 lib/musicmetadata.js
@@ -12,23 +12,8 @@ MusicMetadata.prototype.parse = function() {
var self = this;
this.stream.once('data', function(result) {
-
- //default to id3v1.1 if we cannot detect any other tags
- var tag = 'id3v1';
- if ('ID3' === result.toString('binary', 0, 3)) {
- tag = 'id3v2';
- } else if ('ftypM4A' === result.toString('binary', 4, 11)) {
- tag = 'id4';
- } else if ('ftypmp42' === result.toString('binary', 4, 12)) {
- tag = 'id4'
- } else if ('OggS' === result.toString('binary', 0, 4)) {
- tag = 'ogg';
- } else if ('fLaC' === result.toString('binary', 0, 4)) {
- tag = 'flac';
- } else if ('MAC' === result.toString('binary', 0, 3)) {
- tag = 'monkeysaudio';
- }
+ var tag = common.detectMediaType(result.toString('binary'));
var module = require('./' + tag);
var parser = new module(self.stream);
View
3 test/alltests.js
@@ -9,7 +9,8 @@ if (module == require.main) {
'test-unknownencoding.js', 'test-nonasciichars.js',
'test-flac.js', 'test-utf16bom-encoding.js',
'test-ogg-multipagemetadatabug.js',
- 'test-apev2-monkeysaudio.js'];
+ 'test-apev2-monkeysaudio.js',
+ 'test-regress-GH-14.js'];
var passedTests = 0;
View
19 test/test-regress-GH-14.js
@@ -0,0 +1,19 @@
+var common = require('../lib/common');
+var testy = require('testy')();
+var assert = testy.assert;
+
+// should be able to detect ftypmp42 as a valid mp4 header type
+var buf = new Buffer(12);
+buf[0] = '0x00'
+buf[1] = '0x00'
+buf[2] = '0x00'
+buf[3] = '0x18'
+buf[4] = '0x66' // f
+buf[5] = '0x74' // t
+buf[6] = '0x79' // y
+buf[7] = '0x70' // p
+buf[8] = '0x6D' // m
+buf[9] = '0x70' // p
+buf[10] = '0x34' // 4
+buf[11] = '0x32' // 2
+assert.strictEqual(common.detectMediaType(buf.toString('binary')), 'id4');

0 comments on commit 601ec3e

Please sign in to comment.