Skip to content

Commit

Permalink
feat(core): all setting content-type for individual values in a formd…
Browse files Browse the repository at this point in the history
…ata request (#3316)
  • Loading branch information
RikdeVos authored Aug 20, 2024
1 parent d76844c commit 311d76d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
24 changes: 16 additions & 8 deletions packages/core/lib/engine_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,23 @@ HttpEngine.prototype.step = function step(requestSpec, ee, opts) {
requestParams.formData,
function (acc, v, k) {
let V = template(v, context);
if (V && _.isPlainObject(V) && V.fromFile) {
const absPath = path.resolve(
path.dirname(context.vars.$scenarioFile),
V.fromFile
);
fileUpload = absPath;
V = fs.createReadStream(absPath);
let options;
if (V && _.isPlainObject(V)) {
if (V.contentType) {
options = { contentType: V.contentType };
}
if (V.fromFile) {
const absPath = path.resolve(
path.dirname(context.vars.$scenarioFile),
V.fromFile
);
fileUpload = absPath;
V = fs.createReadStream(absPath);
} else if (V.value) {
V = V.value;
}
}
acc.append(k, V);
acc.append(k, V, options);
return acc;
},
f
Expand Down
21 changes: 18 additions & 3 deletions packages/core/test/unit/engine_http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,13 @@ test('HTTP engine', function (tap) {
nock('http://localhost:8888')
.post(
'/submit',
/Content-Disposition: form-data[\s\S]+activity[\s\S]+surfing/gi
(body) =>
body.match(
/Content-Disposition: form-data[\s\S]+activity[\s\S]+surfing/gi
).length &&
body.match(
/Content-Disposition: form-data[\s\S]+climate[\s\S]+Content-Type: application\/json[\s\S]+{"temperature": 25, "unit": "Celcius"}/gi
).length
)
.reply(200, 'ok');

Expand All @@ -1068,7 +1074,12 @@ test('HTTP engine', function (tap) {
formData: {
activity: '{{ activity }}',
type: '{{ type }}',
location: '{{ location }}'
location: '{{ location }}',
climate: {
value:
'{"temperature": {{ climate.temperature }}, "unit": "{{ climate.unit }}"}',
contentType: 'application/json'
}
}
}
}
Expand All @@ -1095,7 +1106,11 @@ test('HTTP engine', function (tap) {
vars: {
location: 'Lahinch',
type: 'beach',
activity: 'surfing'
activity: 'surfing',
climate: {
temperature: 25,
unit: 'Celcius'
}
}
};

Expand Down

0 comments on commit 311d76d

Please sign in to comment.