-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TBR playground/client: add JSON stream test coverage
* Add tests for error and edge cases for the JSONStream * api.run() returns stream directly instead of having the extra callback. * Add initial message to let the user know that the run has started. Change-Id: I9056da42389c4b4e3dd62e00b9e0b2d69175800b Related: vanadium/issues#38
- Loading branch information
Showing
8 changed files
with
190 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2015 The Vanadium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
var test = require('tape'); | ||
var jsonStream = require('../browser/api/json-stream'); | ||
var Buffer = require('buffer').Buffer; | ||
|
||
test('jsonStream() - single entity per write', function(t) { | ||
t.plan(4); | ||
|
||
var stream = jsonStream(); | ||
|
||
stream | ||
.on('data', function(data) { | ||
t.same(data, { foo: 'bar' }); | ||
}); | ||
|
||
iterate(4, function() { | ||
stream.write(new Buffer('{ "foo": "bar" }\n')); | ||
}); | ||
|
||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - empty strings', function(t) { | ||
t.plan(1); | ||
|
||
var stream = jsonStream(); | ||
|
||
stream | ||
.on('data', function(data) { | ||
t.same(data, { foo: 'bar' }); | ||
}); | ||
|
||
stream.write(new Buffer('{ "foo": "bar" }\n')); | ||
stream.write(new Buffer('')); | ||
|
||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - single entry, multiple writes', function(t) { | ||
t.plan(1); | ||
|
||
var stream = jsonStream(); | ||
|
||
stream | ||
.on('data', function(data) { | ||
t.same(data, { foo: 'bar', baz: 'qux' }); | ||
}); | ||
|
||
stream.write(new Buffer('{ "foo":')); | ||
stream.write(new Buffer('"ba')); | ||
stream.write(new Buffer('r", "ba')); | ||
stream.write(new Buffer('z":')); | ||
stream.write(new Buffer('"qux" }\n')); | ||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - chunk with several entries, then pieces', function(t) { | ||
t.plan(11); | ||
|
||
var stream = jsonStream(); | ||
var chunk = ''; | ||
|
||
stream | ||
.on('data', function(data) { | ||
t.same(data, { a: 'b' }); | ||
}); | ||
|
||
iterate(10, function() { | ||
chunk += '{ "a": "b" }\n'; | ||
}); | ||
|
||
chunk += '{ "a":'; | ||
|
||
stream.write(new Buffer(chunk)); | ||
stream.write(new Buffer(' "b" }\n')); | ||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - end with a partial entry', function(t) { | ||
t.plan(2); | ||
|
||
var stream = jsonStream(); | ||
|
||
stream | ||
.on('end', t.end) | ||
.on('error', function(err) { | ||
t.fail('should not error'); | ||
}) | ||
.on('data', function(data) { | ||
t.same(data, { a: 'b' }); | ||
}); | ||
|
||
stream.write('{ "a": "b" }\n'); | ||
stream.write('{ "a": "b" }\n'); | ||
stream.write('{ "a": '); | ||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - blank line entries', function(t) { | ||
t.plan(1); | ||
|
||
var stream = jsonStream(); | ||
|
||
stream | ||
.on('end', t.end) | ||
.on('error', function(err) { | ||
t.fail('should not error'); | ||
}) | ||
.on('data', function(data) { | ||
t.same(data, { a: 'b' }); | ||
}); | ||
|
||
stream.write('{ "a": "b" }\n'); | ||
stream.write(''); | ||
stream.end(); | ||
}); | ||
|
||
test('jsonStream() - bad json entry', function(t) { | ||
var stream = jsonStream(); | ||
var chunk = '{ bad: "json"'; | ||
|
||
stream.on('error', function(err) { | ||
t.ok(err instanceof Error, 'should error'); | ||
t.ok(err instanceof SyntaxError, 'should be a SyntaxError'); | ||
t.end(); | ||
}); | ||
|
||
stream.write(new Buffer(chunk + '\n')); | ||
}); | ||
|
||
function iterate(times, iterator) { | ||
for (var i = 0; i < times; i++) { | ||
iterator(i); | ||
} | ||
} |