-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero_wbtest.mbt
More file actions
189 lines (175 loc) · 5.67 KB
/
Copy pathzero_wbtest.mbt
File metadata and controls
189 lines (175 loc) · 5.67 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
///|
/// Build a synthetic request for routing assertions.
fn mkreq(m : String, path : String) -> @moonasgi.Request {
{ http_method: m, path, query_string: b"", headers: [], body: b"", }
}
///|
/// A scope carrying only the given request headers, for middleware unit tests.
fn scope_with(headers : Array[(String, String)]) -> @moonasgi.Scope {
@moonasgi.Scope::Http({
http_version: "1.1",
http_method: "GET",
scheme: "http",
path: "/",
raw_path: b"/",
query_string: b"",
root_path: "",
headers,
client: None,
server: None,
asgi: @moonasgi.AsgiVersion::http(),
extensions: @moonasgi.Extensions::none(),
state: Map([]),
})
}
///|
/// Look up a header value by name in a header list.
fn header_of(headers : Array[(String, String)], name : String) -> String? {
for pair in headers {
if pair.0 == name {
return Some(pair.1)
}
}
None
}
///|
test "config-driven server assembly + describe" {
let app = @moonapi.App::new()
app.get("/ping", _ctx => @moonapi.text(200, "pong"))
let conf = ServiceConf::new(name="greet", host="127.0.0.1", port=12000)
let server = Server::new(conf, app).use_(logging)
assert_eq(server.describe(), "greet listening on 127.0.0.1:12000")
let _ = server.to_asgi()
}
///|
test "default config" {
let conf = ServiceConf::new()
assert_eq(conf.name, "app")
assert_eq(conf.host, "0.0.0.0")
assert_eq(conf.port, 12000)
assert_eq(conf.timeout_ms, 3000)
assert_eq(conf.log_level, Info)
}
///|
test "log level naming, parsing, and ordering" {
assert_eq(LogLevel::Debug.to_string(), "debug")
assert_eq(LogLevel::Info.to_string(), "info")
assert_eq(LogLevel::Error.to_string(), "error")
assert_eq(LogLevel::Severe.to_string(), "severe")
assert_eq(LogLevel::parse("severe"), Severe)
assert_eq(LogLevel::parse("debug"), Debug)
assert_eq(LogLevel::parse("nonsense"), Info)
assert_eq(LogLevel::Debug < LogLevel::Severe, true)
}
///|
test "join_path normalises prefix and route" {
assert_eq(join_path("/api/v1", "/users"), "/api/v1/users")
assert_eq(join_path("/api/v1/", "/users"), "/api/v1/users")
assert_eq(join_path("/api/v1", "users"), "/api/v1/users")
assert_eq(join_path("", "/users"), "/users")
assert_eq(join_path("/api/v1", ""), "/api/v1")
assert_eq(join_path("", ""), "/")
}
///|
test "group prefixes routes onto the underlying app" {
let app = @moonapi.App::new()
let g = Group::new(app, "/api/v1")
assert_eq(g.prefix(), "/api/v1")
g.get("/users", _ctx => @moonapi.text(200, "users"))
g.post("orders", _ctx => @moonapi.text(201, "made"))
g.delete("/users/:id", ctx => @moonapi.text(200, ctx.param("id").unwrap()))
// prefixed paths route
assert_eq(app.handle(mkreq("GET", "/api/v1/users")).status, 200)
assert_eq(app.handle(mkreq("POST", "/api/v1/orders")).status, 201)
assert_eq(app.handle(mkreq("DELETE", "/api/v1/users/42")).status, 200)
// the un-prefixed path does NOT match
assert_eq(app.handle(mkreq("GET", "/users")).status, 404)
// right path, wrong method -> 405
assert_eq(app.handle(mkreq("PUT", "/api/v1/users")).status, 405)
}
///|
test "cors_headers advertises the configured policy" {
let conf = CorsConf::new(
allow_origin="https://example.com",
allow_credentials=true,
)
let headers = cors_headers(conf)
assert_eq(
header_of(headers, "access-control-allow-origin"),
Some("https://example.com"),
)
assert_eq(
header_of(headers, "access-control-allow-credentials"),
Some("true"),
)
assert_eq(header_of(headers, "access-control-max-age"), Some("86400"))
assert_eq(
header_of(headers, "access-control-allow-methods"),
Some("GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS"),
)
}
///|
test "cors_headers omits credentials when disabled" {
let headers = cors_headers(CorsConf::new())
assert_eq(header_of(headers, "access-control-allow-credentials"), None)
assert_eq(header_of(headers, "access-control-allow-origin"), Some("*"))
}
///|
test "cors middleware assembles into an AsgiApp" {
let app = @moonapi.App::new()
app.get("/ping", _ctx => @moonapi.text(200, "pong"))
let _ = cors(CorsConf::new())(app.to_asgi())
}
///|
test "request-id resolution mints and reuses" {
let counter : Ref[Int] = { val: 0, }
assert_eq(resolve_request_id(None, counter), "req-1")
assert_eq(resolve_request_id(None, counter), "req-2")
assert_eq(resolve_request_id(Some("abc-123"), counter), "abc-123")
// reuse does not advance the counter
assert_eq(resolve_request_id(None, counter), "req-3")
}
///|
test "request-id reads the inbound header from an HTTP scope" {
assert_eq(
scope_header(scope_with([("x-request-id", "trace-9")]), "x-request-id"),
Some("trace-9"),
)
assert_eq(scope_header(scope_with([]), "x-request-id"), None)
let _ = request_id()(@moonapi.App::new().to_asgi())
}
///|
test "recovery emits a 500 with a body" {
let events = internal_error_events()
let mut status = 0
let mut body_len = 0
for e in events {
match e {
HttpResponseStart(status=s, ..) => status = s
HttpResponseBody(body~, more_body=_) => body_len = body.length()
_ => ()
}
}
assert_eq(status, 500)
assert_eq(body_len > 0, true)
}
///|
test "full middleware onion assembles and describes" {
let app = @moonapi.App::new()
let g = Group::new(app, "/api")
g.get("/health", _ctx => @moonapi.text(200, "ok"))
assert_eq(app.handle(mkreq("GET", "/api/health")).status, 200)
let conf = ServiceConf::new(
name="svc",
host="127.0.0.1",
port=9000,
log_level=Error,
)
let server = Server::new(conf, app)
.use_(cors(CorsConf::new()))
.use_(request_id())
.use_(recovery)
.use_(logging)
assert_eq(server.describe(), "svc listening on 127.0.0.1:9000")
let _ = server.to_asgi()
}