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

Supplement modular azure core basic test case #2236

Merged
merged 11 commits into from
Mar 18, 2024
19 changes: 17 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/typespec-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@azure-tools/cadl-ranch-expect": "^0.13.2",
"@azure-tools/cadl-ranch": "^0.12.4",
"chalk": "^4.0.0",
"@azure-rest/core-client": "^1.2.0",
"@azure-rest/core-client": "^1.3.0",
"@azure/core-auth": "^1.6.0",
"cross-env": "^7.0.3",
"@azure/core-paging": "^1.5.0",
Expand Down Expand Up @@ -114,4 +114,4 @@
"url": "https://github.com/Azure/autorest.typescript/issues"
},
"homepage": "https://github.com/Azure/autorest.typescript/tree/main/packages/typespec-ts/"
}
}
10 changes: 8 additions & 2 deletions packages/typespec-ts/test/integration/extensibleEnums.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ describe("ExtensibleEnums Rest Client", () => {
const result = await client
.path("/type/enum/extensible/string/known-value")
.put({
body: JSON.stringify("Monday")
body: "Monday",
headers: {
"content-type": "text/plain"
}
});
assert.strictEqual(result.status, "204");
} catch (err) {
Expand All @@ -51,7 +54,10 @@ describe("ExtensibleEnums Rest Client", () => {
const result = await client
.path("/type/enum/extensible/string/unknown-value")
.put({
body: JSON.stringify("Weekend")
body: "Weekend",
headers: {
"content-type": "text/plain"
}
});
assert.strictEqual(result.status, "204");
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-ts/test/integration/scalar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("Scalar Client", () => {
try {
const result = await client
.path("/type/scalar/string")
.put({ body: JSON.stringify("test") });
.put({ body: "test", headers: { "content-type": "text/plain" } });
assert.strictEqual(result.status, "204");
} catch (err) {
assert.fail(err as string);
Expand Down Expand Up @@ -71,7 +71,7 @@ describe("Scalar Client", () => {
try {
const result = await client
.path("/type/scalar/unknown")
.put({ body: JSON.stringify("test") });
.put({ body: "test", headers: { "content-type": "text/plain" } });
assert.strictEqual(result.status, "204");
} catch (err) {
assert.fail(err as string);
Expand Down
56 changes: 56 additions & 0 deletions packages/typespec-ts/test/modularIntegration/azureCore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,60 @@ describe("BasicClient Classical Client", () => {
assert.strictEqual(items.length, 1);
assert.strictEqual(items[0]?.name, "Madge");
});

it("should export a user", async () => {
try {
const user = await client.exportOperation(1, "json");
assert.strictEqual(user?.id, 1);
assert.strictEqual(user?.name, "Madge");
assert.strictEqual(user?.etag, "11bdc430-65e8-45ad-81d9-8ffa60d55b59");
} catch (err) {
assert.fail(err as string);
}
});

it("should create or replace a user", async () => {
try {
const user = await client.createOrReplace(
1,
{
name: "Madge",
id: 1,
etag: "11bdc430-65e8-45ad-81d9-8ffa60d55b59"
},
{
requestOptions: { headers: { "content-type": "application/json" } }
}
);
assert.strictEqual(user?.id, 1);
assert.strictEqual(user?.name, "Madge");
assert.strictEqual(user?.etag, "11bdc430-65e8-45ad-81d9-8ffa60d55b59");
} catch (err) {
assert.fail(err as string);
}
});

it("should create or update a user", async () => {
try {
const user = await client.createOrUpdate(1, {
name: "Madge",
id: 1,
etag: "11bdc430-65e8-45ad-81d9-8ffa60d55b59"
});
assert.strictEqual(user?.id, 1);
assert.strictEqual(user?.name, "Madge");
assert.strictEqual(user?.etag, "11bdc430-65e8-45ad-81d9-8ffa60d55b59");
} catch (err) {
assert.fail(err as string);
}
});

it("should delete a user", async () => {
try {
const user = await client.deleteOperation(1);
assert.isUndefined(user);
} catch (err) {
assert.fail(err as string);
}
});
});
32 changes: 28 additions & 4 deletions packages/typespec-ts/test/modularIntegration/scalar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ describe("Scalar Client", () => {

it("should get string value", async () => {
try {
const result = await client.string.get();
const result = await client.string.get({
requestOptions: {
headers: {
accept: "text/plain"
}
}
});
assert.strictEqual(result, "test");
} catch (err) {
assert.fail(err as string);
Expand All @@ -23,7 +29,13 @@ describe("Scalar Client", () => {

it("should put string value", async () => {
try {
const result = await client.string.put(JSON.stringify("test"));
const result = await client.string.put("test", {
requestOptions: {
headers: {
"content-type": "text/plain"
}
}
});
Comment on lines -26 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the previous way was working because core client rest will set the requested content type as application/json here but will pass undefined when serialize the request body here, and we will fall into the situation here which will pass the '"test"' to the cadl ranch server end,
but with the core-client-rest 1.3.0,

  1. if we keep passing '"test"' without additional content type information, we will get the into the condition here and get the content type "application/json; charset=UTF-8" and when we serialize the request body with the content type, we will try to JSON.stringify it again. server side will get a """test""".
  2. if we pass the raw string "test" without the content type, as we have decided, we should just leave the content-type as undefined here we will just send out the raw string to cadl ranch, which is wrong too.
    So we have to specify the original string, and the request content type here. and it also works if we set the content type as "application/json".

@joheredi do you think if the core logic is somewhat broken ? or the cureent logic is the right?

assert.isUndefined(result);
} catch (err) {
assert.fail(err as string);
Expand All @@ -50,7 +62,13 @@ describe("Scalar Client", () => {

it("should get unknown value", async () => {
try {
const result = await client.unknown.get();
const result = await client.unknown.get({
requestOptions: {
headers: {
accept: "text/plain"
}
}
});
assert.strictEqual(result, "test");
} catch (err) {
assert.fail(err as string);
Expand All @@ -59,7 +77,13 @@ describe("Scalar Client", () => {

it("should put unknown value", async () => {
try {
const result = await client.unknown.put(JSON.stringify("test"));
const result = await client.unknown.put("test", {
requestOptions: {
headers: {
"content-type": "text/plain"
}
}
});
assert.isUndefined(result);
} catch (err) {
assert.fail(err as string);
Expand Down
Loading