Skip to content
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

feature/issue 1582 #1591

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/abstractions/src/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ export class MultipartBody implements Parsable {
* @param partContentType the content type of the part to add or replace.
* @param content the content of the part to add or replace.
* @param serializationCallback the serialization callback to use when serializing the part.
* @param fileName the name of the file associated with this part.
*/
public addOrReplacePart<T>(partName: string, partContentType: string, content: T, serializationCallback?: ModelSerializerFunction<Parsable>): void {
public addOrReplacePart<T>(partName: string, partContentType: string, content: T, serializationCallback?: ModelSerializerFunction<Parsable>, fileName?: string): void {
if (!partName) throw new Error("partName cannot be undefined");
if (!partContentType) {
throw new Error("partContentType cannot be undefined");
Expand All @@ -39,6 +40,7 @@ export class MultipartBody implements Parsable {
contentType: partContentType,
content,
originalName: partName,
fileName,
serializationCallback,
};
}
Expand Down Expand Up @@ -90,6 +92,7 @@ interface MultipartEntry {
contentType: string;
content: any;
originalName: string;
fileName?: string;
serializationCallback?: ModelSerializerFunction<Parsable>;
}

Expand Down Expand Up @@ -124,7 +127,7 @@ export const serializeMultipartBody = (writer: SerializationWriter, multipartBod
const part = parts[partName];
writer.writeStringValue("Content-Type", part.contentType);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue("Content-Disposition", 'form-data; name="' + part.originalName + '"');
writer.writeStringValue("Content-Disposition", `form-data; name="${part.originalName}"${part.fileName ? `; filename="${part.fileName}"` : ""}`);
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "\r\n");
if (typeof part.content === "string") {
Expand Down
28 changes: 28 additions & 0 deletions packages/abstractions/test/common/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,31 @@ describe("multipartBody", () => {
});
//serialize method is tested in the serialization library
});

describe("multipartBody with fileName", () => {
const arrayBuffer = new Uint8Array([0x01, 0x01, 0x01]);
const arrayBuffer2 = new Uint8Array([0x02, 0x02, 0x02]);

it("adds parts with a filename", () => {
const mpBody = new MultipartBody();
mpBody.addOrReplacePart("filepart", "application/octet-stream", arrayBuffer, undefined, "file.txt");

const part = mpBody.listParts()["filepart"];
assert.exists(part);
assert.strictEqual(part.contentType, "application/octet-stream");
assert.strictEqual(part.content, arrayBuffer);
assert.strictEqual(part.fileName, "file.txt");
});

it("updates filename when replacing a part", () => {
const mpBody = new MultipartBody();
mpBody.addOrReplacePart("filepart", "application/octet-stream", arrayBuffer, undefined, "file.txt");
mpBody.addOrReplacePart("filepart", "application/pdf", arrayBuffer2, undefined, "document.pdf");

const part = mpBody.listParts()["filepart"];
assert.exists(part);
assert.strictEqual(part.fileName, "document.pdf");
assert.strictEqual(part.content, arrayBuffer2);
assert.strictEqual(part.contentType, "application/pdf");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,17 @@ describe("MultipartSerializationWriter", () => {
const multipartSerializationWriter = new MultipartSerializationWriter();
assert.throw(() => multipartSerializationWriter.writeCollectionOfObjectValues(undefined, [testEntity]));
});

it("serializes Content-Disposition with filename", () => {
const mpBody = new MultipartBody();
mpBody.addOrReplacePart("file", "application/octet-stream", byteForTest, undefined, "file.txt");

const multipartSerializationWriter = new MultipartSerializationWriter();
multipartSerializationWriter.writeObjectValue(undefined, mpBody, serializeMultipartBody);
const multipartContent = multipartSerializationWriter.getSerializedContent();
const result = new TextDecoder().decode(multipartContent);

const expectedString = "--" + mpBody.getBoundary() + '\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name="file"; filename="file.txt"\r\n\r\n' + new TextDecoder().decode(byteForTest) + "\r\n--" + mpBody.getBoundary() + "--\r\n";
assert.equal(result, expectedString);
});
});
Loading