Skip to content

Commit

Permalink
Add encode bytes missing test (#2456)
Browse files Browse the repository at this point in the history
* add missing test

* fix body parameter format

---------

Co-authored-by: qiaozha <qiaozha@microsoft.com>
  • Loading branch information
v-jiaodi and qiaozha authored Apr 19, 2024
1 parent 2ff6238 commit 54bbb85
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 53 deletions.
4 changes: 2 additions & 2 deletions packages/typespec-ts/src/modular/helpers/operationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,12 @@ function buildBodyParameter(
return bodyParameter.optional
? `body: typeof ${bodyParameter.clientName} === 'string'
? uint8ArrayToString(${bodyParameter.clientName}, "${getEncodingFormat(
bodyParameter.type
bodyParameter
)}")
: ${bodyParameter.clientName}`
: `body: uint8ArrayToString(${
bodyParameter.clientName
}, "${getEncodingFormat(bodyParameter.type)}")`;
}, "${getEncodingFormat(bodyParameter)}")`;
} else if (bodyParameter.isBinaryPayload) {
return `\nbody: ${bodyParameter.clientName},`;
}
Expand Down
1 change: 1 addition & 0 deletions packages/typespec-ts/src/modular/modularCodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface BodyParameter {
restApiName: string;
location: "body";
optional: boolean;
format?: string;
description: string;
clientName: string;
inOverload: boolean;
Expand Down
100 changes: 60 additions & 40 deletions packages/typespec-ts/test/integration/encodeBytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import EncodeBytesClientFactory, {
BytesClient
} from "./generated/encode/bytes/src/index.js";
import { buildCsvCollection } from "./generated/encode/bytes/src/serializeHelper.js";
import { uint8ArrayToString } from "@azure/core-util";
import { readFileSync } from "fs";
import { resolve } from "path";

Expand Down Expand Up @@ -71,17 +72,18 @@ describe("EncodeDatetimeClient Rest Client", () => {
assert.fail(err as string);
}
});

});

describe("property", () => {
it(`should post bytes`, async () => {
try {
const result = await client.path(`/encode/bytes/property/default`).post({
body: {
value: "dGVzdA=="
}
});
const result = await client
.path(`/encode/bytes/property/default`)
.post({
body: {
value: "dGVzdA=="
}
});
assert.strictEqual(result.status, "200");
} catch (err) {
assert.fail(err as string);
Expand All @@ -103,11 +105,13 @@ describe("EncodeDatetimeClient Rest Client", () => {

it(`should post bytes base64url encoding`, async () => {
try {
const result = await client.path(`/encode/bytes/property/base64url`).post({
body: {
value: "dGVzdA"
}
});
const result = await client
.path(`/encode/bytes/property/base64url`)
.post({
body: {
value: "dGVzdA"
}
});
assert.strictEqual(result.status, "200");
} catch (err) {
assert.fail(err as string);
Expand All @@ -128,7 +132,6 @@ describe("EncodeDatetimeClient Rest Client", () => {
assert.fail(err as string);
}
});

});

describe("header", () => {
Expand Down Expand Up @@ -185,17 +188,20 @@ describe("EncodeDatetimeClient Rest Client", () => {
assert.fail(err as string);
}
});

});

describe("request body", () => {
const pngFile = readFileSync(resolve("../../packages/typespec-ts/temp/assets/image.png"));
const pngFile = readFileSync(
resolve("../../packages/typespec-ts/temp/assets/image.png")
);
it(`should post bytes`, async () => {
try {
const result = await client.path(`/encode/bytes/body/request/default`).post({
contentType: "application/json",
body: "dGVzdA=="
});
const result = await client
.path(`/encode/bytes/body/request/default`)
.post({
contentType: "application/json",
body: "dGVzdA=="
});
assert.strictEqual(result.status, "204");
} catch (err) {
assert.fail(err as string);
Expand All @@ -204,10 +210,12 @@ describe("EncodeDatetimeClient Rest Client", () => {

it(`should post bytes base64 encoding`, async () => {
try {
const result = await client.path(`/encode/bytes/body/request/base64`).post({
contentType: "application/json",
body: "dGVzdA=="
});
const result = await client
.path(`/encode/bytes/body/request/base64`)
.post({
contentType: "application/json",
body: "dGVzdA=="
});
assert.strictEqual(result.status, "204");
} catch (err) {
assert.fail(err as string);
Expand All @@ -216,17 +224,19 @@ describe("EncodeDatetimeClient Rest Client", () => {

it(`should post bytes base64url encoding`, async () => {
try {
const result = await client.path(`/encode/bytes/body/request/base64url`).post({
contentType: "application/json",
body: "dGVzdA"
});
const result = await client
.path(`/encode/bytes/body/request/base64url`)
.post({
contentType: "application/json",
body: "dGVzdA"
});
assert.strictEqual(result.status, "204");
} catch (err) {
assert.fail(err as string);
}
});

it.skip(`should post bytes with custom content type`, async () => {
it(`should post bytes with custom content type`, async () => {
try {
const result = await client
.path(`/encode/bytes/body/request/custom-content-type`)
Expand All @@ -240,7 +250,7 @@ describe("EncodeDatetimeClient Rest Client", () => {
}
}).timeout(10000);

it.skip(`should post bytes with custom content type`, async () => {
it(`should post bytes with custom content type`, async () => {
try {
const result = await client
.path(`/encode/bytes/body/request/octet-stream`)
Expand All @@ -256,60 +266,70 @@ describe("EncodeDatetimeClient Rest Client", () => {
});

describe("response body", () => {
const pngFile = readFileSync(resolve("../../packages/typespec-ts/temp/assets/image.png"));
const pngFile = readFileSync(
resolve("../../packages/typespec-ts/temp/assets/image.png")
).toString();
it(`should get bytes with base64 encoding by default`, async () => {
try {
const result = await client.path(`/encode/bytes/body/response/default`).get();
const result = await client
.path(`/encode/bytes/body/response/default`)
.get();
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body, "dGVzdA==")
assert.strictEqual(result.body, "dGVzdA==");
} catch (err) {
assert.fail(err as string);
}
});

it(`should get bytes base64 encoding`, async () => {
try {
const result = await client.path(`/encode/bytes/body/response/base64`).get();
const result = await client
.path(`/encode/bytes/body/response/base64`)
.get();
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body, "dGVzdA==")
assert.strictEqual(result.body, "dGVzdA==");
} catch (err) {
assert.fail(err as string);
}
});

it(`should get bytes base64url encoding`, async () => {
try {
const result = await client.path(`/encode/bytes/body/response/base64url`).get();
const result = await client
.path(`/encode/bytes/body/response/base64url`)
.get();
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body, "dGVzdA")
assert.strictEqual(result.body, "dGVzdA");
} catch (err) {
assert.fail(err as string);
}
});

it.skip(`should get bytes with custom content type`, async () => {
it(`should get bytes with custom content type`, async () => {
try {
const result = await client
.path(`/encode/bytes/body/response/custom-content-type`)
.get({
contentType: "image/png"
});
// console.log(result.status, Buffer.from(result.body), pngFile);
assert.strictEqual(result.status, "200");
assert.strictEqual(Buffer.from(result.body), pngFile);
assert.strictEqual(uint8ArrayToString(result.body, "utf-8"), pngFile);
} catch (err) {
assert.fail(err as string);
}
});

it.skip(`should get bytes with octet-stream content type`, async () => {
it(`should get bytes with octet-stream content type`, async () => {
try {
const result = await client
.path(`/encode/bytes/body/response/octet-stream`)
.get({
contentType: "application/octet-stream",
contentType: "application/octet-stream"
});
// console.log(result.status, Buffer.from(result.body), pngFile);
assert.strictEqual(result.status, "200");
assert.strictEqual(Buffer.from(result.body), pngFile);
assert.strictEqual(uint8ArrayToString(result.body, "utf-8"), pngFile);
} catch (err) {
assert.fail(err as string);
}
Expand Down
20 changes: 10 additions & 10 deletions packages/typespec-ts/test/modularIntegration/encodeBytes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ describe("EncodeBytesClient Modular Client", () => {
value: stringToUint8Array("dGVzdA", "base64url")
});
assert.deepEqual(
result.value,
stringToUint8Array("dGVzdA", "base64url")
uint8ArrayToString(result.value, "base64url"),
"dGVzdA"
);
} catch (err) {
assert.fail(err as string);
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("EncodeBytesClient Modular Client", () => {
});
});

describe.skip("request body", () => {
describe("request body", () => {
const pngFile = readFileSync(
resolve("../../packages/typespec-ts/temp/assets/image.png")
);
Expand Down Expand Up @@ -217,7 +217,7 @@ describe("EncodeBytesClient Modular Client", () => {
}
});

it.skip(`should post bytes with custom content type`, async () => {
it(`should post bytes with custom content type`, async () => {
try {
const result = await client.requestBody.customContentType(pngFile, {
contentType: "image/png"
Expand All @@ -228,7 +228,7 @@ describe("EncodeBytesClient Modular Client", () => {
}
}).timeout(10000);

it.skip(`should post bytes with custom content type`, async () => {
it(`should post bytes with custom content type`, async () => {
try {
const result = await client.requestBody.octetStream(pngFile, {
contentType: "application/octet-stream"
Expand All @@ -243,7 +243,7 @@ describe("EncodeBytesClient Modular Client", () => {
describe("response body", () => {
const pngFile = readFileSync(
resolve("../../packages/typespec-ts/temp/assets/image.png")
);
).toString();
it(`should get bytes with base64 encoding by default`, async () => {
try {
const result = await client.responseBody.default();
Expand Down Expand Up @@ -271,27 +271,27 @@ describe("EncodeBytesClient Modular Client", () => {
}
});

it.skip(`should get bytes with custom content type`, async () => {
it(`should get bytes with custom content type`, async () => {
try {
const result = await client.responseBody.customContentType({
onResponse: (res) => {
res.headers.get("content-type") === "image/png";
}
});
assert.strictEqual(Buffer.from(result), pngFile);
assert.strictEqual(uint8ArrayToString(result, "utf-8"), pngFile);
} catch (err) {
assert.fail(err as string);
}
});

it.skip(`should get bytes with octet-stream content type`, async () => {
it(`should get bytes with octet-stream content type`, async () => {
try {
const result = await client.responseBody.octetStream({
onResponse: (res) => {
res.headers.get("content-type") === "application/octet-stream";
}
});
assert.strictEqual(Buffer.from(result), pngFile);
assert.strictEqual(uint8ArrayToString(result, "utf-8"), pngFile);
} catch (err) {
assert.fail(err as string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function _requestBodyBase64urlSend(
.path("/encode/bytes/body/request/base64url")
.post({
...operationOptionsToRequestParameters(options),
body: uint8ArrayToString(value, "base64"),
body: uint8ArrayToString(value, "base64url"),
});
}

Expand Down

0 comments on commit 54bbb85

Please sign in to comment.