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 default endpoint to protocol test stub #1338

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,93 +1,86 @@
import { HttpHandlerOptions, HeaderBag } from "@smithy/types";
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
import { Readable } from 'stream';
import { Readable } from "stream";

/**
* Throws an expected exception that contains the serialized request.
*/
class EXPECTED_REQUEST_SERIALIZATION_ERROR extends Error {
constructor(readonly request: HttpRequest) {
super();
}
constructor(readonly request: HttpRequest) {
super();
}
}

/**
* Throws an EXPECTED_REQUEST_SERIALIZATION_ERROR error before sending a
* request. The thrown exception contains the serialized request.
*/
class RequestSerializationTestHandler implements HttpHandler {
handle(
request: HttpRequest,
options?: HttpHandlerOptions
): Promise<{ response: HttpResponse }> {
return Promise.reject(new EXPECTED_REQUEST_SERIALIZATION_ERROR(request));
}
updateHttpClientConfig(key: never, value: never): void {}
httpHandlerConfigs() { return {}; }
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }> {
return Promise.reject(new EXPECTED_REQUEST_SERIALIZATION_ERROR(request));
}
updateHttpClientConfig(key: never, value: never): void {}
httpHandlerConfigs() {
return {};
}
}

/**
* Returns a resolved Promise of the specified response contents.
*/
class ResponseDeserializationTestHandler implements HttpHandler {
isSuccess: boolean;
code: number;
headers: HeaderBag;
body: String;

constructor(
isSuccess: boolean,
code: number,
headers?: HeaderBag,
body?: String
) {
this.isSuccess = isSuccess;
this.code = code;
if (headers === undefined) {
this.headers = {};
} else {
this.headers = headers;
}
if (body === undefined) {
body = "";
}
this.body = body;
isSuccess: boolean;
code: number;
headers: HeaderBag;
body: String;

constructor(isSuccess: boolean, code: number, headers?: HeaderBag, body?: String) {
this.isSuccess = isSuccess;
this.code = code;
if (headers === undefined) {
this.headers = {};
} else {
this.headers = headers;
}

handle(
request: HttpRequest,
options?: HttpHandlerOptions
): Promise<{ response: HttpResponse }> {
return Promise.resolve({
response: new HttpResponse({
statusCode: this.code,
headers: this.headers,
body: Readable.from([this.body])
})
});
if (body === undefined) {
body = "";
}
updateHttpClientConfig(key: never, value: never): void {}
httpHandlerConfigs() { return {}; }
this.body = body;
}

handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }> {
return Promise.resolve({
response: new HttpResponse({
statusCode: this.code,
headers: this.headers,
body: Readable.from([this.body]),
}),
});
}
updateHttpClientConfig(key: never, value: never): void {}
httpHandlerConfigs() {
return {};
}
}

interface comparableParts {
[ key: string ]: string
[key: string]: string;
}

/**
* Generates a standard map of un-equal values given input parts.
*/
const compareParts = (expectedParts: comparableParts, generatedParts: comparableParts) => {
const unequalParts: any = {};
Object.keys(expectedParts).forEach(key => {
Object.keys(expectedParts).forEach((key) => {
if (generatedParts[key] === undefined) {
unequalParts[key] = { exp: expectedParts[key], gen: undefined };
} else if (!equivalentContents(expectedParts[key], generatedParts[key])) {
unequalParts[key] = { exp: expectedParts[key], gen: generatedParts[key] };
}
});

Object.keys(generatedParts).forEach(key => {
Object.keys(generatedParts).forEach((key) => {
if (expectedParts[key] === undefined) {
unequalParts[key] = { exp: undefined, gen: generatedParts[key] };
}
Expand Down Expand Up @@ -115,10 +108,10 @@ const equivalentContents = (expected: any, generated: any): boolean => {
// If a test fails with an issue in the below 6 lines, it's likely
// due to an issue in the nestedness or existence of the property
// being compared.
delete localExpected['$$metadata'];
delete generated['$$metadata'];
Object.keys(localExpected).forEach(key => localExpected[key] === undefined && delete localExpected[key])
Object.keys(generated).forEach(key => generated[key] === undefined && delete generated[key])
delete localExpected["$$metadata"];
delete generated["$$metadata"];
Object.keys(localExpected).forEach((key) => localExpected[key] === undefined && delete localExpected[key]);
Object.keys(generated).forEach((key) => generated[key] === undefined && delete generated[key]);

const expectedProperties = Object.getOwnPropertyNames(localExpected);
const generatedProperties = Object.getOwnPropertyNames(generated);
Expand All @@ -137,17 +130,18 @@ const equivalentContents = (expected: any, generated: any): boolean => {
}

return true;
}
};

const clientParams = {
region: "us-west-2",
credentials: { accessKeyId: "key", secretAccessKey: "secret" }
}
endpoint: "https://localhost/",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only change. The rest is formatting to match TypeScript code standards present in the repository.

credentials: { accessKeyId: "key", secretAccessKey: "secret" },
};

/**
* A wrapper function that shadows `fail` from jest-jasmine2
* (jasmine2 was replaced with circus in > v27 as the default test runner)
*/
const fail = (error?: any): never => {
throw new Error(error);
}
throw new Error(error);
};
Loading