forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update/Fix form-data/x-www-form-urlencoded snippets (#4)
* node-axios update application/form-data and x-www-form-urlencoded * node axios minor touches, node fetch placeholder path to file * javascript axios multipart/form-data remove content-type header and handle file param as a file instead of base64 * case-insensitive removeProperty helper(currently used to remove "content-type" header) * typo * node-fetch - remove 'content-type' header and add spread formData headers on multipart/form-data * update js-axios content-type header comment * reusable params helper - construct params code, checkIfRequestContainsFile general helper, use URLSearchParams for x-www-form-urlencoded, consistent code for node/js axios and fetch * "encodedParams" also in js axios * codeBuilder clone, constructAppendedParamsCode same "append" method + options argument * new helpers unit tests, mocharc file(to take only spec files) * insensitive case property remove test * use lodash isObject, check constructAppendedParamsCode test code result string * constructAppendedParamsCode options argument tests
- Loading branch information
1 parent
6e4a67e
commit fbaed97
Showing
10 changed files
with
378 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"diff": true, | ||
"extension": ["js", "cjs", "mjs"], | ||
"package": "./package.json", | ||
"reporter": "spec", | ||
"slow": "75", | ||
"timeout": "2000", | ||
"ui": "bdd", | ||
"spec": "**/*.spec.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { omit, isObject } = require("lodash"); | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param {Object} originalObject - The object from which the property needs to be removed | ||
* @param {string} propertyName - The name of the property to remove(case insensitive) | ||
* @returns the object without the property that was asked to remove | ||
*/ | ||
removeProperty: (originalObject, propertyName) => { | ||
if (!isObject(originalObject)) { | ||
throw new Error("originalObject must be an object."); | ||
} | ||
const key = Object.keys(originalObject).find( | ||
(key) => key.toLowerCase() === propertyName.toLowerCase() | ||
); | ||
if (key) { | ||
return omit(originalObject, key); | ||
} else { | ||
return originalObject; | ||
} | ||
}, | ||
|
||
checkIfRequestContainsFile: (request) => { | ||
return ( | ||
request.postData.mimeType === "multipart/form-data" && | ||
request.postData.params.some((param) => param.fileName) | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const { omit } = require("lodash"); | ||
const should = require("should"); | ||
const CodeBuilder = require("./code-builder"); | ||
|
||
const { removeProperty } = require("./general"); | ||
const { constructAppendedParamsCode } = require("./params"); | ||
|
||
describe("Test helpers methods", () => { | ||
describe("Test RemoveProperty helper", () => { | ||
it("RemoveProperty called with invalid params", () => { | ||
(function () { | ||
removeProperty("str", "property"); | ||
}.should.throw(new Error("originalObject must be an object."))); | ||
}); | ||
|
||
it("returned object stayed the same if a non existing property name sent", () => { | ||
const obj = { a: 1, b: 2 }; | ||
const result = removeProperty(obj, "c"); | ||
|
||
result.should.equal(obj); | ||
}); | ||
|
||
it("insensitive case property removed from object successfully", () => { | ||
const obj = { a: 1, b: 2 }; | ||
const result = removeProperty(obj, "B"); | ||
|
||
result.should.deepEqual(omit(obj, "b")); | ||
}); | ||
}); | ||
|
||
describe("Test constructAppendedParamsCode helper", () => { | ||
const fakeParams = [ | ||
{ name: "a", value: "1" }, | ||
{ name: "b", value: "2" }, | ||
]; | ||
|
||
it("called with invalid code argument", () => { | ||
(function () { | ||
constructAppendedParamsCode({}, []); | ||
}.should.throw( | ||
new Error("code argument must be an instance of CodeBuilder") | ||
)); | ||
}); | ||
|
||
it("called with invalid params argument", () => { | ||
(function () { | ||
constructAppendedParamsCode(new CodeBuilder(), {}); | ||
}.should.throw(new Error("params argument must be an array"))); | ||
}); | ||
|
||
describe("called with multiple options variations", () => { | ||
const fakeParamsWithFile = [ | ||
...fakeParams, | ||
{ name: "a", fileName: "fakeFileName" }, | ||
]; | ||
const lastIndex = params.length - 1; | ||
|
||
it("called with file param and false isBrowser option", () => { | ||
const result = constructAppendedParamsCode( | ||
new CodeBuilder(), | ||
fakeParamsWithFile, | ||
{ | ||
isBrowser: false, | ||
} | ||
); | ||
|
||
result.should.be.an.instanceof(CodeBuilder); | ||
result | ||
.join() | ||
.should.containEql( | ||
`fs.createReadStream("/PATH/TO/${fakeParamsWithFile[lastIndex].fileName}")` | ||
); | ||
}); | ||
|
||
it("called with file param and true isBrowser option", () => { | ||
const result = constructAppendedParamsCode( | ||
new CodeBuilder(), | ||
fakeParamsWithFile, | ||
{ | ||
isBrowser: true, | ||
} | ||
); | ||
|
||
result.should.be.an.instanceof(CodeBuilder); | ||
result | ||
.join() | ||
.should.containEql( | ||
`yourAppInput.files[0], ${JSON.stringify( | ||
params[lastIndex].fileName | ||
)}` | ||
); | ||
}); | ||
|
||
it("called with dataVarName option", () => { | ||
const result = constructAppendedParamsCode(new CodeBuilder(), params, { | ||
dataVarName: "dataObject", | ||
}); | ||
|
||
result.should.be.an.instanceof(CodeBuilder); | ||
result.join().should.containEql("dataObject.append"); | ||
}); | ||
}); | ||
|
||
it("returned new code object with two params", () => { | ||
const result = constructAppendedParamsCode(new CodeBuilder(), fakeParams); | ||
|
||
result.should.be.an.instanceof(CodeBuilder); | ||
result.getLength().should.equal(2); | ||
result | ||
.join() | ||
.should.equal('data.append("a", "1");\ndata.append("b", "2");'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { isArray } = require("lodash"); | ||
|
||
const CodeBuilder = require("./code-builder"); | ||
|
||
const defaultConstructParamsCodeOptions = { | ||
isBrowser: false, | ||
dataVarName: "data", | ||
}; | ||
|
||
module.exports = { | ||
/** | ||
* | ||
* @param {CodeBuilder} code - Original codeBuilder instance | ||
* @param {[Object]} params - List of params objects | ||
* * @param {Objects} options | ||
* @param {boolean} options.isBrowser - Boolean indicating if browser or other environment(e.g. node) | ||
* @param {string} options.dataVarName - The data object name | ||
* @returns New code instance with params appended to the supplied data object | ||
*/ | ||
constructAppendedParamsCode: ( | ||
code, | ||
params = [], | ||
options = defaultConstructParamsCodeOptions | ||
) => { | ||
if (!(code instanceof CodeBuilder)) { | ||
throw new Error("code argument must be an instance of CodeBuilder"); | ||
} else if (!isArray(params)) { | ||
throw new Error("params argument must be an array"); | ||
} | ||
|
||
const { isBrowser = false, dataVarName = "data" } = options; | ||
const newCode = code.clone(); | ||
|
||
params.forEach(function (param) { | ||
let value = | ||
param.value !== undefined ? JSON.stringify(param.value.toString()) : ""; | ||
if (param.fileName) { | ||
value = isBrowser | ||
? `yourAppInput.files[0], ${JSON.stringify(param.fileName)}` | ||
: `fs.createReadStream("/PATH/TO/${param.fileName}")`; | ||
} | ||
newCode.push( | ||
"%s.append(%s, %s);", | ||
dataVarName, | ||
JSON.stringify(param.name), | ||
value | ||
); | ||
}); | ||
|
||
return newCode; | ||
}, | ||
}; |
Oops, something went wrong.