Skip to content
59 changes: 59 additions & 0 deletions packages/typespec-ts/test/azureIntegration/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ describe("MultiPartClient Rest Client", () => {
assert.strictEqual(result.status, "204");
});

it("should support wire name", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/mixed-parts-with-wire-name")
.post({
contentType: "multipart/form-data",
body: [
// Using 'as any' because generated types use TypeSpec property names ('identifier', 'image')
// but runtime expects wire names ('id', 'profileImage') per TypeSpec @name annotation
{ name: "id" as any, body: "123" },
{
name: "profileImage" as any,
body: file,
filename: "profileImage.jpg"
}
]
});
assert.strictEqual(result.status, "204");
});

it("supports anonymous model file upload", async () => {
const result = await client
.path("/multipart/form-data/anonymous-model")
Expand All @@ -50,6 +70,45 @@ describe("MultiPartClient Rest Client", () => {
});
});

describe("optional parts", () => {
it("should support id only", async () => {
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [{ name: "id", body: "123" }]
});
assert.strictEqual(result.status, "204");
});

it("should support profileImage only", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [
{ name: "profileImage", body: file, filename: "profileImage.jpg" }
]
});
assert.strictEqual(result.status, "204");
});

it("should support both id and profileImage", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [
{ name: "id", body: "123" },
{ name: "profileImage", body: file, filename: "profileImage.jpg" }
]
});
assert.strictEqual(result.status, "204");
});
});

describe("custom content type + filename", () => {
it("raises 400 error when filename and MIME type unspecified", async () => {
const file = await readFile(imgPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ describe("Multipart Client", () => {
});
});

it("multipart request with wire name", async () => {
await client.formData.withWireName({
identifier: "123",
image: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

it("optional parts - id only", async () => {
await client.formData.optionalParts({
id: "123"
});
});

it("optional parts - profileImage only", async () => {
await client.formData.optionalParts({
profileImage: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

it("optional parts - both id and profileImage", async () => {
await client.formData.optionalParts({
id: "123",
profileImage: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

// TODO not supported
it.skip("anonymous model", async () => {
// @ts-ignore - not supported yet
Expand Down
59 changes: 59 additions & 0 deletions packages/typespec-ts/test/integration/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ describe("MultiPartClient Rest Client", () => {
assert.strictEqual(result.status, "204");
});

it("should support wire name", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/mixed-parts-with-wire-name")
.post({
contentType: "multipart/form-data",
body: [
// Using 'as any' because generated types use TypeSpec property names ('identifier', 'image')
// but runtime expects wire names ('id', 'profileImage') per TypeSpec @name annotation
{ name: "id" as any, body: "123" },
{
name: "profileImage" as any,
body: file,
filename: "profileImage.jpg"
}
]
});
assert.strictEqual(result.status, "204");
});

it("supports anonymous model file upload", async () => {
const result = await client
.path("/multipart/form-data/anonymous-model")
Expand All @@ -51,6 +71,45 @@ describe("MultiPartClient Rest Client", () => {
});
});

describe("optional parts", () => {
it("should support id only", async () => {
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [{ name: "id", body: "123" }]
});
assert.strictEqual(result.status, "204");
});

it("should support profileImage only", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [
{ name: "profileImage", body: file, filename: "profileImage.jpg" }
]
});
assert.strictEqual(result.status, "204");
});

it("should support both id and profileImage", async () => {
const file = await readFile(imgPath);
const result = await client
.path("/multipart/form-data/optional-parts")
.post({
contentType: "multipart/form-data",
body: [
{ name: "id", body: "123" },
{ name: "profileImage", body: file, filename: "profileImage.jpg" }
]
});
assert.strictEqual(result.status, "204");
});
});

describe("custom content type + filename", () => {
it("raises 400 error when filename and MIME type unspecified", async () => {
const file = await readFile(imgPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ describe("Multipart Client", () => {
});
});

it("multipart request with wire name", async () => {
await client.formData.withWireName({
identifier: "123",
image: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

it("optional parts - id only", async () => {
await client.formData.optionalParts({
id: "123"
});
});

it("optional parts - profileImage only", async () => {
await client.formData.optionalParts({
profileImage: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

it("optional parts - both id and profileImage", async () => {
await client.formData.optionalParts({
id: "123",
profileImage: {
contents: fs.createReadStream(imgPath),
filename: "test.jpg"
}
});
});

// TODO not supported
it.skip("anonymous model", async () => {
// @ts-ignore - not supported yet
Expand Down
Loading