Skip to content

Commit 14ade92

Browse files
author
dhwaneetbhatt
committed
Read followRedirect also from request settings if present
1 parent 597225e commit 14ade92

File tree

2 files changed

+121
-17
lines changed

2 files changed

+121
-17
lines changed

codegens/curl/lib/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,13 @@ var self = module.exports = {
244244
* @returns {Boolean}
245245
*/
246246
shouldAddHttpMethod: function (request, options) {
247-
const followOriginalHttpMethod =
248-
options.followOriginalHttpMethod || _.get(request, 'protocolProfileBehavior.followOriginalHttpMethod', false),
247+
const followRedirect = _.get(request, 'protocolProfileBehavior.followRedirects', options.followRedirect),
248+
followOriginalHttpMethod =
249+
_.get(request, 'protocolProfileBehavior.followOriginalHttpMethod', options.followOriginalHttpMethod),
249250
disableBodyPruning = _.get(request, 'protocolProfileBehavior.disableBodyPruning', true),
250251
isBodyEmpty = self.isBodyEmpty(request.body);
251252

252-
if (options.followRedirect && followOriginalHttpMethod) {
253+
if (followRedirect && followOriginalHttpMethod) {
253254
return true;
254255
}
255256

codegens/curl/test/unit/convert.test.js

Lines changed: 117 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,15 @@ describe('curl convert function', function () {
767767
});
768768
});
769769

770-
it('should add --request parameter when options ' +
771-
'followRedirect and followOriginalHttpMethod are true', function () {
772-
const methods = ['GET', 'HEAD', 'DELETE', 'PUT', 'POST', 'PATCH'],
773-
request = new sdk.Request({
770+
describe('followRedirect and followOriginalHttpMethod', function () {
771+
it('should add --request parameter when passed true via options', function () {
772+
const request = new sdk.Request({
774773
'method': 'POST',
775774
'header': [],
776775
'body': {
777776
'mode': 'graphql',
778777
'graphql': {
779-
'query': '{\n findScenes(\n filter: {per_page: 0}\n scene_filter: {is_missing: "performers"}){\n count\n scenes {\n id\n title\n path\n }\n }\n}', // eslint-disable-line
778+
'query': '{\n findScenes(\n filter: {per_page: 0}\n scene_filter: {is_missing: "performers"}){\n count\n scenes {\n id\n title\n path\n }\n }\n}', // eslint-disable-line
780779
'variables': '{\n\t"variable_key": "variable_value"\n}'
781780
}
782781
},
@@ -793,21 +792,125 @@ describe('curl convert function', function () {
793792
}
794793
});
795794

796-
// this needs to be done here because protocolProfileBehavior is not in collections SDK
797-
request.protocolProfileBehavior = {
798-
followOriginalHttpMethod: true
799-
};
795+
convert(request, { followRedirect: true, followOriginalHttpMethod: true }, function (error, snippet) {
796+
if (error) {
797+
expect.fail(null, null, error);
798+
}
799+
expect(snippet).to.be.a('string');
800+
expect(snippet).to.include('--request POST');
801+
});
802+
});
803+
804+
it('should not add --request parameter when passed false via options', function () {
805+
const request = new sdk.Request({
806+
'method': 'POST',
807+
'header': [],
808+
'body': {
809+
'mode': 'graphql',
810+
'graphql': {
811+
'query': '{\n findScenes(\n filter: {per_page: 0}\n scene_filter: {is_missing: "performers"}){\n count\n scenes {\n id\n title\n path\n }\n }\n}', // eslint-disable-line
812+
'variables': '{\n\t"variable_key": "variable_value"\n}'
813+
}
814+
},
815+
'url': {
816+
'raw': 'https://postman-echo.com/post',
817+
'protocol': 'https',
818+
'host': [
819+
'postman-echo',
820+
'com'
821+
],
822+
'path': [
823+
'post'
824+
]
825+
}
826+
});
827+
828+
convert(request, { followRedirect: false, followOriginalHttpMethod: false }, function (error, snippet) {
829+
if (error) {
830+
expect.fail(null, null, error);
831+
}
832+
expect(snippet).to.be.a('string');
833+
expect(snippet).to.not.include('--request POST');
834+
});
835+
});
836+
837+
it('should add --request parameter when passed false via options but true in request settings', function () {
838+
const request = new sdk.Request({
839+
'method': 'POST',
840+
'header': [],
841+
'body': {
842+
'mode': 'graphql',
843+
'graphql': {
844+
'query': '{\n findScenes(\n filter: {per_page: 0}\n scene_filter: {is_missing: "performers"}){\n count\n scenes {\n id\n title\n path\n }\n }\n}', // eslint-disable-line
845+
'variables': '{\n\t"variable_key": "variable_value"\n}'
846+
}
847+
},
848+
'url': {
849+
'raw': 'https://postman-echo.com/post',
850+
'protocol': 'https',
851+
'host': [
852+
'postman-echo',
853+
'com'
854+
],
855+
'path': [
856+
'post'
857+
]
858+
}
859+
});
860+
861+
// this needs to be done here because protocolProfileBehavior is not in collections SDK
862+
request.protocolProfileBehavior = {
863+
followRedirects: true,
864+
followOriginalHttpMethod: true
865+
};
800866

801-
for (let method of methods) {
802-
request.method = method;
803-
convert(request, { followRedirect: true }, function (error, snippet) {
867+
convert(request, { followRedirect: false, followOriginalHttpMethod: false }, function (error, snippet) {
804868
if (error) {
805869
expect.fail(null, null, error);
806870
}
807871
expect(snippet).to.be.a('string');
808-
expect(snippet).to.include(`--request ${method}`);
872+
expect(snippet).to.include('--request POST');
809873
});
810-
}
874+
});
875+
876+
it('should not add --request parameter when passed true via options but false in request settings', function () {
877+
const request = new sdk.Request({
878+
'method': 'POST',
879+
'header': [],
880+
'body': {
881+
'mode': 'graphql',
882+
'graphql': {
883+
'query': '{\n findScenes(\n filter: {per_page: 0}\n scene_filter: {is_missing: "performers"}){\n count\n scenes {\n id\n title\n path\n }\n }\n}', // eslint-disable-line
884+
'variables': '{\n\t"variable_key": "variable_value"\n}'
885+
}
886+
},
887+
'url': {
888+
'raw': 'https://postman-echo.com/post',
889+
'protocol': 'https',
890+
'host': [
891+
'postman-echo',
892+
'com'
893+
],
894+
'path': [
895+
'post'
896+
]
897+
}
898+
});
899+
900+
// this needs to be done here because protocolProfileBehavior is not in collections SDK
901+
request.protocolProfileBehavior = {
902+
followRedirects: false,
903+
followOriginalHttpMethod: false
904+
};
905+
906+
convert(request, { followRedirect: true, followOriginalHttpMethod: true }, function (error, snippet) {
907+
if (error) {
908+
expect.fail(null, null, error);
909+
}
910+
expect(snippet).to.be.a('string');
911+
expect(snippet).to.not.include('--request POST');
912+
});
913+
});
811914
});
812915
});
813916
});

0 commit comments

Comments
 (0)