Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
add feature for html produces
Browse files Browse the repository at this point in the history
  • Loading branch information
SrHuevo committed Apr 4, 2018
1 parent cb48d7a commit c93b2d3
Show file tree
Hide file tree
Showing 18 changed files with 481 additions and 433 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamF
handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation);
handlebars.registerHelper('isJsonMediaType', helpers.isJsonMediaType);
handlebars.registerHelper('isXmlMediaType', helpers.isXmlMediaType);
handlebars.registerHelper('isHtmlMediaType', helpers.isHtmlMediaType);
handlebars.registerHelper('isPdfMediaType', helpers.isPdfMediaType);
handlebars.registerHelper('isNecessaryBody', helpers.isNecessaryBody);

Expand Down
17 changes: 17 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
isJsonMediaType : isJsonMediaType,
isXmlMediaType : isXmlMediaType,
isPdfMediaType : isPdfMediaType,
isHtmlMediaType : isHtmlMediaType,
isNecessaryBody : isNecessaryBody,
mediaTypeContainsJson : mediaTypeContainsJson,
mediaTypeContainsPdf : mediaTypeContainsPdf,
Expand Down Expand Up @@ -98,6 +99,14 @@ function isPdfMediaType(type, options) {
return mediaTypeContainsPdf(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if a mediaType is PDF
* @param {string} type content type to be evaluated
*/
function isHtmlMediaType(type, options) {
return mediaTypeContainsHtml(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if is necessary that function test generated
* have body parameter
Expand Down Expand Up @@ -141,6 +150,14 @@ function mediaTypeContainsPdf(type) {
return /\bpdf\b/i.test(type);
}

/**
* determines if the mediatype is pdf
* @param {string} type content type to be evaluated
*/
function mediaTypeContainsHtml(type) {
return /\bhtml\b/i.test(type);
}

/**
* replaces path params with obvious indicator for filling values
* (i.e. if any part of the path is surrounded in curly braces {})
Expand Down
2 changes: 1 addition & 1 deletion templates/request/delete/delete.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
done();
done();
{{/validateResponse}}
});
});
146 changes: 79 additions & 67 deletions templates/request/get/get.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -28,82 +28,94 @@
function(error, res{{#isNecessaryBody returnType noSchema}}, body{{/isNecessaryBody}}) {
if (error) return done(error);

{{#is assertion 'expect'}}
{{#if default}}
expect(res.statusCode).to.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
expect(res.statusCode).to.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'should'}}
{{#if default}}
res.statusCode.should.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
res.statusCode.should.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'assert'}}
{{#if default}}
assert.equal(res.statusCode, 'DEFAULT RESPONSE CODE HERE');
{{else}}
assert.equal(res.statusCode, {{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'expect'}}
{{#if default}}
expect(res.statusCode).to.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
expect(res.statusCode).to.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'should'}}
{{#if default}}
res.statusCode.should.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
res.statusCode.should.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'assert'}}
{{#if default}}
assert.equal(res.statusCode, 'DEFAULT RESPONSE CODE HERE');
{{else}}
assert.equal(res.statusCode, {{responseCode}});
{{/if}}
{{/is}}

{{#validateResponse returnType noSchema}}
{{#validateResponse returnType noSchema}}
{{#isJsonMediaType returnType}}
{{#is assertion 'expect'}}
expect(validator.validate(body, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(body, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(body, schema));
{{/is}}
done();
{{#is assertion 'expect'}}
expect(validator.validate(body, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(body, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(body, schema));
{{/is}}
done();
{{/isJsonMediaType}}
{{#isXmlMediaType returnType}}
var xml2jsConfig = {{xml2jsConfig}};
var xml2jsConfig = {{xml2jsConfig}};

parseString(res.text, xml2jsConfig, function(parseErr, result) {
if (parseErr) return done(parseErr);
{{#is assertion 'expect'}}
expect(validator.validate(result, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(result, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(result, schema));
{{/is}}
done();
});
parseString(res.text, xml2jsConfig, function(parseErr, result) {
if (parseErr) return done(parseErr);
{{#is assertion 'expect'}}
expect(validator.validate(result, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(result, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(result, schema));
{{/is}}
done();
});
{{/isXmlMediaType}}
{{else}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'expect'}}
expect(body).to.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
body.should.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.equal(body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'should'}}
body.should.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{#isHtmlMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.include('<!DOCTYPE html>');
{{/is}}
{{#is assertion 'should'}}
res.body.should.include('<!DOCTYPE html>');
{{/is}}
{{#is assertion 'assert'}}
assert.include(res.body, '<!DOCTYPE html>');
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'should'}}
body.should.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isHtmlMediaType}}
{{/isPdfMediaType}}
done();
{{/validateResponse}}
done();
{{/validateResponse}}
});
});
{{#if isLoadTest}}
Expand Down
126 changes: 58 additions & 68 deletions templates/request/patch/patch.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -57,82 +57,72 @@
function(error, res{{#isNecessaryBody returnType noSchema}}, body{{/isNecessaryBody}}) {
if (error) return done(error);

{{#is assertion 'expect'}}
{{#if default}}
expect(res.statusCode).to.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
expect(res.statusCode).to.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'should'}}
{{#if default}}
res.statusCode.should.equal('DEFAULT RESPONSE CODE HERE');
{{else}}
res.statusCode.should.equal({{responseCode}});
{{/if}}
{{/is}}
{{#is assertion 'assert'}}
{{#if default}}
assert.equal(res.statusCode, 'DEFAULT RESPONSE CODE HERE');
{{else}}
assert.equal(res.statusCode, {{responseCode}});
{{/if}}
{{/is}}

{{#validateResponse returnType noSchema}}
{{#validateResponse returnType noSchema}}
{{#isJsonMediaType returnType}}
{{#is assertion 'expect'}}
expect(validator.validate(body, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(body, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(body, schema));
{{/is}}
done();
{{#is assertion 'expect'}}
expect(validator.validate(body, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(body, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(body, schema));
{{/is}}
done();
{{/isJsonMediaType}}
{{#isXmlMediaType returnType}}
var xml2jsConfig = {{xml2jsConfig}};
var xml2jsConfig = {{xml2jsConfig}};

parseString(res.text, xml2jsConfig, function(parseErr, result) {
if (parseErr) return done(parseErr);
{{#is assertion 'expect'}}
expect(validator.validate(body, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(body, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(body, schema));
{{/is}}
done();
});
parseString(res.text, xml2jsConfig, function(parseErr, result) {
if (parseErr) return done(parseErr);
{{#is assertion 'expect'}}
expect(validator.validate(result, schema)).to.be.true;
{{/is}}
{{#is assertion 'should'}}
validator.validate(result, schema).should.be.true;
{{/is}}
{{#is assertion 'assert'}}
assert.true(validator.validate(result, schema));
{{/is}}
done();
});
{{/isXmlMediaType}}
{{else}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'expect'}}
expect(body).to.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
body.should.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.equal(body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'should'}}
body.should.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{#isHtmlMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.include('<!DOCTYPE html>');
{{/is}}
{{#is assertion 'should'}}
res.body.should.include('<!DOCTYPE html>');
{{/is}}
{{#is assertion 'assert'}}
assert.include(res.body, '<!DOCTYPE html>');
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'should'}}
body.should.equal(null); // non-json response or no schema
{{/is}}
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isHtmlMediaType}}
{{/isPdfMediaType}}
done();
{{/validateResponse}}
done();
{{/validateResponse}}
});
});
{{#if isLoadTest}}
Expand Down
Loading

0 comments on commit c93b2d3

Please sign in to comment.