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

Add rlc coverage cases for payload media type in cadl-ranch #2265

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions packages/typespec-ts/test/commands/cadl-ranch-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export const rlcTsps: TypeSpecRanchConfig[] = [
outputPath: "payload/pageable",
inputPath: "payload/pageable"
},
{
outputPath: "payload/media-type",
inputPath: "payload/media-type"
},
{
outputPath: "client/naming",
inputPath: "client/naming"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import {
StringBodySendAsTextParameters,
StringBodyGetAsTextParameters,
StringBodySendAsJsonParameters,
StringBodyGetAsJsonParameters,
} from "./parameters.js";
import {
StringBodySendAsText200Response,
StringBodyGetAsText200Response,
StringBodySendAsJson200Response,
StringBodyGetAsJson200Response,
} from "./responses.js";
import { Client, StreamableMethod } from "@azure-rest/core-client";

export interface SendAsText {
post(
options: StringBodySendAsTextParameters,
): StreamableMethod<StringBodySendAsText200Response>;
}

export interface GetAsText {
get(
options?: StringBodyGetAsTextParameters,
): StreamableMethod<StringBodyGetAsText200Response>;
}

export interface SendAsJson {
post(
options: StringBodySendAsJsonParameters,
): StreamableMethod<StringBodySendAsJson200Response>;
}

export interface GetAsJson {
get(
options?: StringBodyGetAsJsonParameters,
): StreamableMethod<StringBodyGetAsJson200Response>;
}

export interface Routes {
/** Resource for '/payload/media-type/string-body/sendAsText' has methods for the following verbs: post */
(path: "/payload/media-type/string-body/sendAsText"): SendAsText;
/** Resource for '/payload/media-type/string-body/getAsText' has methods for the following verbs: get */
(path: "/payload/media-type/string-body/getAsText"): GetAsText;
/** Resource for '/payload/media-type/string-body/sendAsJson' has methods for the following verbs: post */
(path: "/payload/media-type/string-body/sendAsJson"): SendAsJson;
/** Resource for '/payload/media-type/string-body/getAsJson' has methods for the following verbs: get */
(path: "/payload/media-type/string-body/getAsJson"): GetAsJson;
}

export type MediaTypeClient = Client & {
path: Routes;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import MediaTypeClient from "./mediaTypeClient.js";

export * from "./mediaTypeClient.js";
export * from "./parameters.js";
export * from "./responses.js";
export * from "./clientDefinitions.js";

export default MediaTypeClient;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { createClientLogger } from "@azure/logger";
export const logger = createClientLogger("payload-media-type");
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { getClient, ClientOptions } from "@azure-rest/core-client";
import { logger } from "./logger.js";
import { MediaTypeClient } from "./clientDefinitions.js";

/**
* Initialize a new instance of `MediaTypeClient`
* @param options - the parameter for all optional parameters
*/
export default function createClient(
options: ClientOptions = {},
): MediaTypeClient {
const endpointUrl =
options.endpoint ?? options.baseUrl ?? `http://localhost:3000`;
const userAgentInfo = `azsdk-js-payload-media-type-rest/1.0.0-beta.1`;
const userAgentPrefix =
options.userAgentOptions && options.userAgentOptions.userAgentPrefix
? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`
: `${userAgentInfo}`;
options = {
...options,
userAgentOptions: {
userAgentPrefix,
},
loggingOptions: {
logger: options.loggingOptions?.logger ?? logger.info,
},
};

const client = getClient(endpointUrl, options) as MediaTypeClient;

client.pipeline.removePolicy({ name: "ApiVersionPolicy" });
return client;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { RequestParameters } from "@azure-rest/core-client";

export interface StringBodySendAsTextBodyParam {
body: string;
}

export interface StringBodySendAsTextMediaTypesParam {
contentType: "text/plain";
}

export type StringBodySendAsTextParameters =
StringBodySendAsTextMediaTypesParam &
StringBodySendAsTextBodyParam &
RequestParameters;
export type StringBodyGetAsTextParameters = RequestParameters;

export interface StringBodySendAsJsonBodyParam {
body: string;
}

export interface StringBodySendAsJsonMediaTypesParam {
contentType: "application/json";
}

export type StringBodySendAsJsonParameters =
StringBodySendAsJsonMediaTypesParam &
StringBodySendAsJsonBodyParam &
RequestParameters;
export type StringBodyGetAsJsonParameters = RequestParameters;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpResponse } from "@azure-rest/core-client";

/** The request has succeeded. */
export interface StringBodySendAsText200Response extends HttpResponse {
status: "200";
}

/** The request has succeeded. */
export interface StringBodyGetAsText200Response extends HttpResponse {
status: "200";
body: string;
}

/** The request has succeeded. */
export interface StringBodySendAsJson200Response extends HttpResponse {
status: "200";
}

/** The request has succeeded. */
export interface StringBodyGetAsJson200Response extends HttpResponse {
status: "200";
body: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
emit:
- "@azure-tools/typespec-ts"
options:
"@azure-tools/typespec-ts":
"emitter-output-dir": "{project-root}"
generateMetadata: false
generateTest: false
addCredentials: false
azureSdkForJs: false
isTypeSpecTest: true
enableOperationGroup: true
packageDetails:
name: "@msinternal/payload-media-type"
63 changes: 63 additions & 0 deletions packages/typespec-ts/test/integration/payloadMediaType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { assert } from "chai";
import MediaTypeClientFactory, {
MediaTypeClient
} from "./generated/payload/media-type/src/index.js";

describe("MediaType Client", () => {
let client: MediaTypeClient;

beforeEach(() => {
client = MediaTypeClientFactory({
allowInsecureConnection: true,
retryOptions: {
maxRetries: 0
}
v-jiaodi marked this conversation as resolved.
Show resolved Hide resolved
});
});

it("should getAsText", async () => {
try {
const result = await client
.path("/payload/media-type/string-body/getAsText")
.get({ accept: "text/plain" });
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body, "{cat}");
} catch (err) {
assert.fail(err as string);
}
});

it("should sendAsText", async () => {
try {
const result = await client
.path("/payload/media-type/string-body/sendAsText")
.post({ body: "{cat}", contentType: "text/plain" });
assert.strictEqual(result.status, "200");
} catch (err) {
assert.fail(err as string);
}
});

it("should sendAsJson", async () => {
try {
const result = await client
.path("/payload/media-type/string-body/sendAsJson")
.post({ body: "foo", contentType: "application/json" });
assert.strictEqual(result.status, "200");
} catch (err) {
assert.fail(err as string);
}
});

it("should getAsJson", async () => {
try {
const result = await client
.path("/payload/media-type/string-body/getAsJson")
.get({ accept: "application/json" });
assert.strictEqual(result.status, "200");
assert.strictEqual(result.body, "foo");
} catch (err) {
assert.fail(err as string);
}
});
});
Loading