* Test grep.js
(assert)
| 390 | * Test grep.js |
| 391 | */ |
| 392 | function testGrep(assert) { |
| 393 | |
| 394 | var handler = grep.main, |
| 395 | exit = function () {}, |
| 396 | headers, content, pipes; |
| 397 | |
| 398 | // Helper for converting strings to buffers. |
| 399 | function buffer(data) { |
| 400 | if (data.constructor != Buffer) { |
| 401 | data = new Buffer(data, 'utf8'); |
| 402 | } |
| 403 | return data; |
| 404 | } |
| 405 | |
| 406 | // Simple grep. |
| 407 | pipes = mockPipes(); |
| 408 | handler([ 'grep', 'ba' ], pipes, exit); |
| 409 | |
| 410 | pipes.dataOut.on('data', function (data) { |
| 411 | if (data.toString('utf-8').indexOf('\r\n\r\n') >= 0) return; |
| 412 | var lines = data.toString('utf-8').split("\n"); |
| 413 | assert(lines.length == 3 && lines[0] == 'bar' && |
| 414 | lines[1] == 'baz' && lines[2] == 'ccbacc', |
| 415 | "Grep plaintext lines"); |
| 416 | }); |
| 417 | |
| 418 | headers = new meta.headers(); |
| 419 | content = "foo\nbar\nbaz\nbingo\nccbacc\n\n\nfffuuu\n"; |
| 420 | headers.set('Content-Type', 'text/plain'); |
| 421 | headers.set('Content-Length', content.length); |
| 422 | pipes.dataIn.emit('data', buffer(headers.generate())); |
| 423 | pipes.dataIn.emit('data', buffer(content)); |
| 424 | pipes.dataIn.emit('end'); |
| 425 | |
| 426 | // Simple grep (negative). |
| 427 | pipes = mockPipes(); |
| 428 | handler([ 'grep', '-v', 'ba' ], pipes, exit); |
| 429 | |
| 430 | pipes.dataOut.on('data', function (data) { |
| 431 | if (data.toString('utf-8').indexOf('\r\n\r\n') >= 0) return; |
| 432 | var lines = data.toString('utf-8').split("\n"); |
| 433 | assert(lines.length == 5 && lines[0] == 'foo' && |
| 434 | lines[1] == 'bingo' && lines[2] == '' && lines[3] == '' && lines[4] == 'fffuuu', |
| 435 | "Grep plaintext lines (negative)"); |
| 436 | }); |
| 437 | |
| 438 | headers = new meta.headers(); |
| 439 | content = "foo\nbar\nbaz\nbingo\nccbacc\n\n\nfffuuu\n"; |
| 440 | headers.set('Content-Type', 'text/plain'); |
| 441 | headers.set('Content-Length', content.length); |
| 442 | pipes.dataIn.emit('data', buffer(headers.generate())); |
| 443 | pipes.dataIn.emit('data', buffer(content)); |
| 444 | pipes.dataIn.emit('end'); |
| 445 | |
| 446 | // JSON grep. |
| 447 | pipes = mockPipes(); |
| 448 | handler([ 'grep', 'ba' ], pipes, exit); |
| 449 |