forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 4
Update/Fix form-data/x-www-form-urlencoded snippets #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
43b0c18
node-axios update application/form-data and x-www-form-urlencoded
saharbechor 5ca8d63
node axios minor touches,
saharbechor 83e1450
javascript axios multipart/form-data remove content-type header and h…
saharbechor b972442
case-insensitive removeProperty helper(currently used to remove "cont…
saharbechor ffec314
typo
saharbechor 9120dba
node-fetch - remove 'content-type' header and add spread formData hea…
saharbechor 0befb34
update js-axios content-type header comment
saharbechor ff751fb
reusable params helper - construct params code,
saharbechor b34d38b
"encodedParams" also in js axios
saharbechor b0bf0dd
codeBuilder clone,
saharbechor 1eb6d8f
new helpers unit tests,
saharbechor a05c62c
insensitive case property remove test
saharbechor 53d2b87
use lodash isObject,
saharbechor 82b2955
constructAppendedParamsCode options argument tests
saharbechor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
saharbechor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result | ||
.join() | ||
.should.equal('data.append("a", "1");\ndata.append("b", "2");'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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) { | ||
saharbechor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.