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

Commit

Permalink
add feature to testgen for pdf media type (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
SrHuevo authored and noahdietz committed May 5, 2018
1 parent 3f30165 commit be8a957
Show file tree
Hide file tree
Showing 24 changed files with 1,446 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ handlebars.registerHelper('printJSON', helpers.printJSON);
handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter);
handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation);
handlebars.registerHelper('isJsonMediaType', helpers.isJsonMediaType);
handlebars.registerHelper('isPdfMediaType', helpers.isPdfMediaType);


module.exports = {
Expand Down
18 changes: 18 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ module.exports = {
printJSON: printJSON,
len: len,
setLen: setLen,
isPdfMediaType : isPdfMediaType,
requestDataParamFormatter : requestDataParamFormatter,
isJsonRepresentation : isJsonRepresentation,
isJsonMediaType : isJsonMediaType,
mediaTypeContainsPdf : mediaTypeContainsPdf,
mediaTypeContainsJson : mediaTypeContainsJson
};

Expand Down Expand Up @@ -77,6 +79,14 @@ function isJsonMediaType(type, options) {
return mediaTypeContainsJson(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 isPdfMediaType(type, options) {
return mediaTypeContainsPdf(type) ? options.fn(this) : options.inverse(this);
}

/**
* decides if this request/response has a JSON representation
* @param {string} contentType the media type of the request
Expand All @@ -94,6 +104,14 @@ function mediaTypeContainsJson(type) {
return /\bjson\b/i.test(type);
}

/**
* determines if the mediatype is pdf
* @param {string} type content type to be evaluated
*/
function mediaTypeContainsPdf(type) {
return /\bpdf\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
12 changes: 12 additions & 0 deletions templates/request/get/get.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
assert.true(validator.validate(body, schema));
{{/is}}
{{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}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -70,6 +81,7 @@
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/request/patch/patch.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
assert.true(validator.validate(body, schema));
{{/is}}
{{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}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -99,6 +110,7 @@
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/request/post/post.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@
assert.true(validator.validate(body, schema));
{{/is}}
{{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}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -97,6 +108,7 @@
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/request/put/put.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@
assert.true(validator.validate(body, schema));
{{/is}}
{{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}}
{{else}}
{{#is assertion 'expect'}}
expect(body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -97,6 +108,7 @@
{{#is assertion 'assert'}}
assert.isNull(body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/supertest/get/get.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
assert.true(validator.validate(res.body, schema));
{{/is}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
res.body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(res.body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(res.body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -55,6 +66,7 @@
{{#is assertion 'assert'}}
assert.isNull(res.body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/supertest/patch/patch.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@
assert.true(validator.validate(res.body, schema));
{{/is}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
res.body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(res.body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(res.body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -80,6 +91,7 @@
{{#is assertion 'assert'}}
assert.isNull(res.body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/supertest/post/post.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
assert.true(validator.validate(res.body, schema));
{{/is}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
res.body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(res.body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(res.body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -79,6 +90,7 @@
{{#is assertion 'assert'}}
assert.isNull(res.body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
12 changes: 12 additions & 0 deletions templates/supertest/put/put.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
assert.true(validator.validate(res.body, schema));
{{/is}}
{{else}}
{{#isPdfMediaType returnType}}
{{#is assertion 'expect'}}
expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'should'}}
res.body.should.deep.equal(new Buffer(Number(res.header['content-length'])));
{{/is}}
{{#is assertion 'assert'}}
assert.deepEqual(res.body, new Buffer(Number(res.header['content-length'])));
{{/is}}
{{else}}
{{#is assertion 'expect'}}
expect(res.body).to.equal(null); // non-json response or no schema
{{/is}}
Expand All @@ -79,6 +90,7 @@
{{#is assertion 'assert'}}
assert.isNull(res.body); // non-json response or no schema
{{/is}}
{{/isPdfMediaType}}
{{/validateResponse}}
done();
});
Expand Down
4 changes: 4 additions & 0 deletions test/pdfConsumeTypeTest/compare/request/assert/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OAUTH=YOUR_TOKEN_GOES_HERE
KEY=YOUR_TOKEN_GOES_HERE
BASIC_AUTH=YOUR_TOKEN_GOES_HERE

Loading

0 comments on commit be8a957

Please sign in to comment.