-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.w
56 lines (45 loc) · 1.3 KB
/
main.w
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
bring cloud;
bring expect;
bring http;
bring "./hitcounter.w" as h;
class Shortener {
mapping: cloud.Bucket;
pub url: str;
hitcounter: h.HitCounter;
new() {
let api = new cloud.Api() as "data_plane";
let mapping = new cloud.Bucket() as "mapping";
this.hitcounter = new h.HitCounter();
this.mapping = mapping;
this.url = api.url;
api.get("/:alias", inflight (req) => {
let alias = req.vars.get("alias");
let target = mapping.get(alias);
this.hitcounter.hit(alias);
log("redirecting {alias} to ?");
return {
status: 307,
headers: {
location: target
}
};
});
}
pub inflight create(alias: str, target: str) {
this.mapping.put(alias, target);
}
pub inflight stats(): Map<num> {
return this.hitcounter.query();
}
}
let s = new Shortener() as "s2";
test "happy flow" {
s.create("wing", "https://winglang.io");
let response = http.get("{s.url}/wing", redirect: http.RequestRedirect.MANUAL);
expect.equal(307, response.status);
let location = response.headers.get("location");
expect.equal("https://winglang.io", location);
http.get("{s.url}/wing", redirect: http.RequestRedirect.MANUAL);
http.get("{s.url}/wing", redirect: http.RequestRedirect.MANUAL);
log(Json.stringify(s.stats()));
}