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

Fix malformed multipart body #1423

Merged
merged 18 commits into from
Oct 24, 2024
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
12 changes: 8 additions & 4 deletions packages/abstractions/src/multipartBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface MultipartEntry {
serializationCallback?: ModelSerializerFunction<Parsable>;
}

export const serializeMultipartBody = (writer: SerializationWriter, multipartBody: Partial<MultipartBody> | undefined | null = new MultipartBody()): void => {
export const serializeMultipartBody = (writer: SerializationWriter, multipartBody: Partial<MultipartBody> = new MultipartBody()): void => {
if (!writer) {
throw new Error("writer cannot be undefined");
}
Expand All @@ -117,13 +117,16 @@ export const serializeMultipartBody = (writer: SerializationWriter, multipartBod
if (first) {
first = false;
} else {
writer.writeStringValue(undefined, "");
writer.writeStringValue(undefined, "\r\n");
}
writer.writeStringValue(undefined, "--" + boundary);
writer.writeStringValue(undefined, "\r\n");
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(undefined, "");
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "\r\n");
if (typeof part.content === "string") {
writer.writeStringValue(undefined, part.content);
} else if (part.content instanceof ArrayBuffer) {
Expand All @@ -150,8 +153,9 @@ export const serializeMultipartBody = (writer: SerializationWriter, multipartBod
}
}
}
writer.writeStringValue(undefined, "");
writer.writeStringValue(undefined, "\r\n");
writer.writeStringValue(undefined, "--" + boundary + "--");
writer.writeStringValue(undefined, "\r\n");
};

export const deserializeIntoMultipartBody = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,16 @@ export class MultipartSerializationWriter implements SerializationWriter {
if (key) {
this.writeRawStringValue(key);
}
if (value !== undefined) {
if (value) {
if (key) {
this.writeRawStringValue(": ");
}
this.writeRawStringValue(value);
}
this.writeRawStringValue("\r\n");
};
private writeRawStringValue = (value?: string | null): void => {
if (value !== undefined) {
const isNullValue = value === null;

this.writeByteArrayValue(undefined, new TextEncoder().encode(isNullValue ? "null" : value).buffer);
if (value) {
this.writeByteArrayValue(undefined, new TextEncoder().encode(value).buffer);
}
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ describe("MultipartSerializationWriter", () => {
.join(""),
);
});
it("serializes multipart body with correct CRLF delimiters", () => {
const multipartSerializationWriter = new MultipartSerializationWriter();
const mpBody = new MultipartBody();
mpBody.addOrReplacePart("testPart", "text/plain", "test content");
mpBody.requestAdapter = {
getSerializationWriterFactory: () => new JsonSerializationWriterFactory(),
} as RequestAdapter;

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

const expectedString = `--${mpBody.getBoundary()}\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name="testPart"\r\n\r\ntest content\r\n--${mpBody.getBoundary()}--\r\n`;
assert.equal(result, expectedString);
});
it("writes a structured object", () => {
const testEntity = {} as TestEntity;
testEntity.id = "48d31887-5fad-4d73-a9f5-3c356e68a038";
Expand Down
Loading