-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnodejs-documents-binary-range.js
More file actions
138 lines (124 loc) · 4.8 KB
/
Copy pathnodejs-documents-binary-range.js
File metadata and controls
138 lines (124 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var should = require('should');
var fs = require('fs');
var stream = require('stream');
var util = require('util');
var concatStream = require('concat-stream');
var valcheck = require('core-util-is');
var testconfig = require('../etc/test-config-qa.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var dbReader = marklogic.createDatabaseClient(testconfig.restReaderConnection);
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
describe('Binary documents test', function () {
var binaryPath = __dirname + '/data/mediaCQ.mp3';
var uri = '/test/binary/range/someMp3File.mp3';
var binaryValue = null;
before(function (done) {
this.timeout(10000);
fs.createReadStream(binaryPath).
pipe(concatStream({ encoding: 'buffer' }, function (value) {
binaryValue = value;
done();
}));
});
it('should write the binary with Readable stream', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
var readableBinary = new ValueStream(binaryValue);
//readableBinary.pause();
dbWriter.documents.write({
uri: uri,
contentType: 'audio/mpeg',
content: readableBinary
}).
result(function (response) {
response.should.have.property('documents');
done();
}, done);
});
it('should read the binary with range', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
dbReader.documents.read({ uris: uri, range: [10, 15] }).
result(function (documents) {
//console.log(JSON.stringify(documents[0], null, 2));
JSON.stringify(documents[0].content[0]).should.equal('80');
done();
}, done);
});
it('should read the binary with invalid range', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
dbReader.documents.read({ uris: uri, range: [-10, 15] }).
result(function (documents) {
documents.should.equal('SHOULD HAVE FAILED');
done();
}, function (error) {
error.body.errorResponse.messageCode.should.equal('REST-INVALIDPARAM');
//console.log(JSON.stringify(error, null, 2));
done();
});
});
it('should read the binary with invalid float range', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
dbReader.documents.read({ uris: uri, range: [10.6, 15] }).
result(function (documents) {
documents.should.equal('SHOULD HAVE FAILED');
done();
}, function (error) {
error.body.errorResponse.messageCode.should.equal('REST-INVALIDPARAM');
//console.log(JSON.stringify(error, null, 2));
done();
});
});
it('should read the binary with reversed range', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
try {
dbReader.documents.read({ uris: uri, range: [15, 10] }).
should.equal('SHOULD HAVE FAILED');
done();
} catch (error) {
var strError = error.toString();
//console.log(error.toString());
strError.should.equal('Error: start length greater than or equal to end length for byte range: 15,10');
done();
}
});
it('should read the binary without start range', function (done) {
this.timeout(10000);
var uri = '/test/write/range/someMp3File.mp3';
try {
dbReader.documents.read({ uris: uri, range: [, 10] }).
should.equal('SHOULD HAVE FAILED');
done();
} catch (error) {
var strError = error.toString();
//console.log(error.toString());
strError.should.equal('Error: start length for byte range parameter is not integer: undefined');
done();
}
});
it('should delete mp3 file', function (done) {
dbAdmin.documents.removeAll({
all: true
}).
result(function (response) {
done();
}, done);
});
});
function ValueStream(value) {
stream.Readable.call(this);
this.value = value;
}
util.inherits(ValueStream, stream.Readable);
ValueStream.prototype._read = function () {
this.push(this.value);
this.push(null);
};