-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.rs
More file actions
801 lines (713 loc) · 27.1 KB
/
Copy pathhttp.rs
File metadata and controls
801 lines (713 loc) · 27.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! HTTP and REST-oriented helper handlers.
//!
//! These functions build/validate plain struct payloads that the runtime treats as regular values.
use crate::handler::argument::Argument;
use crate::handler::macros::args;
use crate::handler::registry::FunctionRegistration;
use crate::runtime::execution::value_store::ValueStore;
use crate::types::errors::runtime_error::RuntimeError;
use crate::types::signal::Signal;
use crate::value::number_to_string;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::io::Read;
use tucana::shared::helper::value::{ToValue, from_json_value, to_json_value};
use tucana::shared::value::Kind;
use tucana::shared::{Struct, Value};
use ureq::http;
use ureq::{Body, RequestExt};
pub(crate) const FUNCTIONS: &[FunctionRegistration] = &[
FunctionRegistration::eager("http::request::create", create_request, 4),
FunctionRegistration::eager("http::request::send", send_request, 1),
FunctionRegistration::eager("http::response::create", create_response, 3),
FunctionRegistration::eager("rest::control::respond", respond, 1),
];
fn fail(category: &str, message: impl Into<String>) -> Signal {
Signal::Failure(RuntimeError::new("T-STD-00001", category, message))
}
fn respond(
args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal,
) -> Signal {
args!(args => struct_val: Struct);
let fields = &struct_val.fields;
let Some(headers_val) = fields.get("headers") else {
return Signal::Failure(RuntimeError::new(
"T-STD-00001",
"InvalidArgumentRuntimeError",
"Missing 'headers' field".to_string(),
));
};
let Some(status_code_val) = fields.get("http_status_code") else {
return fail(
"InvalidArgumentRuntimeError",
"Missing 'http_status_code' field",
);
};
let Some(payload_val) = fields.get("payload") else {
return Signal::Failure(RuntimeError::new(
"T-STD-00001",
"InvalidArgumentRuntimeError",
"Missing 'payload' field".to_string(),
));
};
let Some(Kind::StructValue(_headers_struct)) = &headers_val.kind else {
return fail(
"InvalidArgumentRuntimeError",
"Expected 'headers' to be StructValue",
);
};
let Some(Kind::NumberValue(_status_code_str)) = &status_code_val.kind else {
return Signal::Failure(RuntimeError::new(
"T-STD-00001",
"InvalidArgumentRuntimeError",
"Expected 'status_code' to be NumberValue".to_string(),
));
};
let Some(_payload_kind) = &payload_val.kind else {
return Signal::Failure(RuntimeError::new(
"T-STD-00001",
"InvalidArgumentRuntimeError",
"Expected 'payload' to have a value".to_string(),
));
};
// `Respond` is a control signal; the executor can still continue with `next` if present.
Signal::Respond(Value {
kind: Some(Kind::StructValue(struct_val.clone())),
})
}
fn create_request(
args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal,
) -> Signal {
args!(args => http_method: String, headers: Struct, http_url: String, payload: Value);
let mut fields = std::collections::HashMap::new();
fields.insert(String::from("http_method"), http_method.to_value());
fields.insert(String::from("url"), http_url.to_value());
fields.insert(String::from("payload"), payload.clone());
fields.insert(
String::from("headers"),
Value {
kind: Some(Kind::StructValue(headers.clone())),
},
);
Signal::Success(Value {
kind: Some(Kind::StructValue(Struct { fields })),
})
}
fn send_request(
args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal,
) -> Signal {
args!(args => http_request: Struct);
let method = match expect_struct_string_field(&http_request, "http_method") {
Ok(value) => value,
Err(signal) => return signal,
};
let url = match expect_struct_string_field(&http_request, "url") {
Ok(value) => value,
Err(signal) => return signal,
};
let headers_struct = match expect_struct_struct_field(&http_request, "headers") {
Ok(value) => value,
Err(signal) => return signal,
};
let payload = match http_request.fields.get("payload") {
Some(value) => value.clone(),
None => {
return fail(
"InvalidArgumentRuntimeError",
"Missing 'payload' field in http_request",
);
}
};
let mut headers = match encode_headers(&headers_struct) {
Ok(headers) => headers,
Err(message) => return fail("InvalidArgumentRuntimeError", message),
};
let request_content_type = content_type_header_value(&headers);
let (request_body, default_content_type) =
match encode_request_payload(&payload, request_content_type.as_deref()) {
Ok(result) => result,
Err(message) => return fail("InvalidArgumentRuntimeError", message),
};
if let Some(default_content_type) = default_content_type
&& request_content_type.is_none()
{
headers.insert("content-type".to_string(), default_content_type.to_string());
}
let http_method = match http::Method::from_bytes(method.as_bytes()) {
Ok(value) => value,
Err(_) => {
return fail(
"InvalidArgumentRuntimeError",
format!("Invalid HTTP method '{}'", method),
);
}
};
let mut request_builder = http::Request::builder().method(http_method).uri(&url);
for (name, value) in &headers {
request_builder = request_builder.header(name, value);
}
let response_result = match request_body {
Some(bytes) => {
let request = match request_builder.body(bytes) {
Ok(request) => request,
Err(err) => {
return fail(
"InvalidArgumentRuntimeError",
format!("Invalid HTTP request: {}", err),
);
}
};
request
.with_default_agent()
.configure()
.http_status_as_error(false)
.allow_non_standard_methods(true)
.run()
}
None => {
let request = match request_builder.body(()) {
Ok(request) => request,
Err(err) => {
return fail(
"InvalidArgumentRuntimeError",
format!("Invalid HTTP request: {}", err),
);
}
};
request
.with_default_agent()
.configure()
.http_status_as_error(false)
.allow_non_standard_methods(true)
.run()
}
};
let response = match response_result {
Ok(response) => response,
Err(err) => {
return fail(
"HttpRequestRuntimeError",
format!("HTTP request error while sending request: {}", err),
);
}
};
let status_code = response.status().as_u16() as i64;
let response_headers = decode_headers(&response);
let response_payload = match decode_response_payload(response) {
Ok(result) => result,
Err(message) => return fail("HttpRequestRuntimeError", message),
};
let mut fields = HashMap::new();
fields.insert("http_status_code".to_string(), status_code.to_value());
fields.insert(
"headers".to_string(),
Value {
kind: Some(Kind::StructValue(response_headers)),
},
);
fields.insert("payload".to_string(), response_payload);
Signal::Success(Value {
kind: Some(Kind::StructValue(Struct { fields })),
})
}
fn create_response(
args: &[Argument],
_ctx: &mut ValueStore,
_run: &mut dyn FnMut(i64, &mut ValueStore) -> Signal,
) -> Signal {
args!(args => http_status_code: i64, headers: Struct, payload: Value);
let mut fields = std::collections::HashMap::new();
fields.insert(
String::from("http_status_code"),
http_status_code.to_value(),
);
fields.insert(String::from("payload"), payload.clone());
fields.insert(
String::from("headers"),
Value {
kind: Some(Kind::StructValue(headers.clone())),
},
);
Signal::Success(Value {
kind: Some(Kind::StructValue(Struct { fields })),
})
}
fn expect_struct_string_field(struct_val: &Struct, field: &str) -> Result<String, Signal> {
let Some(value) = struct_val.fields.get(field) else {
return Err(fail(
"InvalidArgumentRuntimeError",
format!("Missing '{}' field in http_request", field),
));
};
match &value.kind {
Some(Kind::StringValue(str_val)) => Ok(str_val.clone()),
_ => Err(fail(
"InvalidArgumentRuntimeError",
format!("Expected '{}' to be StringValue", field),
)),
}
}
fn expect_struct_struct_field(struct_val: &Struct, field: &str) -> Result<Struct, Signal> {
let Some(value) = struct_val.fields.get(field) else {
return Err(fail(
"InvalidArgumentRuntimeError",
format!("Missing '{}' field in http_request", field),
));
};
match &value.kind {
Some(Kind::StructValue(struct_val)) => Ok(struct_val.clone()),
_ => Err(fail(
"InvalidArgumentRuntimeError",
format!("Expected '{}' to be StructValue", field),
)),
}
}
fn encode_headers(headers: &Struct) -> Result<HashMap<String, String>, String> {
let mut out = HashMap::with_capacity(headers.fields.len());
for (name, value) in &headers.fields {
if name.trim().is_empty() {
return Err("Header name cannot be empty".to_string());
}
out.insert(name.clone(), value_to_string(value)?);
}
Ok(out)
}
fn value_to_string(value: &Value) -> Result<String, String> {
match &value.kind {
Some(Kind::StringValue(str_val)) => Ok(str_val.clone()),
Some(Kind::NumberValue(number)) => Ok(number_to_string(number)),
Some(Kind::BoolValue(bool_val)) => Ok(bool_val.to_string()),
Some(Kind::NullValue(_)) | None => Err("Null is not a valid header value".to_string()),
Some(Kind::ListValue(_)) | Some(Kind::StructValue(_)) => {
serde_json::to_string(&to_json_value(value.clone()))
.map_err(|err| format!("Unable to serialize header value: {}", err))
}
}
}
fn content_type_header_value(headers: &HashMap<String, String>) -> Option<String> {
headers.iter().find_map(|(name, value)| {
if name.eq_ignore_ascii_case("content-type") {
Some(value.clone())
} else {
None
}
})
}
fn normalize_content_type(content_type: &str) -> String {
content_type
.split(';')
.next()
.unwrap_or_default()
.trim()
.to_ascii_lowercase()
}
fn content_type_is_text_plain(content_type: &str) -> bool {
content_type == "text/plain"
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum RequestBodyEncoding {
Json,
TextPlain,
}
fn resolve_request_body_encoding(
payload: &Value,
request_content_type: Option<&str>,
) -> Result<Option<RequestBodyEncoding>, String> {
if let Some(content_type) = request_content_type {
let normalized = normalize_content_type(content_type);
if content_type_is_json(&normalized) {
return Ok(Some(RequestBodyEncoding::Json));
}
if content_type_is_text_plain(&normalized) {
return Ok(Some(RequestBodyEncoding::TextPlain));
}
return Err(format!(
"Unsupported content-type '{}' for http::request::send. Supported types: application/json, text/plain",
content_type
));
}
match payload.kind.as_ref() {
Some(Kind::NullValue(_)) | None => Ok(None),
Some(Kind::StringValue(_)) => Ok(Some(RequestBodyEncoding::TextPlain)),
_ => Ok(Some(RequestBodyEncoding::Json)),
}
}
fn encode_request_payload(
payload: &Value,
request_content_type: Option<&str>,
) -> Result<(Option<Vec<u8>>, Option<&'static str>), String> {
let Some(encoding) = resolve_request_body_encoding(payload, request_content_type)? else {
return Ok((None, None));
};
match encoding {
RequestBodyEncoding::Json => {
let json = to_json_value(payload.clone());
let body = serde_json::to_vec(&json)
.map_err(|err| format!("Unable to serialize request payload: {}", err))?;
Ok((Some(body), Some("application/json")))
}
RequestBodyEncoding::TextPlain => match payload.kind.as_ref() {
Some(Kind::NullValue(_)) | None => Ok((None, Some("text/plain"))),
Some(Kind::StringValue(body)) => {
Ok((Some(body.as_bytes().to_vec()), Some("text/plain")))
}
_ => Err("Payload must be StringValue when content-type is text/plain".to_string()),
},
}
}
fn decode_headers(response: &http::Response<Body>) -> Struct {
let mut fields = HashMap::new();
for (name, value) in response.headers().iter() {
if let Ok(value) = value.to_str() {
fields.insert(name.as_str().to_string(), value.to_string().to_value());
}
}
Struct { fields }
}
fn decode_response_payload(response: http::Response<Body>) -> Result<Value, String> {
let content_type = response
.headers()
.get(http::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.map(|value| value.to_ascii_lowercase());
let mut bytes = Vec::new();
let (_, body) = response.into_parts();
let mut reader = body.into_reader();
reader
.read_to_end(&mut bytes)
.map_err(|err| format!("Unable to read HTTP response payload: {}", err))?;
if bytes.is_empty() {
return Ok(Value {
kind: Some(Kind::NullValue(0)),
});
}
if let Ok(text) = String::from_utf8(bytes.clone()) {
if content_type
.as_deref()
.map(content_type_is_json)
.unwrap_or(false)
{
if let Ok(json) = serde_json::from_str::<JsonValue>(&text) {
return Ok(from_json_value(json));
}
}
return Ok(text.to_value());
}
let values: Vec<i64> = bytes.iter().map(|byte| *byte as i64).collect();
Ok(values.to_value())
}
fn content_type_is_json(content_type: &str) -> bool {
content_type.contains("/json") || content_type.contains("+json")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::handler::argument::Argument;
use crate::runtime::execution::value_store::ValueStore;
use crate::value::number_to_i64_lossy;
use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
use std::time::Duration;
fn string_value(value: &str) -> Value {
value.to_string().to_value()
}
#[test]
fn encode_request_payload_serializes_non_string_values_to_json() {
let payload = Value {
kind: Some(Kind::StructValue(Struct {
fields: HashMap::from([(
"ok".to_string(),
Value {
kind: Some(Kind::BoolValue(true)),
},
)]),
})),
};
let (body, content_type) = match encode_request_payload(&payload, None) {
Ok(result) => result,
Err(err) => panic!("unexpected error: {}", err),
};
assert_eq!(content_type, Some("application/json"));
let body = body.unwrap_or_default();
let text = match String::from_utf8(body) {
Ok(text) => text,
Err(err) => panic!("payload was not valid utf8: {}", err),
};
let decoded = serde_json::from_str::<JsonValue>(&text).unwrap_or(JsonValue::Null);
let JsonValue::Object(map) = decoded else {
panic!("expected encoded payload to be json object");
};
let Some(JsonValue::Bool(ok)) = map.get("ok") else {
panic!("missing ok field in json payload");
};
assert!(*ok);
let (empty_body, empty_content_type) = encode_request_payload(
&Value {
kind: Some(Kind::NullValue(0)),
},
None,
)
.unwrap_or((Some(vec![1]), Some("application/json")));
assert_eq!(empty_content_type, None);
assert!(empty_body.is_none());
}
#[test]
fn encode_request_payload_uses_text_plain_header_and_rejects_unsupported_content_type() {
let (text_body, text_content_type) =
encode_request_payload(&string_value("hello"), Some("text/plain; charset=utf-8"))
.unwrap_or((None, None));
assert_eq!(text_content_type, Some("text/plain"));
let body = text_body.unwrap_or_default();
assert_eq!(body, b"hello");
let err = encode_request_payload(&string_value("hello"), Some("application/xml"));
let Err(err) = err else {
panic!("expected unsupported content-type error");
};
assert!(err.contains("Supported types: application/json, text/plain"));
let err = encode_request_payload(
&Value {
kind: Some(Kind::NullValue(0)),
},
Some("application/octet-stream"),
);
let Err(err) = err else {
panic!("expected unsupported content-type error for null payload");
};
assert!(err.contains("Supported types: application/json, text/plain"));
let err = encode_request_payload(
&Value {
kind: Some(Kind::StructValue(Struct {
fields: HashMap::from([("a".to_string(), 1i64.to_value())]),
})),
},
Some("text/plain"),
);
let Err(err) = err else {
panic!("expected text/plain payload validation error");
};
assert!(err.contains("Payload must be StringValue"));
}
#[test]
fn encode_headers_rejects_null_values() {
let headers = Struct {
fields: HashMap::from([(
"x-null".to_string(),
Value {
kind: Some(Kind::NullValue(0)),
},
)]),
};
let result = encode_headers(&headers);
let Err(err) = result else {
panic!("expected error for null header value");
};
assert!(err.contains("Null is not a valid header value"));
}
#[test]
fn send_request_tcp_listener_roundtrip_validates_request_and_response_mapping() {
let listener = match TcpListener::bind("127.0.0.1:0") {
Ok(listener) => listener,
Err(err) => panic!("failed to bind test listener: {}", err),
};
let addr = match listener.local_addr() {
Ok(addr) => addr,
Err(err) => panic!("failed to fetch local address: {}", err),
};
let server = thread::spawn(move || {
let (mut stream, _) = match listener.accept() {
Ok(pair) => pair,
Err(err) => panic!("failed to accept inbound socket: {}", err),
};
if let Err(err) = stream.set_read_timeout(Some(Duration::from_secs(3))) {
panic!("failed to configure socket timeout: {}", err);
}
let mut request_bytes = Vec::new();
let mut buf = [0_u8; 1024];
let mut headers_end = None;
let mut content_length = 0_usize;
loop {
let n = match stream.read(&mut buf) {
Ok(n) => n,
Err(err) => panic!("failed while reading request bytes: {}", err),
};
if n == 0 {
break;
}
request_bytes.extend_from_slice(&buf[..n]);
if headers_end.is_none() {
headers_end = request_bytes.windows(4).position(|w| w == b"\r\n\r\n");
if let Some(idx) = headers_end {
let header_text = match String::from_utf8(request_bytes[..idx].to_vec()) {
Ok(text) => text,
Err(err) => panic!("request headers not utf8: {}", err),
};
for line in header_text.lines().skip(1) {
if let Some((name, value)) = line.split_once(':')
&& name.eq_ignore_ascii_case("content-length")
{
content_length = value.trim().parse::<usize>().unwrap_or(0);
}
}
}
}
if let Some(idx) = headers_end {
let body_start = idx + 4;
if request_bytes.len() >= body_start + content_length {
break;
}
}
}
let Some(headers_end) = headers_end else {
panic!("did not receive full header block");
};
let body_start = headers_end + 4;
let header_text = match String::from_utf8(request_bytes[..headers_end].to_vec()) {
Ok(text) => text,
Err(err) => panic!("request headers not utf8: {}", err),
};
let body_bytes = &request_bytes[body_start..];
let body_text = match String::from_utf8(body_bytes.to_vec()) {
Ok(text) => text,
Err(err) => panic!("request body not utf8: {}", err),
};
let mut header_map = HashMap::<String, String>::new();
let mut lines = header_text.lines();
let start_line = lines.next().unwrap_or_default().to_string();
for line in lines {
if let Some((name, value)) = line.split_once(':') {
header_map.insert(name.trim().to_ascii_lowercase(), value.trim().to_string());
}
}
assert_eq!(start_line, "POST /echo?x=1 HTTP/1.1");
assert_eq!(
header_map.get("x-bool").map(String::as_str),
Some("true"),
"expected bool header conversion to string"
);
assert_eq!(
header_map.get("content-type").map(String::as_str),
Some("application/json"),
"expected automatic JSON content type for structured payload"
);
let json = serde_json::from_str::<JsonValue>(&body_text).unwrap_or(JsonValue::Null);
let JsonValue::Object(map) = json else {
panic!("request body should be json object");
};
assert_eq!(
map.get("msg"),
Some(&JsonValue::String("hello".to_string()))
);
assert_eq!(
map.get("count"),
Some(&JsonValue::Number(serde_json::Number::from(2)))
);
let response_body = r#"{"ok":true,"echo":"done"}"#;
let response = format!(
"HTTP/1.1 201 Created\r\nContent-Type: application/json\r\nX-Reply: ok\r\nContent-Length: {}\r\n\r\n{}",
response_body.len(),
response_body
);
if let Err(err) = stream.write_all(response.as_bytes()) {
panic!("failed sending response: {}", err);
}
});
let request_payload = Value {
kind: Some(Kind::StructValue(Struct {
fields: HashMap::from([
("msg".to_string(), string_value("hello")),
("count".to_string(), 2i64.to_value()),
]),
})),
};
let request_headers = Struct {
fields: HashMap::from([("x-bool".to_string(), true.to_value())]),
};
let request = Struct {
fields: HashMap::from([
("http_method".to_string(), string_value("POST")),
(
"url".to_string(),
string_value(&format!("http://{}/echo?x=1", addr)),
),
(
"headers".to_string(),
Value {
kind: Some(Kind::StructValue(request_headers)),
},
),
("payload".to_string(), request_payload),
]),
};
let args = vec![Argument::Eval(Value {
kind: Some(Kind::StructValue(request)),
})];
let mut ctx = ValueStore::default();
let mut run = |_: i64, _: &mut ValueStore| Signal::Stop;
let signal = send_request(&args, &mut ctx, &mut run);
let response = match signal {
Signal::Success(Value {
kind: Some(Kind::StructValue(response)),
}) => response,
other => panic!("expected success struct response, got: {:?}", other),
};
let status = match response.fields.get("http_status_code") {
Some(Value {
kind: Some(Kind::NumberValue(number)),
}) => number_to_i64_lossy(number).unwrap_or_default(),
_ => panic!("expected numeric status code"),
};
assert_eq!(status, 201);
match response.fields.get("headers") {
Some(Value {
kind: Some(Kind::StructValue(headers)),
}) => {
let reply = headers
.fields
.get("x-reply")
.and_then(|value| value.kind.as_ref())
.and_then(|kind| match kind {
Kind::StringValue(value) => Some(value.as_str()),
_ => None,
});
assert_eq!(reply, Some("ok"));
}
_ => panic!("expected response headers struct"),
}
match response.fields.get("payload") {
Some(Value {
kind: Some(Kind::StructValue(payload)),
}) => {
let ok = payload
.fields
.get("ok")
.and_then(|value| value.kind.as_ref())
.and_then(|kind| match kind {
Kind::BoolValue(value) => Some(*value),
_ => None,
});
assert_eq!(ok, Some(true));
let echo = payload
.fields
.get("echo")
.and_then(|value| value.kind.as_ref())
.and_then(|kind| match kind {
Kind::StringValue(value) => Some(value.as_str()),
_ => None,
});
assert_eq!(echo, Some("done"));
}
_ => panic!("expected JSON response payload struct"),
}
if let Err(err) = server.join() {
panic!("server thread join failed: {:?}", err);
}
}
}