diff --git a/package.json b/package.json index 4492a12..a8edf88 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ }, "devDependencies": { "chai": "4.1.2", - "coffeescript": "1.12.7", "mocha": "5.0.4" }, "keywords": [ diff --git a/scripts/bdd b/scripts/bdd index 3003d78..a107060 100755 --- a/scripts/bdd +++ b/scripts/bdd @@ -1,2 +1,2 @@ #!/bin/sh -./node_modules/mocha/bin/mocha -w --reporter spec --compilers=coffee:coffeescript/register ./test/**/*-test.coffee +./node_modules/mocha/bin/mocha -w --reporter spec ./test/**/*-test.js diff --git a/scripts/test b/scripts/test index 72ca538..7b61f70 100755 --- a/scripts/test +++ b/scripts/test @@ -1,2 +1,2 @@ #!/bin/sh -./node_modules/mocha/bin/mocha --reporter spec --compilers=coffee:coffeescript/register ./test/**/*-test.coffee +./node_modules/mocha/bin/mocha --reporter spec ./test/**/*-test.js diff --git a/test/integration/cli-test.coffee b/test/integration/cli-test.coffee deleted file mode 100644 index 0d954d7..0000000 --- a/test/integration/cli-test.coffee +++ /dev/null @@ -1,93 +0,0 @@ -fs = require('fs') -{assert} = require('chai') -{exec} = require('child_process') - -cmdPrefix = '' - -describe "Command line", () -> - describe "parsing from standard input with --raw", () -> - stdout = "" - stderr = "" - exitStatus = "" - - before (done) -> - cmd = 'cat ./test/fixtures/get/tracefile | ' + - './bin/curl-trace-parser --raw' - - cli = exec cmdPrefix + cmd, (error, out, err) -> - stdout = out - stderr = err - if error - exitStatus = error.status - done() - - cli.on 'exit', (code) -> - exitStatus = code - - it "should not return nothing to stderr", () -> - assert.equal stderr, "" - - it "should return with exit code 0", () -> - - it "should return parsed body to standard output", (done) -> - expectedOutputPath = "./test/fixtures/get/expected-output" - fs.readFile expectedOutputPath, 'utf8', (err, expected) -> - assert.equal stdout, expected - done() - - describe "parsing from standard input with --blueprint option", () -> - stdout = "" - stderr = "" - exitStatus = "" - - before (done) -> - cmd = 'cat ./test/fixtures/post/tracefile | ' + - './bin/curl-trace-parser --blueprint' - - cli = exec cmdPrefix + cmd, (error, out, err) -> - stdout = out - stderr = err - if error - exitStatus = error.status - done() - - cli.on 'exit', (code) -> - exitStatus = code - - it "should not return nothing to stderr", () -> - assert.equal stderr, "" - - it "should return with exit code 0", () -> - - it "should return parsed body in API Blueprint format to standard output", (done) -> - expectedOutputPath = "./test/fixtures/post/expected-output.md" - fs.readFile expectedOutputPath, 'utf8', (err, expected) -> - assert.equal stdout, expected - done() - - describe "no input on stdin and no options", ()-> - - stdout = "" - stderr = "" - exitStatus = "" - - before (done) -> - cmd = './bin/curl-trace-parser' - cli = exec cmdPrefix + cmd, (error, out, err) -> - stdout = out - stderr = err - if error - exitStatus = error.code - - - cli.on 'exit', (code) -> - exitStatus = code - done() - - - it "should exit with status 1", () -> - assert.equal exitStatus, 1 - - it "should return error message to stderr", () -> - assert.include stderr, "No input on stdin" - diff --git a/test/integration/cli-test.js b/test/integration/cli-test.js new file mode 100644 index 0000000..42a413b --- /dev/null +++ b/test/integration/cli-test.js @@ -0,0 +1,101 @@ +const fs = require('fs'); +const { assert } = require('chai'); +const { exec } = require('child_process'); + +const cmdPrefix = ''; + +describe('Command line', () => { + describe('parsing from standard input with --raw', () => { + let stdout = ''; + let stderr = ''; + let exitStatus = ''; + + before((done) => { + const cmd = 'cat ./test/fixtures/get/tracefile | ' + + './bin/curl-trace-parser --raw'; + + const cli = exec(cmdPrefix + cmd, (error, out, err) => { + stdout = out; + stderr = err; + if (error) { + exitStatus = error.status; + } + done(); + }); + + cli.on('exit', (code) => { exitStatus = code; }); + }); + + it('should not return nothing to stderr', () => assert.equal(stderr, '')); + + it('should return with exit code 0', () => assert.equal(exitStatus, 0)); + + it('should return parsed body to standard output', (done) => { + const expectedOutputPath = './test/fixtures/get/expected-output'; + fs.readFile(expectedOutputPath, 'utf8', (err, expected) => { + assert.equal(stdout, expected); + done(); + }); + }); + }); + + describe('parsing from standard input with --blueprint option', () => { + let stdout = ''; + let stderr = ''; + let exitStatus = ''; + + before((done) => { + const cmd = 'cat ./test/fixtures/post/tracefile | ' + + './bin/curl-trace-parser --blueprint'; + + const cli = exec(cmdPrefix + cmd, (error, out, err) => { + stdout = out; + stderr = err; + if (error) { + exitStatus = error.status; + } + done(); + }); + + cli.on('exit', (code) => { exitStatus = code; }); + }); + + it('should not return nothing to stderr', () => assert.equal(stderr, '')); + + it('should return with exit code 0', () => assert.equal(exitStatus, 0)); + + it('should return parsed body in API Blueprint format to standard output', (done) => { + const expectedOutputPath = './test/fixtures/post/expected-output.md'; + fs.readFile(expectedOutputPath, 'utf8', (err, expected) => { + assert.equal(stdout, expected); + done(); + }); + }); + }); + + describe('no input on stdin and no options', () => { + let stderr = ''; + let exitStatus = ''; + + before((done) => { + const cmd = './bin/curl-trace-parser'; + const cli = exec(cmdPrefix + cmd, (error, out, err) => { + stderr = err; + if (error) { + exitStatus = error.code; + } + }); + + + cli.on('exit', (code) => { + exitStatus = code; + done(); + }); + }); + + + it('should exit with status 1', () => assert.equal(exitStatus, 1)); + + it('should return error message to stderr', () => assert.include(stderr, 'No input on stdin')); + }); +}); diff --git a/test/integration/js-api-test.coffee b/test/integration/js-api-test.coffee deleted file mode 100644 index 013890e..0000000 --- a/test/integration/js-api-test.coffee +++ /dev/null @@ -1,18 +0,0 @@ -fs = require 'fs' - -{assert} = require('chai') -{exec} = require('child_process') -parser = require('../../lib/parser') - -describe "Javascript API", () -> - describe "parsing from file", () -> - - it "should return expected raw HTTP in format described in Readme", (done) -> - traceFilePath = "./test/fixtures/get/tracefile" - expectedOutputPath = "./test/fixtures/get/expected-output" - - fs.readFile traceFilePath, 'utf8', (err,trace) -> - parsed = parser.parseToString trace - fs.readFile expectedOutputPath, 'utf8', (err,expected) -> - assert.equal parsed, expected - done() \ No newline at end of file diff --git a/test/integration/js-api-test.js b/test/integration/js-api-test.js new file mode 100644 index 0000000..71ee136 --- /dev/null +++ b/test/integration/js-api-test.js @@ -0,0 +1,20 @@ +const fs = require('fs'); + +const { assert } = require('chai'); +const parser = require('../../lib/parser'); + +describe('Javascript API', () => + describe('parsing from file', () => + + it('should return expected raw HTTP in format described in Readme', (done) => { + const traceFilePath = './test/fixtures/get/tracefile'; + const expectedOutputPath = './test/fixtures/get/expected-output'; + + fs.readFile(traceFilePath, 'utf8', (err, trace) => { + const parsed = parser.parseToString(trace); + fs.readFile(expectedOutputPath, 'utf8', (error, expected) => { + assert.equal(parsed, expected); + done(); + }); + }); + }))); diff --git a/test/unit/parser-test.coffee b/test/unit/parser-test.coffee deleted file mode 100644 index c06fab8..0000000 --- a/test/unit/parser-test.coffee +++ /dev/null @@ -1,161 +0,0 @@ -fs = require 'fs' -assert = require('chai').assert -parser = require '../../lib/parser' - -describe 'parser module', () -> - output = "" - trace = "" - - before (done) -> - fs.readFile __dirname + '/../fixtures/get/tracefile', (err, data) -> - done err if err - trace = data.toString() - done() - - it "has parse() defined", () -> - assert.isFunction parser.parse - - describe "parse(string) return", () -> - - before () -> - output = parser.parse trace - - it "is obejct", () -> - assert.isObject output - - describe "returned object", () -> - - it "has key request", () -> - assert.include Object.keys(output), "request" - - it "has key response", () -> - assert.include Object.keys(output), "response" - - describe "parsed request", () -> - request = "" - before () -> - request = output['request'] - - it "contains multiple lines delimited by CRLF", () -> - lines = request.split "\r\n" - assert.isTrue lines.length > 1, 'expected more than 1 item, got: ' + lines.length - - it "contains user agent string", () -> - agentString = "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5" - assert.include request, agentString - - - it "does not contain double LF at the end", () -> - outputChars = request.split '' - - lastChars = "" - lastChars = outputChars.pop() + lastChars - lastChars = outputChars.pop() + lastChars - assert.notInclude lastChars, "\n\n" - - describe "parsed response", () -> - response = "" - before () -> - response = output['response'] - - it "containt application/json", () -> - string = "application/json" - assert.include response, string - - it "contains multiple lines delimited by CRLF", () -> - lines = response.split "\r\n" - assert.isTrue lines.length > 1, 'expected more than 1 item, got: ' + lines.length - - it "has parseToString() defined", () -> - assert.isFunction parser.parseToString - - describe "parseToString(traceString) return", () -> - - before () -> - output = parser.parseToString trace - - it "should have trailing LF as last character to do not brake terminal", () -> - outputChars = output.split '' - lastChars = "" - lastChars = outputChars.pop() + lastChars - lastChars = outputChars.pop() + lastChars - assert.include lastChars, "\n" - - it "should have Request ended by trailling LF", () -> - request = [] - - for line in output.split('\r\n') - request.push line if /^> /.test line - - lastLine = request.pop() - assert.equal lastLine.split('').pop(), '\n' - - it "should have all parsed Request lines leaded by '> '", () -> - request = parser.parse(trace)['request'] - - counter = 0 - for line in request.split "\r\n" - counter++ - needle = '> ' + line - assert.include output, needle, "Request on line #" + counter.toString() + " does not contain needle." - - it "should have all parsed Response lines leaded by '< '", () -> - response = parser.parse(trace)['response'] - - counter = 0 - for line in response.split "\r\n" - counter++ - needle = '< ' + line - assert.include output, needle, "Response on line #" + counter.toString() + " does not contain needle." - - describe "parseBackRequestAndResponseFromString(parsedString)", () -> - parsedObject = {} - parsedString = "" - - before () -> - parsedObject = parser.parse trace - parsedString = parser.parseToString trace - output = parser.parseBackRequestAndResponseFromString parsedString - - it "has parseBackRequestAndResponseFromString() defined", () -> - assert.isFunction parser.parseBackRequestAndResponseFromString - - it "should return object", () -> - assert.isObject output - - describe "returned object", () -> - it "has key request", () -> - assert.include Object.keys(output), "request" - - it "has key response", () -> - assert.include Object.keys(output), "response" - - describe "parsed request", () -> - request = "" - before () -> - request = output['request'] - - it "is a string", () -> - assert.isString request - - it "is equal to raw genuine request", () -> - assert.equal request, parsedObject['request'] - - describe "parsed response", () -> - response = "" - before () -> - response = output['response'] - - it "is a string", () -> - assert.isString response - - it "is equal to raw genuine response", () -> - assert.equal response, parsedObject['response'] - - describe "parseBack(parsedString) ", () -> - it "is a function", () -> - assert.isFunction parser.parseBack - - it "is an alias for parseBackRequestAndResponseFromString", () -> - assert.equal parser.parseBack, parser.parseBackRequestAndResponseFromString - diff --git a/test/unit/parser-test.js b/test/unit/parser-test.js new file mode 100644 index 0000000..3155ff2 --- /dev/null +++ b/test/unit/parser-test.js @@ -0,0 +1,160 @@ +const fs = require('fs'); +const { assert } = require('chai'); +const parser = require('../../lib/parser'); + +describe('parser module', () => { + let output = ''; + let trace = ''; + + before(done => + fs.readFile(`${__dirname}/../fixtures/get/tracefile`, (err, data) => { + if (err) { done(err); } + trace = data.toString(); + done(); + })); + + it('has parse() defined', () => assert.isFunction(parser.parse)); + + describe('parse(string) return', () => { + before(() => { output = parser.parse(trace); }); + + it('is obejct', () => assert.isObject(output)); + + describe('returned object', () => { + it('has key request', () => assert.include(Object.keys(output), 'request')); + + it('has key response', () => assert.include(Object.keys(output), 'response')); + + describe('parsed request', () => { + let request = ''; + before(() => { ({ request } = output); }); + + it('contains multiple lines delimited by CRLF', () => { + const lines = request.split('\r\n'); + assert.isTrue(lines.length > 1, `expected more than 1 item, got: ${lines.length}`); + }); + + it('contains user agent string', () => { + const agentString = 'curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5'; + assert.include(request, agentString); + }); + + it('does not contain double LF at the end', () => { + const outputChars = request.split(''); + + let lastChars = ''; + lastChars = outputChars.pop() + lastChars; + lastChars = outputChars.pop() + lastChars; + assert.notInclude(lastChars, '\n\n'); + }); + }); + + describe('parsed response', () => { + let response = ''; + before(() => { ({ response } = output); }); + + it('containt application/json', () => { + const string = 'application/json'; + assert.include(response, string); + }); + + it('contains multiple lines delimited by CRLF', () => { + const lines = response.split('\r\n'); + assert.isTrue(lines.length > 1, `expected more than 1 item, got: ${lines.length}`); + }); + }); + }); + }); + + it('has parseToString() defined', () => assert.isFunction(parser.parseToString)); + + describe('parseToString(traceString) return', () => { + before(() => { output = parser.parseToString(trace); }); + + it('should have trailing LF as last character to do not brake terminal', () => { + const outputChars = output.split(''); + let lastChars = ''; + lastChars = outputChars.pop() + lastChars; + lastChars = outputChars.pop() + lastChars; + assert.include(lastChars, '\n'); + }); + + it('should have Request ended by trailling LF', () => { + const request = []; + + for (const line of output.split('\r\n')) { + if (/^> /.test(line)) { request.push(line); } + } + + const lastLine = request.pop(); + assert.equal(lastLine.split('').pop(), '\n'); + }); + + it("should have all parsed Request lines leaded by '> '", () => { + const { request } = parser.parse(trace); + + let counter = 0; + for (const line of request.split('\r\n')) { + counter++; + const needle = `> ${line}`; + assert.include(output, needle, `Request on line #${counter.toString()} does not contain needle.`); + } + }); + + it("should have all parsed Response lines leaded by '< '", () => { + const { response } = parser.parse(trace); + + let counter = 0; + for (const line of response.split('\r\n')) { + counter++; + const needle = `< ${line}`; + assert.include(output, needle, `Response on line #${counter.toString()} does not contain needle.`); + } + }); + }); + + describe('parseBackRequestAndResponseFromString(parsedString)', () => { + let parsedObject = {}; + let parsedString = ''; + + before(() => { + parsedObject = parser.parse(trace); + parsedString = parser.parseToString(trace); + output = parser.parseBackRequestAndResponseFromString(parsedString); + }); + + it('has parseBackRequestAndResponseFromString() defined', () => assert.isFunction(parser.parseBackRequestAndResponseFromString)); + + it('should return object', () => assert.isObject(output)); + + describe('returned object', () => { + it('has key request', () => assert.include(Object.keys(output), 'request')); + + it('has key response', () => assert.include(Object.keys(output), 'response')); + + describe('parsed request', () => { + let request = ''; + before(() => { ({ request } = output); }); + + it('is a string', () => assert.isString(request)); + + it('is equal to raw genuine request', () => assert.equal(request, parsedObject.request)); + }); + + describe('parsed response', () => { + let response = ''; + before(() => { ({ response } = output); }); + + it('is a string', () => assert.isString(response)); + + it('is equal to raw genuine response', () => assert.equal(response, parsedObject.response)); + }); + }); + }); + + describe('parseBack(parsedString) ', () => { + it('is a function', () => assert.isFunction(parser.parseBack)); + + it('is an alias for parseBackRequestAndResponseFromString', () => assert.equal(parser.parseBack, parser.parseBackRequestAndResponseFromString)); + }); +});