-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathpaths.ts
170 lines (154 loc) · 5.01 KB
/
paths.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { app, ValidationError, json } from "../api";
import { coverageService } from "../services";
import { coerceDateString } from "../utils";
const Constants = {
DEFAULT_SERVER_PORT: "3000",
MULTIBYTE_BUFFER: "啊齄丂狛狜隣郎隣兀﨩",
MULTIBYTE_BUFFER_BODY:
"啊齄丂狛狜隣郎隣兀﨩ˊ〞〡¦℡㈱‐ー﹡﹢﹫、〓ⅰⅹ⒈€㈠㈩ⅠⅫ! ̄ぁんァヶΑ︴АЯаяāɡㄅㄩ─╋︵﹄︻︱︳︴ⅰⅹɑɡ〇〾⿻⺁䜣€",
};
const scenarios = {
bool: {
name: "Bool",
scenarios: {
true: "True",
false: "False",
},
},
int: {
name: "Int",
scenarios: {
"1000000": { name: "Positive", expectedValue: 1000000 },
"-1000000": { name: "Negative", expectedValue: -1000000 },
"1460505600": { name: "UnixTime", expectedValue: 1460505600 },
},
},
long: {
name: "Long",
scenarios: {
"10000000000": { name: "Positive", expectedValue: 10000000000 },
"-10000000000": { name: "Negative", expectedValue: -10000000000 },
},
},
float: {
name: "Float",
scenarios: {
"1.034E+20": { name: "Positive", expectedValue: 1.034e20 },
"-1.034E-20": { name: "Negative", expectedValue: -1.034e-20 },
},
},
double: {
name: "Double",
scenarios: {
"9999999.999": { name: "Positive", expectedValue: 9999999.999 },
"-9999999.999": { name: "Negative", expectedValue: -9999999.999 },
},
},
string: {
name: "String",
scenarios: {
"empty": "Empty",
"unicode": { name: "Unicode", expectedValue: Constants.MULTIBYTE_BUFFER },
"begin!*'();:@ &=+$,/?#[]end": "UrlEncoded",
"begin!*'();:@&=+$,end": {
name: "UrlNonEncoded",
expectedValue: "begin!*'();:@&=+$,end",
},
// This scenario shouldn't really be possible. The sdk should never call the endpoint.
// "null": "Null",
"bG9yZW0": "Base64Url",
},
},
byte: {
name: "Byte",
scenarios: {
empty: "Empty",
multibyte: { name: "MultiByte", expectedValue: Buffer.from(Constants.MULTIBYTE_BUFFER).toString("base64") },
},
},
date: {
name: "Date",
scenarios: {
"2012-01-01": "Valid",
},
},
datetime: {
name: "DateTime",
scenarios: {
"2012-01-01T01:01:01Z": {
name: "Valid",
expectedValue: new Date("2012-01-01T01:01:01Z").toISOString(),
},
},
},
enum: {
name: "Enum",
scenarios: {
"green color": "Valid",
},
},
array: {
name: "Array",
scenarios: {
"ArrayPath1,begin!*'();:@ &=+$,/?#[]end,,": "CSVInPath",
},
},
} as const;
type Scenarios = typeof scenarios;
interface ScenarioConfig {
name: string;
expectedValue: unknown;
}
app.category("vanilla", () => {
for (const [type, value] of Object.entries(scenarios)) {
for (const scenarioConfig of Object.values<string | ScenarioConfig>(value.scenarios)) {
const coverageName = `UrlPaths${value.name}${getScenarioName(scenarioConfig)}`;
coverageService.register("vanilla", coverageName);
}
app.get(`/paths/${type}/:scenarioName/:wireParameter?`, undefined, (req) => {
const scenarioName: string = decodeURIComponent(req.params.scenarioName);
const scenarioConfig: ScenarioConfig = value.scenarios[scenarioName as keyof typeof value.scenarios];
const coverageName = `UrlPaths${value.name}${getScenarioName(scenarioConfig)}`;
if (scenarioName === "empty") {
if (req.params.wireParameter) {
throw new ValidationError("wireParameter should be empty", undefined, req.params.wireParameter);
}
coverageService.track("vanilla", coverageName);
return {
status: 200,
};
}
let wireParameter = deserializeValue(type as never, req.params.wireParameter);
if (scenarioConfig === undefined) {
return { status: 404, body: json({ message: `Scenario "${scenarioName}" not found for type ${type}` }) };
}
let expectedValue = getScenarioExpectedValue(scenarioName, scenarioConfig);
if (type === "datetime") {
wireParameter = coerceDateString(wireParameter);
expectedValue = coerceDateString(expectedValue as string);
}
if (wireParameter !== expectedValue) {
throw new ValidationError("wireParameter path does not match expected value", expectedValue, wireParameter);
}
coverageService.track("vanilla", coverageName);
return { status: 200 };
});
}
});
function deserializeValue<T extends keyof Scenarios>(type: T, value: string) {
switch (type) {
case "int":
case "long":
case "float":
case "double":
return JSON.parse(decodeURIComponent(value));
default:
return value;
}
}
function getScenarioName(scenarioConfig: string | ScenarioConfig) {
return typeof scenarioConfig === "string" ? scenarioConfig : scenarioConfig.name;
}
function getScenarioExpectedValue(scenarioPath: string, scenarioConfig: string | ScenarioConfig) {
return typeof scenarioConfig === "string" ? scenarioPath : scenarioConfig.expectedValue;
}