Skip to content

Commit ac1677e

Browse files
authored
add default endpoint to protocol test stub (#1338)
1 parent 11b9cd9 commit ac1677e

File tree

1 file changed

+54
-60
lines changed
  • smithy-typescript-codegen/src/main/resources/software/amazon/smithy/typescript/codegen

1 file changed

+54
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,86 @@
11
import { HttpHandlerOptions, HeaderBag } from "@smithy/types";
22
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
3-
import { Readable } from 'stream';
3+
import { Readable } from "stream";
44

55
/**
66
* Throws an expected exception that contains the serialized request.
77
*/
88
class EXPECTED_REQUEST_SERIALIZATION_ERROR extends Error {
9-
constructor(readonly request: HttpRequest) {
10-
super();
11-
}
9+
constructor(readonly request: HttpRequest) {
10+
super();
11+
}
1212
}
1313

1414
/**
1515
* Throws an EXPECTED_REQUEST_SERIALIZATION_ERROR error before sending a
1616
* request. The thrown exception contains the serialized request.
1717
*/
1818
class RequestSerializationTestHandler implements HttpHandler {
19-
handle(
20-
request: HttpRequest,
21-
options?: HttpHandlerOptions
22-
): Promise<{ response: HttpResponse }> {
23-
return Promise.reject(new EXPECTED_REQUEST_SERIALIZATION_ERROR(request));
24-
}
25-
updateHttpClientConfig(key: never, value: never): void {}
26-
httpHandlerConfigs() { return {}; }
19+
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }> {
20+
return Promise.reject(new EXPECTED_REQUEST_SERIALIZATION_ERROR(request));
21+
}
22+
updateHttpClientConfig(key: never, value: never): void {}
23+
httpHandlerConfigs() {
24+
return {};
25+
}
2726
}
2827

2928
/**
3029
* Returns a resolved Promise of the specified response contents.
3130
*/
3231
class ResponseDeserializationTestHandler implements HttpHandler {
33-
isSuccess: boolean;
34-
code: number;
35-
headers: HeaderBag;
36-
body: String;
37-
38-
constructor(
39-
isSuccess: boolean,
40-
code: number,
41-
headers?: HeaderBag,
42-
body?: String
43-
) {
44-
this.isSuccess = isSuccess;
45-
this.code = code;
46-
if (headers === undefined) {
47-
this.headers = {};
48-
} else {
49-
this.headers = headers;
50-
}
51-
if (body === undefined) {
52-
body = "";
53-
}
54-
this.body = body;
32+
isSuccess: boolean;
33+
code: number;
34+
headers: HeaderBag;
35+
body: String;
36+
37+
constructor(isSuccess: boolean, code: number, headers?: HeaderBag, body?: String) {
38+
this.isSuccess = isSuccess;
39+
this.code = code;
40+
if (headers === undefined) {
41+
this.headers = {};
42+
} else {
43+
this.headers = headers;
5544
}
56-
57-
handle(
58-
request: HttpRequest,
59-
options?: HttpHandlerOptions
60-
): Promise<{ response: HttpResponse }> {
61-
return Promise.resolve({
62-
response: new HttpResponse({
63-
statusCode: this.code,
64-
headers: this.headers,
65-
body: Readable.from([this.body])
66-
})
67-
});
45+
if (body === undefined) {
46+
body = "";
6847
}
69-
updateHttpClientConfig(key: never, value: never): void {}
70-
httpHandlerConfigs() { return {}; }
48+
this.body = body;
49+
}
50+
51+
handle(request: HttpRequest, options?: HttpHandlerOptions): Promise<{ response: HttpResponse }> {
52+
return Promise.resolve({
53+
response: new HttpResponse({
54+
statusCode: this.code,
55+
headers: this.headers,
56+
body: Readable.from([this.body]),
57+
}),
58+
});
59+
}
60+
updateHttpClientConfig(key: never, value: never): void {}
61+
httpHandlerConfigs() {
62+
return {};
63+
}
7164
}
7265

7366
interface comparableParts {
74-
[ key: string ]: string
67+
[key: string]: string;
7568
}
7669

7770
/**
7871
* Generates a standard map of un-equal values given input parts.
7972
*/
8073
const compareParts = (expectedParts: comparableParts, generatedParts: comparableParts) => {
8174
const unequalParts: any = {};
82-
Object.keys(expectedParts).forEach(key => {
75+
Object.keys(expectedParts).forEach((key) => {
8376
if (generatedParts[key] === undefined) {
8477
unequalParts[key] = { exp: expectedParts[key], gen: undefined };
8578
} else if (!equivalentContents(expectedParts[key], generatedParts[key])) {
8679
unequalParts[key] = { exp: expectedParts[key], gen: generatedParts[key] };
8780
}
8881
});
8982

90-
Object.keys(generatedParts).forEach(key => {
83+
Object.keys(generatedParts).forEach((key) => {
9184
if (expectedParts[key] === undefined) {
9285
unequalParts[key] = { exp: undefined, gen: generatedParts[key] };
9386
}
@@ -115,10 +108,10 @@ const equivalentContents = (expected: any, generated: any): boolean => {
115108
// If a test fails with an issue in the below 6 lines, it's likely
116109
// due to an issue in the nestedness or existence of the property
117110
// being compared.
118-
delete localExpected['$$metadata'];
119-
delete generated['$$metadata'];
120-
Object.keys(localExpected).forEach(key => localExpected[key] === undefined && delete localExpected[key])
121-
Object.keys(generated).forEach(key => generated[key] === undefined && delete generated[key])
111+
delete localExpected["$$metadata"];
112+
delete generated["$$metadata"];
113+
Object.keys(localExpected).forEach((key) => localExpected[key] === undefined && delete localExpected[key]);
114+
Object.keys(generated).forEach((key) => generated[key] === undefined && delete generated[key]);
122115

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

139132
return true;
140-
}
133+
};
141134

142135
const clientParams = {
143136
region: "us-west-2",
144-
credentials: { accessKeyId: "key", secretAccessKey: "secret" }
145-
}
137+
endpoint: "https://localhost/",
138+
credentials: { accessKeyId: "key", secretAccessKey: "secret" },
139+
};
146140

147141
/**
148142
* A wrapper function that shadows `fail` from jest-jasmine2
149143
* (jasmine2 was replaced with circus in > v27 as the default test runner)
150144
*/
151145
const fail = (error?: any): never => {
152-
throw new Error(error);
153-
}
146+
throw new Error(error);
147+
};

0 commit comments

Comments
 (0)