zero-bounce 4.1.0

Wrapper library over the ZeroBounce API v2
Documentation
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
use super::{ZBError, ZBResult};

/// At most the first `max_bytes` bytes of `text`, cut back to a character
/// boundary. Slicing at a fixed byte offset panics when a multi-byte
/// character straddles it, and response bodies are not ASCII-only.
/// (`str::floor_char_boundary` would do this, but it is newer than the MSRV.)
fn prefix_at_char_boundary(text: &str, max_bytes: usize) -> &str {
    if text.len() <= max_bytes {
        return text;
    }
    let mut end = max_bytes;
    while !text.is_char_boundary(end) {
        end -= 1;
    }
    &text[..end]
}

pub fn body_looks_like_html(body: &str) -> bool {
    let sample = prefix_at_char_boundary(body, 2000);
    let lower = sample.to_ascii_lowercase();
    lower.contains("<!doctype html")
        || lower.contains("<html")
        || lower.contains("<head")
        || lower.contains("<body")
}

pub fn content_type_is_html(content_type: &str) -> bool {
    content_type.to_ascii_lowercase().contains("text/html")
}

pub fn strip_error_prefix(message: &str) -> String {
    let mut message = message.trim().to_string();
    loop {
        let trimmed = message.trim();
        if let Some(rest) = trimmed.strip_prefix("Error:") {
            message = rest.trim().to_string();
            continue;
        }
        if let Some(rest) = trimmed.strip_prefix("[Error]:") {
            message = rest.trim().to_string();
            continue;
        }
        return trimmed.to_string();
    }
}

fn html_title(body: &str) -> String {
    let lower = body.to_ascii_lowercase();
    let Some(start) = lower.find("<title") else {
        return String::new();
    };
    let Some(open_end_rel) = lower[start..].find('>') else {
        return String::new();
    };
    let open_end = start + open_end_rel;
    let Some(close_rel) = lower[open_end..].find("</title>") else {
        return String::new();
    };
    body[open_end + 1..open_end + close_rel].trim().to_string()
}

/// Keys that only ever mean an error. Narrower than `MESSAGE_KEYS` on purpose:
/// a successful response uses "message"/"Message" too (a bulk submit answers
/// {"success": true, "message": "File Accepted"}, a greylisted validate carries
/// "Message" next to the result), so those cannot decide success by themselves.
/// "Error" is how the filters spell it: {"Error": "Invalid API key ..."}.
const ERROR_ONLY_KEYS: [&str; 3] = ["error", "error_message", "Error"];

/// Every key an error message may hide under, once the response is already
/// known to be a failure.
/// "detail" is how the Inbox Placement Tester (Django REST framework) reports errors.
const MESSAGE_KEYS: [&str; 6] = ["error", "message", "error_message", "Message", "Error", "detail"];

fn api_error_field(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> String {
    for key in keys {
        match object.get(*key) {
            Some(serde_json::Value::String(text)) if !text.trim().is_empty() => {
                return text.trim().to_string();
            }
            Some(serde_json::Value::Array(items)) => {
                if let Some(serde_json::Value::String(text)) = items.first() {
                    if !text.trim().is_empty() {
                        return text.trim().to_string();
                    }
                }
            }
            _ => {}
        }
    }
    String::new()
}

fn first_api_error_field(object: &serde_json::Map<String, serde_json::Value>) -> String {
    api_error_field(object, &MESSAGE_KEYS)
}

/// The API's own message when an HTTP 200 body is nothing but an error: an
/// "error"/"error_message" field, and no "success" field to gate on. An
/// invalid or drained key answers every v2 endpoint that way.
fn error_only_body(object: &serde_json::Map<String, serde_json::Value>) -> String {
    if object.contains_key("success") {
        return String::new();
    }
    api_error_field(object, &ERROR_ONLY_KEYS)
}

/// The error for a 3xx. Redirects are never followed: the API key travels
/// with the request, and a redirect could send it to another host.
pub(crate) const REDIRECT_REFUSED_MESSAGE: &str =
    "ZeroBounce returned a redirect, which this client does not follow.";

pub fn message_from_body(status: u16, content_type: &str, body: &str) -> String {
    if (300..400).contains(&status) {
        return REDIRECT_REFUSED_MESSAGE.into();
    }
    if body_looks_like_html(body) || content_type_is_html(content_type) {
        let title = html_title(body);
        if title.is_empty() {
            return "ZeroBounce returned an HTML error page instead of an API response.".into();
        }
        return format!(
            "ZeroBounce returned an HTML error page (\"{title}\") instead of an API response."
        );
    }
    let trimmed = body.trim();
    if trimmed.is_empty() {
        return format!("Empty response from ZeroBounce (HTTP {status})");
    }
    if trimmed.starts_with('{') || trimmed.starts_with('[') {
        match serde_json::from_str::<serde_json::Value>(trimmed) {
            Ok(serde_json::Value::Object(object)) => {
                if let Some(serde_json::Value::String(title)) = object.get("title") {
                    let access = title.contains("Access denied") || title.contains("1020");
                    let cloudflare =
                        object.get("cloudflare_error") == Some(&serde_json::Value::Bool(true));
                    if access && cloudflare {
                        return title.clone();
                    }
                }
                let message = first_api_error_field(&object);
                if !message.is_empty() {
                    return strip_error_prefix(&message);
                }
                if trimmed.len() > 1000 {
                    return "ZeroBounce returned a response that is not valid JSON.".into();
                }
                return strip_error_prefix(trimmed);
            }
            Ok(_) => {
                if trimmed.len() > 1000 {
                    return "ZeroBounce returned a response that is not valid JSON.".into();
                }
                return strip_error_prefix(trimmed);
            }
            Err(_) => return "ZeroBounce returned a response that is not valid JSON.".into(),
        }
    }
    if trimmed.len() > 1000 {
        return "ZeroBounce returned a response that is not valid JSON.".into();
    }
    let message = strip_error_prefix(trimmed);
    if message.is_empty() {
        return format!("Empty response from ZeroBounce (HTTP {status})");
    }
    message
}

/// Whether a "success" field means failure. The API sends it as a JSON false,
/// "false" or "False" depending on the endpoint.
fn is_falsy_api_success(value: &serde_json::Value) -> bool {
    match value {
        serde_json::Value::Bool(flag) => !flag,
        serde_json::Value::String(text) => text.eq_ignore_ascii_case("false"),
        _ => false,
    }
}

/// Accept a 2xx JSON document. Anything else is a short error.
pub fn ensure_json_api_body(status: u16, content_type: &str, body: &str) -> ZBResult<()> {
    let success = (200..300).contains(&status);
    let trimmed = body.trim();
    if !success
        || trimmed.is_empty()
        || body_looks_like_html(body)
        || content_type_is_html(content_type)
    {
        return Err(ZBError::api(
            status,
            message_from_body(status, content_type, body),
        ));
    }
    // Strict RFC 8259 JSON, parsed from the body as received: serde_json
    // allows only space, tab, CR and LF around the value, and no BOM,
    // comment, trailing comma or value, NaN or other extension
    // (tests/fixtures/strict-json-corpus.tsv). `str::trim` would also drop
    // Unicode spaces such as U+00A0. Text that is not JSON is its own message;
    // JSON that does not parse is not an API answer either. (A body that
    // parses but does not fit the response type is the SDK's parse error.)
    let parsed = match serde_json::from_str::<serde_json::Value>(body) {
        Ok(parsed) => parsed,
        Err(_) if !trimmed.starts_with('{') && !trimmed.starts_with('[') => {
            return Err(ZBError::api(
                status,
                message_from_body(status, content_type, body),
            ))
        }
        Err(_) => {
            return Err(ZBError::api(
                status,
                "ZeroBounce returned a response that is not valid JSON.",
            ))
        }
    };
    // LIVE: {"success": false} on an HTTP 200 is a failure, not a result. The
    // API answers an unknown bulk file id that way, with a 200.
    if let serde_json::Value::Object(object) = parsed {
        if object.get("success").is_some_and(is_falsy_api_success) {
            return Err(ZBError::api(
                status,
                message_from_body(status, content_type, body),
            ));
        }
        // LIVE: an invalid or drained key answers with an HTTP 200 and a
        // body that is only {"error": "Invalid API key"} -- no "success"
        // field to gate on. Without this the body reached serde, which
        // failed on the unexpected shape and reported a parse error,
        // hiding the real cause.
        if !error_only_body(&object).is_empty() {
            return Err(ZBError::api(
                status,
                message_from_body(status, content_type, body),
            ));
        }
    }
    Ok(())
}

/// Whether a JSON body is nothing but a message: an object with no "success"
/// field whose keys are all message keys. The keys decide, whatever the
/// message's value (a string, blank, null or a list), since no result object
/// has these keys. An empty object (`{}`, `{ }`, `{\n}`) has no keys at all and
/// is the same failure: no result object is empty either.
fn message_only_body(object: &serde_json::Map<String, serde_json::Value>) -> bool {
    !object.contains_key("success")
        && object.keys().all(|key| MESSAGE_KEYS.contains(&key.as_str()))
}

/// For a call whose result is never a bare message -- the email finder and
/// domain search -- a message-only body is an error. LIVE: guessformat answers
/// an invalid or drained key with an HTTP 200 and
/// {"Message": "Invalid API key or your account ran out of credits"}.
/// Not for validate: a repeated greylist_processing request answers
/// {"Message": "Greylist validation still in progress..."}, which is a result.
pub fn reject_message_only_body(status: u16, content_type: &str, body: &str) -> ZBResult<()> {
    if let Ok(serde_json::Value::Object(object)) = serde_json::from_str::<serde_json::Value>(body.trim()) {
        if message_only_body(&object) {
            let message = if object.is_empty() {
                UNEXPECTED_JSON_SHAPE_MESSAGE.to_string()
            } else {
                message_from_body(status, content_type, body)
            };
            return Err(ZBError::api(status, message));
        }
    }
    Ok(())
}

/// For a call that hands back the raw body but whose answer is always a JSON
/// object (the finder and domain search file status): a body that is not an
/// object (an array, `0`, `"x"`, `null`), an empty object, or a message-only
/// object is an [`ZBError::ApiError`] with the response status. Any other
/// object, which is a real status, passes.
/// A file-status object has one of these names. Anything else (`{"foo":1}`,
/// `{"message":"m","file_id":null}`) is not a status, even though it is an object.
const FILE_STATUS_FIELDS: [&str; 9] = [
    "success",
    "file_id",
    "file_name",
    "file_status",
    "error_reason",
    "file_phase_2_status",
    "return_url",
    "upload_date",
    "complete_percentage",
];

pub(crate) fn object_has_message_key(object: &serde_json::Map<String, serde_json::Value>) -> bool {
    object
        .keys()
        .any(|key| MESSAGE_KEYS.contains(&key.as_str()))
}

pub(crate) fn object_has_non_null_named_field(
    object: &serde_json::Map<String, serde_json::Value>,
    names: &[&str],
) -> bool {
    names
        .iter()
        .any(|name| matches!(object.get(*name), Some(value) if !value.is_null()))
}

/// Parse a body as `T`, but a message beside only absent or null result fields
/// (`{"message":"m","success":null}`) is not a result: it is an
/// [`ZBError::ApiError`] with the response status and
/// [`UNEXPECTED_JSON_SHAPE_MESSAGE`]. With no message key, nulls stay a result.
/// `names` are the result's own fields (not message keys).
pub(crate) fn parse_unless_message_beside_nulls<T: serde::de::DeserializeOwned>(
    status: u16,
    body: &str,
    names: &[&str],
) -> ZBResult<T> {
    if let Ok(serde_json::Value::Object(object)) = serde_json::from_str::<serde_json::Value>(body) {
        if object_has_message_key(&object) && !object_has_non_null_named_field(&object, names) {
            return Err(ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE));
        }
    }
    parse_json_body_with_status(status, body)
}

pub fn require_status_object_body(status: u16, content_type: &str, body: &str) -> ZBResult<()> {
    match serde_json::from_str::<serde_json::Value>(body.trim()) {
        Ok(serde_json::Value::Object(object)) => {
            reject_message_only_body(status, content_type, body)?;
            if object_has_non_null_named_field(&object, &FILE_STATUS_FIELDS) {
                Ok(())
            } else {
                Err(ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE))
            }
        }
        _ => Err(ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE)),
    }
}

/// The message for a 2xx body that is valid JSON but not the shape the call
/// returns (`0`, `"x"`, `null`, a list where an object is expected, a field of
/// the wrong type).
pub const UNEXPECTED_JSON_SHAPE_MESSAGE: &str =
    "ZeroBounce returned JSON that is not the expected response.";

/// Parse a 2xx body the caller has already checked
/// ([`ensure_json_api_body`]) as the call's result type. A body that does not
/// fit is the API's answer, not a transport or SDK error: a
/// [`ZBError::ApiError`] with status 200 and [`UNEXPECTED_JSON_SHAPE_MESSAGE`].
/// The message carries no body content. Use [`parse_json_body_with_status`]
/// where the response status is known: the error then carries it.
///
/// A JSON array is never read into a struct: serde's derived structs also
/// take their fields positionally from an array (`["x"]` would fill the first
/// field), so the body is read through `ObjectsOnly`, which offers a struct
/// only a JSON object, at every depth.
pub fn parse_json_body<T: serde::de::DeserializeOwned>(body: &str) -> ZBResult<T> {
    parse_json_body_with_status(200, body)
}

/// [`parse_json_body`], with a wrong-shape error that carries the response's
/// own `status` instead of a hard-coded 200.
pub fn parse_json_body_with_status<T: serde::de::DeserializeOwned>(
    status: u16,
    body: &str,
) -> ZBResult<T> {
    serde_json::from_str::<serde_json::Value>(body)
        .and_then(|value| T::deserialize(ObjectsOnly(value)))
        .map_err(|_| ZBError::api(status, UNEXPECTED_JSON_SHAPE_MESSAGE))
}

/// A [`serde_json::Value`] deserializer that refuses an array where a struct
/// is expected, at every depth. Everything else behaves as the `Value`'s own
/// deserializer: the values inside objects and arrays are `ObjectsOnly` too.
struct ObjectsOnly(serde_json::Value);

impl<'de> serde::Deserializer<'de> for ObjectsOnly {
    type Error = serde_json::Error;

    fn deserialize_any<V: serde::de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
        match self.0 {
            serde_json::Value::Array(items) => {
                let mut seq = serde::de::value::SeqDeserializer::<_, serde_json::Error>::new(
                    items.into_iter().map(ObjectsOnly),
                );
                let value = visitor.visit_seq(&mut seq)?;
                seq.end()?;
                Ok(value)
            }
            serde_json::Value::Object(members) => {
                let mut map = serde::de::value::MapDeserializer::<_, serde_json::Error>::new(
                    members.into_iter().map(|(key, value)| (key, ObjectsOnly(value))),
                );
                let value = visitor.visit_map(&mut map)?;
                map.end()?;
                Ok(value)
            }
            other => other.deserialize_any(visitor),
        }
    }

    fn deserialize_struct<V: serde::de::Visitor<'de>>(
        self,
        name: &'static str,
        fields: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error> {
        match self.0 {
            serde_json::Value::Object(_) => self.deserialize_any(visitor),
            serde_json::Value::Array(_) => {
                Err(serde::de::Error::invalid_type(serde::de::Unexpected::Seq, &visitor))
            }
            other => other.deserialize_struct(name, fields, visitor),
        }
    }

    fn deserialize_option<V: serde::de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
        match self.0 {
            serde_json::Value::Null => visitor.visit_none(),
            _ => visitor.visit_some(self),
        }
    }

    fn deserialize_newtype_struct<V: serde::de::Visitor<'de>>(
        self,
        _name: &'static str,
        visitor: V,
    ) -> Result<V::Value, Self::Error> {
        visitor.visit_newtype_struct(self)
    }

    fn deserialize_enum<V: serde::de::Visitor<'de>>(
        self,
        name: &'static str,
        variants: &'static [&'static str],
        visitor: V,
    ) -> Result<V::Value, Self::Error> {
        self.0.deserialize_enum(name, variants, visitor)
    }

    serde::forward_to_deserialize_any! {
        bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
        bytes byte_buf unit unit_struct seq tuple tuple_struct map identifier ignored_any
    }
}

impl<'de> serde::de::IntoDeserializer<'de, serde_json::Error> for ObjectsOnly {
    type Deserializer = Self;

    fn into_deserializer(self) -> Self {
        self
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::utility::ZBError;

    const JSON: &str = "application/json";

    fn error_message(status: u16, body: &str) -> String {
        match ensure_json_api_body(status, JSON, body) {
            Err(ZBError::ApiError { message, .. }) => message,
            other => panic!("expected an ApiError, got {other:#?}"),
        }
    }

    /// LIVE: an invalid or drained key answers with an HTTP 200 and nothing
    /// but an "error" field. It used to reach serde and surface as a bogus
    /// "not valid JSON" error.
    #[test]
    fn test_200_with_error_field_is_an_api_error() {
        assert_eq!(
            error_message(200, r#"{"error":"Invalid API key"}"#),
            "Invalid API key"
        );
        assert_eq!(
            error_message(
                200,
                r#"{"error":"Invalid API key or your account ran out of credits"}"#
            ),
            "Invalid API key or your account ran out of credits"
        );
    }

    #[test]
    fn test_200_with_error_message_field_is_an_api_error() {
        assert_eq!(
            error_message(200, r#"{"error_message":"Missing parameter: api_key."}"#),
            "Missing parameter: api_key."
        );
    }

    #[test]
    fn test_200_with_empty_error_field_is_not_an_error() {
        // A payload that merely carries an empty "error" is still a result.
        assert!(ensure_json_api_body(200, JSON, r#"{"error":"","found":true}"#).is_ok());
    }

    /// A falsy "success" still decides on its own, and a truthy one wins over
    /// a message field.
    #[test]
    fn test_success_field_still_decides() {
        assert_eq!(
            error_message(200, r#"{"success":false,"message":"File cannot be found."}"#),
            "File cannot be found."
        );
        assert!(ensure_json_api_body(
            200,
            JSON,
            r#"{"success":true,"message":"File Accepted","file_id":"abc"}"#
        )
        .is_ok());
    }

    /// A greylisted validate is a result, not an error, and it carries
    /// "Message" with no "success" field -- the shape the narrow key list
    /// protects.
    #[test]
    fn test_greylisted_validation_is_not_an_error() {
        assert!(ensure_json_api_body(
            200,
            JSON,
            r#"{"address":"a@b.com","status":"unknown","sub_status":"greylisted","Message":"Greylisted, try again later"}"#
        )
        .is_ok());
    }

    /// Batch validate reports per-address failures in a nested "errors" array
    /// while the call itself succeeded.
    #[test]
    fn test_batch_nested_errors_are_not_a_failure() {
        assert!(ensure_json_api_body(
            200,
            JSON,
            r#"{"email_batch":[],"errors":[{"error":"Invalid email address","email_address":"bad@"}]}"#
        )
        .is_ok());
    }

    /// The HTML sniff looked at `&body[..2000]`, which panicked when a
    /// multi-byte character straddled byte 2000.
    #[test]
    fn test_html_sniff_does_not_split_a_multi_byte_character() {
        for wide in ["é", "€", "😀"] {
            for start in 2000 - wide.len() + 1..2000 {
                let body = format!("{}{wide}{}", "a".repeat(start), "b".repeat(100));
                assert!(!body.is_char_boundary(2000), "{wide} at {start}");
                assert!(!body_looks_like_html(&body), "{wide} at {start}");
                let page = format!("<html>{}{wide}</html>", "a".repeat(start - 6));
                assert!(body_looks_like_html(&page), "{wide} at {start}");
                assert!(ensure_json_api_body(200, JSON, &format!("{{\"city\":\"{}{wide}\"}}", "a".repeat(start - 9))).is_ok());
            }
        }
        assert_eq!(prefix_at_char_boundary("aé", 2), "a");
        assert_eq!(prefix_at_char_boundary("aé", 3), "aé");
        assert_eq!(prefix_at_char_boundary("", 0), "");
    }

    fn reject(status: u16, body: &str) -> ZBResult<()> {
        reject_message_only_body(status, JSON, body)
    }

    /// An empty object is the same failure as a message-only one, whatever
    /// whitespace JSON allows inside it.
    #[test]
    fn an_empty_object_is_rejected_as_message_only() {
        for body in ["{}", "{ }", "{\n}", "{\t}", "{\r\n \t}", " {} "] {
            match reject(202, body) {
                Err(ZBError::ApiError { status: 202, message }) => {
                    assert_eq!(message, UNEXPECTED_JSON_SHAPE_MESSAGE, "{body:?}")
                }
                other => panic!("{body:?}: {other:?}"),
            }
        }
        // Not message-only: a result, a greylist-style row, a success flag.
        assert!(reject(200, r#"{"domain":null}"#).is_ok());
        assert!(reject(200, r#"{"Message":"m","status":"unknown"}"#).is_ok());
        assert!(reject(200, r#"{"success":true,"message":"m"}"#).is_ok());
    }

    #[test]
    fn parse_json_body_with_status_carries_the_status() {
        #[derive(Debug, serde::Deserialize)]
        #[allow(dead_code)]
        struct Row {
            id: u32,
        }
        for status in [200, 201, 299] {
            match parse_json_body_with_status::<Row>(status, r#"{"id":"x"}"#) {
                Err(ZBError::ApiError { status: got, message }) => {
                    assert_eq!(got, status);
                    assert_eq!(message, UNEXPECTED_JSON_SHAPE_MESSAGE);
                }
                other => panic!("{other:?}"),
            }
        }
        match parse_json_body::<Row>(r#"[1]"#) {
            Err(ZBError::ApiError { status: 200, .. }) => {}
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn a_status_body_must_be_a_real_status_object() {
        for body in ["{}", "{ }", "[]", r#"[{"file_status":"x"}]"#, "0", r#""x""#, "null",
            r#"{"message":"m"}"#, r#"{"Message":null}"#, r#"{"error":""}"#] {
            match require_status_object_body(203, JSON, body) {
                Err(ZBError::ApiError { status: 203, .. }) => {}
                other => panic!("{body}: {other:?}"),
            }
        }
        assert!(require_status_object_body(200, JSON, r#"{"success":true,"file_status":"Complete"}"#).is_ok());
        assert!(require_status_object_body(200, JSON, r#"{"file_status":"Processing"}"#).is_ok());
    }
}