-
Notifications
You must be signed in to change notification settings - Fork 8
/
can-ndjson-stream-test.js
116 lines (95 loc) · 3.3 KB
/
can-ndjson-stream-test.js
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
var QUnit = require("steal-qunit");
var ndjsonStream = require("can-ndjson-stream");
// Skip all tests in browsers that do not support ReadableStream
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
var isReadStreamSupported = true;
try {
new ReadableStream();
} catch(err) {
isReadStreamSupported = false;
}
var conditionalTest = isReadStreamSupported ? QUnit.test : QUnit.skip;
var conditionalAsyncTest = isReadStreamSupported ? QUnit.test : QUnit.skip;
function readableStreamFromString(s) {
return new ReadableStream({
start: function(controller) {
var encoder = new TextEncoder();
// Our current position in s
var pos = 0;
// How much to serve on each push
var chunkSize = 1;
function push() {
// Are we done?
if (pos >= s.length) {
controller.close();
return;
}
// Push some of the html,
// converting it into an Uint8Array of utf-8 data
controller.enqueue(
encoder.encode(s.slice(pos, pos + chunkSize))
);
// Advance the position
pos += chunkSize;
push();
}
// Let's go!
push();
},
cancel: function() {
}
});
}
function inputStream(objArray) {
var jsons = objArray.map( function(obj) {return JSON.stringify(obj);} );
return readableStreamFromString(jsons.join('\n'));
}
QUnit.module('can-ndjson-stream');
conditionalTest('Initialized the plugin', function(assert){
assert.equal(typeof ndjsonStream, 'function');
});
conditionalAsyncTest('simple_test_from_stream', function(assert) {
var testObject = [
{"date":"2017-02-24 03:07:45","user":"21109850","fuel":"37","ammo":"2","steel":"13","baux":"5","seaweed":"0","type":"LOOT","product":"134"},
{"date":"2017-02-22 04:40:13","user":"21109850","fuel":"37","ammo":"2","steel":"13","baux":"5","seaweed":"0","type":"LOOT","product":"75"},
{"date":"2017-02-21 20:47:51","user":"26464462","fuel":"37","ammo":"3","steel":"19","baux":"5","seaweed":"1","type":"LOOT","product":"81"}
];
var readObjects = [];
var todoStream = ndjsonStream( inputStream(testObject) );
var reader = todoStream.getReader();
var done = assert.async();
reader.read().then(function read(result) {
if (result.done) {
assert.deepEqual(readObjects, testObject, "Two arrays should be the same in value");
done();
return;
}
readObjects.push(result.value);
return reader.read().then(read);
});
});
conditionalAsyncTest('malformed json', function(assert) {
var malformed_string = "{\"1\":2}\n{sss: 2}";
var readObjects = [];
var todoStream = ndjsonStream( readableStreamFromString(malformed_string) );
var reader = todoStream.getReader();
var errorCaught = false;
function errCheck() {
errorCaught = true;
}
var done = assert.async();
var allDone = reader.read().then(function read(result) {
if (result.start) {
return;
}
readObjects.push(result.value);
return reader.read().then(read, errCheck);
}, errCheck);
allDone.then(function(){
assert.strictEqual(errorCaught, true, "malformed json string should cause an error");
done();
}, function(){
assert.strictEqual(errorCaught, true, "rejected: malformed json string should cause an error");
done();
});
});