-
Notifications
You must be signed in to change notification settings - Fork 0
/
_mock_server.ts
111 lines (110 loc) · 2.84 KB
/
_mock_server.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
// Copyright 2020 KwanJunWen. All rights reserved. MIT license.
import {
listenAndServe,
ServerRequest,
Status,
decode,
} from "./deps.ts";
import { HOSTNAME, PORT } from "./_mock_config.ts";
console.log(`OneWaySMS mock server running at http://${HOSTNAME}:${PORT}/`);
listenAndServe(
{ port: PORT, hostname: HOSTNAME },
(req: ServerRequest): void => {
let qs: { [key: string]: string | string[] } = {};
const qsmatch = req.url.match(/\?(.*)/);
if (qsmatch && qsmatch.length > 0) {
qs = decode(qsmatch[1]);
}
const {
apiusername,
apipassword,
senderid,
mobileno,
message,
languagetype,
mtid,
} = qs;
if (req.url.includes("/api.aspx")) {
if (apiusername === "invalid" || apipassword === "invalid") {
req.respond({ body: "-100" });
return;
}
if (senderid === "invalid") {
req.respond({ body: "-200" });
return;
}
if (mobileno === "invalid") {
req.respond({ body: "-300" });
return;
}
// Mock invalid language type
if (languagetype === "2") {
req.respond({ body: "-400" });
return;
}
if (message === "invalid") {
req.respond({ body: "-500" });
return;
}
if (message === "insufficient credit balance") {
req.respond({ body: "-600" });
return;
}
if (message === "unknown error") {
req.respond({ body: "random" });
return;
}
if (message === "request failure") {
req.respond({ status: Status.InternalServerError });
return;
}
if (mobileno === "60123456789,60129876543") {
req.respond({ body: "145712468,145712469" });
return;
}
if (mobileno === "60123456789") {
req.respond({ body: "145712468" });
return;
}
return;
}
if (req.url.includes("/bulktrx.aspx")) {
if (mtid === "1") {
req.respond({ body: "-100" });
return;
}
if (mtid === "2") {
req.respond({ body: "-200" });
return;
}
if (mtid === "3") {
req.respond({ body: "random" });
return;
}
if (mtid === "145712470") {
req.respond({ body: "0" });
return;
}
if (mtid === "145712471") {
req.respond({ body: "100" });
return;
}
return;
}
if (req.url.includes("/bulkcredit.aspx")) {
if (apiusername === "invalid" || apipassword === "invalid") {
req.respond({ body: "-100" });
return;
}
if (apiusername === "unknown error" || apipassword === "unknown error") {
req.respond({ body: "random" });
return;
}
if (apiusername === "Username" || apipassword === "Password") {
req.respond({ body: "6500" });
return;
}
return;
}
},
);