-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresponse.mbt
More file actions
67 lines (61 loc) · 1.37 KB
/
response.mbt
File metadata and controls
67 lines (61 loc) · 1.37 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
///|
pub(all) struct HttpResponse {
mut status_code : StatusCode
headers : Map[StringView, StringView]
cookies : Map[String, CookieItem]
mut raw_body : Bytes
}
///|
pub fn[T : BodyReader] HttpResponse::read_body(self : HttpResponse) -> T raise {
T::from_request({
http_method: "",
url: "",
headers: self.headers,
raw_body: self.raw_body,
})
}
///|
pub fn HttpResponse::new(
status_code : StatusCode,
headers? : Map[StringView, StringView],
cookies? : Map[String, CookieItem],
raw_body? : Bytes,
) -> HttpResponse {
HttpResponse::{
status_code,
headers: headers.unwrap_or({}),
cookies: cookies.unwrap_or({}),
raw_body: raw_body.unwrap_or(""),
}
}
///|
pub fn HttpResponse::body(
self : HttpResponse,
body : &Responder,
) -> HttpResponse {
let buf = @buffer.new()
body.output(buf)
self.raw_body = buf.to_bytes()
self
}
///|
pub fn HttpResponse::json(self : HttpResponse, obj : &ToJson) -> HttpResponse {
self.body(obj.to_json())
}
///|
pub fn HttpResponse::to_responder(self : HttpResponse) -> &Responder {
self
}
///|
test "read_response_body" {
let res = HttpResponse::new(OK, raw_body=b"{\"Hello\":\"Response\"}")
let text : String = res.read_body()
let json : Json = res.read_body()
inspect(
text,
content=(
#|{"Hello":"Response"}
),
)
json_inspect(json, content={ "Hello": "Response" })
}