xapi-rs 0.2.0

A conformant LRS implementation of xAPI 2.0.0
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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
// SPDX-License-Identifier: GPL-3.0-or-later

#![allow(non_snake_case)]

mod utils;

use chrono::{DateTime, Utc};
use rocket::{
    http::{ContentType, Header, Status, hyper::header},
    serde::json::from_str,
    uri,
};
use std::str::FromStr;
use test_context::test_context;
use tracing_test::traced_test;
use utils::{
    BOUNDARY, CR_LF, MyTestContext, accept_json, authorization, boundary_delimiter_line,
    content_type, multipart, read_to_string, v2,
};
use uuid::{Uuid, uuid};
use xapi_data::{
    MyEmailAddress, MyLanguageTag, Statement, StatementIDs, StatementResult, Validate, Vocabulary,
    adl_verb,
};
use xapi_rs::{CONSISTENT_THRU_HDR, MyError, config, resources};

/// IMPORTANT (rsn) 20240412 - while xAPI [1] states that... "If used, an
/// LRP should send a Timestamp with at least millisecond precision (3
/// decimal points beyond seconds)", some examples incl. the one dubbed
/// _Simple Statement_ used here shows a timestamp w/ no millis at all;
/// i.e. "2015-11-18T12:17:00+00:00".
///
/// We **always** output timestamps w/ milli-seconds precision even when
/// that value is zero. For this reason when comparing the result of
/// serializing a Statement we generate w/ a representation of the result
/// we use a timestamp **with** milli-seconds. So if we deserialize that
/// _Simple Statement_ containing a timestamp like the above, we compare
/// it to "2015-11-18T12:17:00.000+00:00" to assert equality.
///
#[test]
fn test_display() {
    const DISPLAY: &str = r#"Statement{ id: "fd41c918-b88b-4b20-a0a5-a4c32391aaa0", actor: Agent{ name: "Project Tin Can API", mbox: "user@example.com" }, verb: Verb{ id: "http://example.com/xapi/verbs#sent-a-statement", display: {"en-US":"sent"} }, object: Activity{ id: "http://example.com/xapi/activity/simplestatement", definition: ActivityDefinition{ name: {"en-US":"simple statement"}, description: {"en-US":"A simple Experience API statement. Note that the LRS \ndoes not need to have any prior information about the Actor (learner), the \nverb, or the Activity/object."} } }, timestamp: "2015-11-18T12:17:00.000Z" }"#;

    let json = read_to_string("statement-simple", true);

    let de_result = serde_json::from_str::<Statement>(&json);
    assert!(de_result.is_ok());
    let st = de_result.unwrap();
    let display = format!("{}", st);
    assert_eq!(display, DISPLAY);
}

#[test]
fn test_unicode() -> Result<(), MyError> {
    let json = read_to_string("statement-103-unicode", true);

    let de_result = serde_json::from_str::<Statement>(&json);
    assert!(de_result.is_ok());

    let st = de_result.unwrap();
    let v = st.verb();

    let gb = MyLanguageTag::from_str("en-GB")?;
    let jp = MyLanguageTag::from_str("ja-JP")?;
    let kn = MyLanguageTag::from_str("kn-IN")?;
    let ar = MyLanguageTag::from_str("ar-EG")?;

    assert!(v.display(&MyLanguageTag::from_str("en")?).is_none());
    assert!(v.display(&gb).is_some());
    assert!(v.display(&MyLanguageTag::from_str("en-US")?).is_some());
    assert!(v.display(&jp).is_some());
    assert!(v.display(&MyLanguageTag::from_str("ko-KR")?).is_some());
    assert!(v.display(&MyLanguageTag::from_str("is-IS")?).is_some());
    assert!(v.display(&MyLanguageTag::from_str("ru-RU")?).is_some());
    assert!(v.display(&MyLanguageTag::from_str("pa-IN")?).is_some());
    assert!(v.display(&MyLanguageTag::from_str("sk-SK")?).is_some());
    assert!(v.display(&ar).is_some());
    assert!(v.display(&MyLanguageTag::from_str("hy-AM")?).is_some());
    assert!(v.display(&kn).is_some());

    assert_eq!(v.display(&gb).unwrap(), "attended");
    assert_eq!(v.display(&jp).unwrap(), "出席した");
    assert_eq!(v.display(&ar).unwrap(), "حضر");
    assert_eq!(v.display(&kn).unwrap(), "ಹಾಜರಿದ್ದರು");

    Ok(())
}

#[traced_test]
#[test]
fn test_validate() {
    let json = read_to_string("statement-long", true);

    let de_result = serde_json::from_str::<Statement>(&json);
    assert!(de_result.is_ok());
    let st = de_result.unwrap();

    let res = st.validate();
    assert!(res.is_empty());
}

#[test]
fn test_serde() -> Result<(), MyError> {
    const S1: &str = r#"{
        "id":"01919422-a115-7121-99e5-88d5486ad5f4",
        "actor":{ "objectType":"Agent", "name":"xAPI account", "mbox":"mailto:xapi@adlnet.gov" },
        "verb":{
            "id":"http://adlnet.gov/expapi/verbs/attended",
            "display":{ "en-GB":"attended","en-US":"attended" }
        },
        "object":{ "objectType":"Activity", "id":"http://www.example.com/meetings/occurances/34534" },
        "attachments":[{
            "usageType":"http://example.com/attachment-usage/test",
            "display":{ "en-US":"A test attachment" },
            "description":{ "en-US":"A test attachment (description)" },
            "contentType":"text/plain; charset=ascii",
            "length":27,
            "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a",
            "fileUrl":"http://over.there.com/file.txt"
        }]}"#;

    let s = serde_json::from_str::<Statement>(S1).unwrap();

    let uuid = s.id().unwrap();
    let expected = Uuid::from_str("01919422-a115-7121-99e5-88d5486ad5f4").unwrap();
    assert_eq!(uuid, &expected);

    let actor = s.actor();
    assert!(actor.is_agent());
    let a = actor.as_agent()?;
    assert_eq!(a.name_as_str(), Some("xAPI account"));
    assert_eq!(
        a.mbox(),
        MyEmailAddress::from_str("xapi@adlnet.gov").ok().as_ref()
    );

    let v = s.verb();
    let attended = adl_verb(Vocabulary::Attended);
    // they'll differ in their 'display' value...
    assert_ne!(v, attended);
    // but they should be Equivalent...
    assert_eq!(v.uid(), attended.uid());
    // or differently put...
    assert!(v.equivalent(attended));

    let object = s.object();
    assert!(object.is_activity());
    let act = object.as_activity()?;
    assert_eq!(
        act.id_as_str(),
        "http://www.example.com/meetings/occurances/34534"
    );

    assert_eq!(s.attachments().len(), 1);
    let att = &s.attachments()[0];
    assert_eq!(
        att.file_url_as_str(),
        Some("http://over.there.com/file.txt")
    );
    assert_eq!(att.length(), 27);

    Ok(())
}

#[traced_test]
#[test]
fn test_agent_0_ifi() {
    const S: &str = r#"{
"actor":{"objectType":"Agent"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    assert!(Statement::from_str(S).is_err());
}

#[traced_test]
#[test]
fn test_agent_w_gt_1_ifi() {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI mbox","mbox":"mailto:xapi@adlnet.gov","account":{"homePage":"http://www.example.com","name":"xAPI account name"}},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    assert!(Statement::from_str(S).is_err());
}

#[traced_test]
#[test]
fn test_group_w_0_agents() {
    const S: &str = r#"{
"actor":{"objectType":"Group","name":"Group Anonymous"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-US":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    assert!(Statement::from_str(S).is_err());
}

#[traced_test]
#[test]
fn test_ctx_agents_is_vec() {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"mailto:xapi@adlnet.gov"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-US":"attended"}},
"object":{
  "objectType":"SubStatement",
  "actor":{"objectType":"Agent","name":"xAPI account","mbox":"mailto:xapi@adlnet.gov"},
  "verb":{"id":"http://adlnet.gov/expapi/verbs/reported","display":{"en-US":"reported"}},
  "context":{"contextAgents":[{
    "objectType":"contextAgent",
    "agent":{"objectType":"Agent","mbox":"mailto:player-1@example.com"},
    "relevantTypes":[
      "https://example.com/xapi/american-footbal/activity-types/personnel/player",
      "https://example.com/xapi/american-footbal/activity-types/position/quarterback"
    ]}]},
  "object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}}"#;

    let s = Statement::from_str(S);
    assert!(s.is_ok());
    let s = s.unwrap();
    assert!(s.object().is_sub_statement());
    let ss = s.object().as_sub_statement().unwrap();
    assert!(ss.context().is_some());
    let ss_ctx = ss.context().unwrap();
    assert!(ss_ctx.context_agents().map_or(false, |x| x.len() == 1))
}

#[test]
fn test_actor_group() {
    const S: &str = r#"{
"actor":{
  "objectType":"Group",
  "name":"Group Anonymous",
  "member":[{"objectType":"Agent","name":"xAPI mbox","mbox":"mailto:xapi@adlnet.gov"}]
},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended","en-US":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    let s = Statement::from_str(S);
    assert!(s.is_ok());
    let s = s.unwrap();
    let actor = s.actor();
    assert!(actor.is_group());
    let group = actor.as_group().unwrap();
    assert_eq!(group.members().len(), 1);
}

#[traced_test]
#[test]
fn test_authority_group() {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"mailto:xapi@adlnet.gov"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended"}},
"authority":{
  "objectType":"Group",
  "member":[
    {"account":{"homePage":"http://example.com/xAPI/OAuth/Token","name":"oauth_consumer_x75db"}},
    {"mbox":"mailto:bob_authority@example.com"}
  ]},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    let s = Statement::from_str(S);
    assert!(s.is_ok());
    let s = s.unwrap();
    assert!(s.authority().is_some());
    let auth = s.authority().unwrap();
    assert!(auth.is_group());
    let auth_group = auth.as_group().unwrap();
    assert_eq!(auth_group.members().len(), 2);
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_non_matching_uuid(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"id":"fd41c918-b88b-4b20-a0a5-a4c32391aaa0",
"timestamp":"2015-11-18T12:17:00+00:00",
"actor":{"objectType":"Agent","name":"Project Tin Can API","mbox":"mailto:user@example.com"},
"verb":{"id":"http://example.com/xapi/verbs#sent-a-statement","display":{"en-US":"sent"}},
"object":{
    "id":"http://example.com/xapi/activity/simplestatement",
    "definition":{
    "name":{"en-US":"simple statement"},
    "description":{"en-US":"A simple Experience API statement."}}}}"#;

    let client = &ctx.client;

    // `statementId` query parameter must be the same as the Statement's `id` property...
    let req = client
        .put(uri!(
            "/statements",
            resources::statement::put_json(statementId = "fd41c918b88b4b20a0a5a4c32391aaa1")
        ))
        .body(S)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

// NOTE (rsn) 20250113 - i changed the Statement JSON data to include an
// `authority` property.  this was done to ensure the resulting ETag becomes
// an invariant whatever the mode is in use or not.
#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_etag(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"id":"fd41c918-b88b-4b20-a0a5-a4c32391aaa0",
"timestamp":"2015-11-18T12:17:00+00:00",
"actor":{"objectType":"Agent","name":"Project Tin Can API","mbox":"mailto:user@example.com"},
"verb":{"id":"http://example.com/xapi/verbs#sent-a-statement","display":{"en-US":"sent"}},
"authority":{"objectType":"Agent", "mbox":"mailto:bob_authority@example.com"},
"object":{
    "id":"http://example.com/xapi/activity/simplestatement",
    "definition":{
    "name":{"en-US":"simple statement"},
    "description":{"en-US":"A simple Experience API statement."}}}}"#;

    let client = &ctx.client;

    // PUT must return an etag.
    let req1 = client
        .put(uri!(
            "/statements",
            resources::statement::put_json(statementId = "fd41c918b88b4b20a0a5a4c32391aaa0")
        ))
        .body(S)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp1 = req1.dispatch();
    assert_eq!(resp1.status(), Status::NoContent);
    let etag_hdr = resp1.headers().get_one(header::ETAG.as_str());
    assert!(etag_hdr.is_some());
    let etag = etag_hdr.unwrap();
    assert_eq!(etag, "\"523-254282048567927318730551152372839811817\"");

    Ok(())
}

fn att_missing_cte() -> Vec<u8> {
    let mut result = vec![];

    result.extend_from_slice(b"Content-Type: text/plain; charset=ascii\r\n");
    result.extend_from_slice(b"X-Experience-API-Hash: 495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a\r\n");
    result.extend_from_slice(CR_LF);
    result.extend_from_slice(b"here is a simple attachment");

    result
}

fn att_bad_cte() -> Vec<u8> {
    let mut result = vec![];

    result.extend_from_slice(b"Content-Type: text/plain; charset=ascii\r\n");
    result.extend_from_slice(b"Content-Transfer-Encoding: gzip\r\n");
    result.extend_from_slice(b"X-Experience-API-Hash: 495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a\r\n");
    result.extend_from_slice(CR_LF);
    result.extend_from_slice(b"here is a simple attachment");

    result
}

fn att_ok1() -> Vec<u8> {
    let mut result = vec![];

    result.extend_from_slice(b"Content-Type: text/plain; charset=ascii\r\n");
    result.extend_from_slice(b"Content-Transfer-Encoding: binary\r\n");
    result.extend_from_slice(b"X-Experience-API-Hash: 495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a\r\n");
    result.extend_from_slice(CR_LF);
    result.extend_from_slice(b"here is a simple attachment");

    result
}

fn att_ok2() -> Vec<u8> {
    let mut result = vec![];

    result.extend_from_slice(b"Content-Type: text/plain\r\n");
    result.extend_from_slice(b"Content-Transfer-Encoding: binary\r\n");
    result.extend_from_slice(b"X-Experience-API-Hash: 7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a\r\n");
    result.extend_from_slice(CR_LF);
    result.extend_from_slice(b"here is another simple attachment");

    result
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_missing_cte(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"mbox":"mailto:sample.agent@example.com","name":"Sample Agent","objectType":"Agent"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/answered","display":{"en-US":"answered"}},
"object":{
    "id":"http://www.example.com/tincan/activities/multipart",
    "objectType":"Activity",
    "definition":{
    "name":{"en-US":"Multi Part Activity"},
    "description":{"en-US":"Multi Part Activity Description"}
    }
},
"attachments":[{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment"},
    "description":{"en-US":"A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"
}]}"#;

    let client = &ctx.client;

    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_missing_cte()), None);
    let req = client
        .put(uri!(
            "/statements",
            resources::statement::put_mixed(statementId = "fd41c918b88b4b20a0a5a4c32391aaa0")
        ))
        .body(body)
        .header(Header::new(
            header::CONTENT_TYPE.as_str(),
            header.to_string(),
        ))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should fail b/c of missing Content-Type-Encoding
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_bad_cte(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"mbox":"mailto:sample.agent@example.com","name":"Sample Agent","objectType":"Agent"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/answered","display":{"en-US":"answered"}},
"object":{
    "id":"http://www.example.com/tincan/activities/multipart",
    "objectType":"Activity",
    "definition":{
    "name":{"en-US":"Multi Part Activity"},
    "description":{"en-US":"Multi Part Activity Description"}
    }
},
"attachments":[{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment"},
    "description":{"en-US":"A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"
}]}"#;

    let client = &ctx.client;

    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_bad_cte()), None);
    let req = client
        .put(uri!(
            "/statements",
            resources::statement::put_mixed(statementId = "fd41c918b88b4b20a0a5a4c32391aaa0")
        ))
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should fail b/c of Content-Type-Encoding MUST be binary
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_missing_cl(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"Sample Agent","mbox":"mailto:sample.agent@example.com"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/answered","display":{"en-US":"answered"}},
"object":{
    "objectType":"Activity",
    "id":"http://www.example.com/tincan/activities/multipart",
    "definition":{
    "name":{"en-US":"Multi Part Activity"},
    "description":{"en-US":"Multi Part Activity Description"}
    }
},
"attachments":[{
    "usageType": "http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment" },
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a",
    "fileUrl":"http://somewhere.com/here"
},{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment"},
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain",
    "length":100,
    "sha2":"7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a",
    "fileUrl":"https://somewhere.com/there"
}]}"#;

    let client = &ctx.client;

    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_ok1()), Some(att_ok2()));
    let req = client
        .put(uri!(
            "/statements",
            resources::statement::put_mixed(statementId = "fd41c918b88b4b20a0a5a4c32391aaa0")
        ))
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should succeed even if actual bytes count of 2nd attachment is not
    // equal to its corresponding Attachment's 'length' property value.
    assert_eq!(resp.status(), Status::NoContent);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_get_json(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S1: &str = r#"{
"actor":{"objectType":"Agent","name":"Sample Agent","mbox":"mailto:sample.agent@example.com"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/answered","display":{"en-US":"answered"}},
"object":{
    "objectType":"Activity",
    "id":"http://www.example.com/tincan/activities/multipart",
    "definition":{
    "name":{"en-US":"Multi Part Activity"},
    "description":{"en-US":"Multi Part Activity Description"}
    }
},
"attachments":[{
    "usageType": "http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment" },
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a",
    "fileUrl":"http://somewhere.com/here"
},{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment"},
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain",
    "length":100,
    "sha2":"7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a",
    "fileUrl":"https://somewhere.com/there"
}]}"#;
    // this one is different than S1 in that its 2nd Attachment has no fileUrl
    const S2: &str = r#"{
"actor":{"objectType":"Agent","name":"Sample Agent","mbox":"mailto:sample.agent@example.com"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/answered","display":{"en-US":"answered"}},
"object":{
    "objectType":"Activity",
    "id":"http://www.example.com/tincan/activities/multipart",
    "definition":{
    "name":{"en-US":"Multi Part Activity"},
    "description":{"en-US":"Multi Part Activity Description"}
    }
},
"attachments":[{
    "usageType": "http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment" },
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"
},{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US": "A test attachment"},
    "description":{"en-US": "A test attachment (description)"},
    "contentType":"text/plain",
    "length":100,
    "sha2":"7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a"
}]}"#;

    let client = &ctx.client;

    // 1. POST a Statement w/ 2 Attachments...
    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S1, Some(att_ok1()), Some(att_ok2()));
    // work w/ timestamps in millis.  subtract 1 since we don't suffer network
    // lags when testing...
    let now = Utc::now().timestamp_millis() - 1;
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ the ID(s) of the now persisted Statement(s) and
    // a Consistent-Through xAPI header...
    assert_eq!(resp.status(), Status::Ok);
    let consistent_thru_hdr = resp.headers().get_one(CONSISTENT_THRU_HDR);
    assert!(consistent_thru_hdr.is_some());
    let timestamp = DateTime::parse_from_rfc3339(consistent_thru_hdr.unwrap())
        .unwrap()
        .timestamp_millis();
    assert!(now < timestamp);

    let json = resp.into_string().unwrap();
    let uuids = serde_json::from_str::<StatementIDs>(&json).unwrap().0;
    assert_eq!(uuids.len(), 1);
    let uuid = uuids[0];

    // 2. GET statements w/o Attachments...
    let req = client
        .get("/statements/?attachments=false")
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ the ID(s) of the now persisted Statement(s) and
    // a Last-Modified header that is the same as the 'stored' of the
    // returned Statement
    assert_eq!(resp.status(), Status::Ok);

    let last_modified_hdr = resp.headers().get_one(header::LAST_MODIFIED.as_str());
    let last_modified = DateTime::parse_from_rfc3339(last_modified_hdr.unwrap())
        .unwrap()
        .timestamp_millis();

    let json = resp.into_string().unwrap();
    // should be a serialized StatementResult w/ 1 Statement...
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert_eq!(sr.statements().len(), 1);
    // did we get the right Statement?
    let received = &sr.statements()[0];
    assert_eq!(received.id().unwrap(), &uuid);
    // but is it (equivalent to) what we POSTed?
    let sent: Statement = serde_json::from_str(&S2).unwrap();
    assert!(sent.equivalent(received));

    // now check the 'stored' property...
    let stored = received.stored().unwrap().timestamp_millis();
    assert_eq!(stored, last_modified);

    // 3. now try again but this time include the attachments...
    let req = client
        .get("/statements/?attachments=true")
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    let multipart = resp.into_string().unwrap();
    // debug!("multipart:{}", multipart);
    // at least check that we got the right number of parts...
    // start by collecting the CRLF indices in 'multipart'
    let n: Vec<_> = multipart.match_indices("\r\n").map(|(x, _)| x).collect();
    // usually there's a CRLF at the begining of a multipart...
    assert_eq!(n[0], 0);
    // what's between the first 2 CRLFs is the boundary...
    let boundary = &multipart[n[0]..n[1]];
    let m: Vec<_> = multipart.match_indices(boundary).map(|(x, _)| x).collect();
    // stream starts + ends w/ a boundary => there should be 3 parts
    let parts_count = m.len() - 1;
    assert_eq!(parts_count, 3);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_voiding(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const UUID: Uuid = uuid!("1dc85813-a334-48cc-b196-4c6e798599b8");

    /// A valid Statement w/ a known UUID.  will be voided later...
    const SV1: &str = r#"{
"id":"1dc85813-a334-48cc-b196-4c6e798599b8",
"actor":{"objectType":"Agent","name":"agent 99","mbox":"a99@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended"},
"object":{"id":"http://www.example.com/meetings/occurances/34534"}
}"#;

    /// An invalid voiding Statement (uses 'voided' Verb but not a StatementRef
    /// object).
    const BAD_S1: &str = r#"{
"actor":{"objectType":"Agent","name":"agent 86","mbox":"a86@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/voided"},
"object":{"id":"http://www.example.com/ceremony/ref/101"}
}"#;

    /// A valid voiding Statement that correctly voids SV1.
    const SV2: &str = r#"{
"id":"0192079d-686a-7160-b519-98f519974e50",
"actor":{"objectType":"Agent","name":"agent 86","mbox":"a86@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/voided"},
"object":{"objectType":"StatementRef","id":"1dc85813-a334-48cc-b196-4c6e798599b8"}
}"#;

    /// An invalid voiding Statement; it tries to void SV2 (a voiding Statement).
    const BAD_S2: &str = r#"{
"actor":{"objectType":"Agent","name":"bond","mbox":"a007@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/voided"},
"object":{"objectType":"StatementRef","id":"0192079d-686a-7160-b519-98f519974e50"}
}"#;

    let client = &ctx.client;
    // always use timestamp in millis - 1 since in tests there's no network lag
    let now = Utc::now().timestamp_millis() - 1;

    // 1. POST SV1: a Statement w/ a know UUID...
    let req = client
        .post("/statements")
        .body(SV1)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ the ID(s) of the now persisted Statement(s) and
    // a Consistent-Through xAPI header...
    assert_eq!(resp.status(), Status::Ok);
    let consistent_thru_hdr = resp.headers().get_one(CONSISTENT_THRU_HDR);
    assert!(consistent_thru_hdr.is_some());
    let timestamp = DateTime::parse_from_rfc3339(consistent_thru_hdr.unwrap())
        .unwrap()
        .timestamp_millis();
    assert!(now < timestamp);
    // ensure we get back expected UUID...
    let uuids = resp
        .into_json::<StatementIDs>()
        .expect("Failed deserializing array of UUIDs")
        .0;
    assert_eq!(uuids.len(), 1);
    assert_eq!(uuids[0], UUID);

    // 2. GET UUID1 should return the original SV1
    let req = client
        .get("/statements/?statementId=1dc85813a33448ccb1964c6e798599b8")
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);
    let s1 = resp
        .into_json::<Statement>()
        .expect("Failed deserializing Statement");
    assert_eq!(&UUID, s1.id().unwrap());
    assert!(s1.equivalent(&from_str::<Statement>(SV1).unwrap()));

    // 3. POST BAD_S1.  should fail...
    let req = client
        .post("/statements")
        .body(BAD_S1)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    // 4. to be sure, to be sure... GET UUID1 as voidedStatementId should return
    // nada
    let req = client
        .get("/statements/?voidedStatementId=1dc85813a33448ccb1964c6e798599b8")
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::NotFound);

    // 5. POST a valid voiding statement: SV2.  should succeed...
    let req = client
        .post("/statements")
        .body(SV2)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    // 6. GET w/ statementId = UUID1 should now fail...
    let req = client
        .get("/statements/?statementId=1dc85813a33448ccb1964c6e798599b8")
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::NotFound);

    // 7. but GET w/ voidedStatementId = UUID1 should now succeed...
    let req = client
        .get("/statements/?voidedStatementId=1dc85813a33448ccb1964c6e798599b8")
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    // 8. finally voiding SV2 (a voiding statement) should fail...
    let req = client
        .post("/statements")
        .body(BAD_S2)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_more(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const UUID1: Uuid = uuid!("d9b1130919c14bf68b20f90c8796406a");
    const UUID2: Uuid = uuid!("bb2e8574ca7b4e208dd0425ff97ad0ca");
    const UUID3: Uuid = uuid!("0469dd9555c24a99ae1ef755914c883c");

    const S: &str = r#"[{
"id":"d9b11309-19c1-4bf6-8b20-f90c8796406a",
"actor":{"objectType":"Agent","name":"xAPI mbox","mbox":"a99@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/test/more/target/one","display":{"en":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}
},{
"id":"bb2e8574-ca7b-4e20-8dd0-425ff97ad0ca",
"actor":{"objectType":"Agent","name":"xAPI mbox","mbox":"a86@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/test/more/target/two","display":{"en-US":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}
},{
"id":"0469dd95-55c2-4a99-ae1e-f755914c883c",
"actor":{"objectType":"Agent","name":"xAPI mbox","mbox":"a007@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}
}]"#;

    let client = &ctx.client;
    let now = Utc::now().timestamp_millis() - 1;

    // 1. POST 3 statements w/ know UUIDs...
    let req = client
        .post("/statements")
        .body(S)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ the ID(s) of the now persisted Statement(s) and
    // a Consistent-Through xAPI header...
    assert_eq!(resp.status(), Status::Ok);
    let consistent_thru_hdr = resp.headers().get_one(CONSISTENT_THRU_HDR);
    assert!(consistent_thru_hdr.is_some());
    let timestamp = DateTime::parse_from_rfc3339(consistent_thru_hdr.unwrap())
        .unwrap()
        .timestamp_millis();
    assert!(now < timestamp);
    // ensure we get back all 3 UUIDs...
    let mut uuids = resp
        .into_json::<StatementIDs>()
        .expect("Failed deserializing array of UUIDs")
        .0;
    assert_eq!(uuids.len(), 3);
    assert!(uuids.contains(&UUID1));
    assert!(uuids.contains(&UUID2));
    assert!(uuids.contains(&UUID3));

    // 2. GET all Statements w/ a `limit` of 1 so we can exercise `more`...
    let req = client
        .get("/statements/?limit=1")
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ a StatementResult w/ non-null more...
    assert_eq!(resp.status(), Status::Ok);
    let json = resp.into_string().unwrap();
    // should be a StatementResult w/ 1 Statement and more field...
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert_eq!(sr.statements().len(), 1);
    // did we get expected UUID?
    let received = &sr.statements()[0];
    let id1 = received.id().unwrap();
    assert!(uuids.contains(id1));
    // now remove it...
    uuids.retain(|x| x != id1);
    // did we get more?
    assert!(sr.more().is_some());
    let more_url = sr.more().expect("Missing 1st 'more' URL");
    assert!(more_url.as_str().starts_with(&config().external_url));

    // 3. GET using the returned 'more' URL; should fetch another Statement...
    // translate `more_url` to a local URL...
    let url = more_url.as_str().replace(&config().external_url, "");
    let req = client
        .get(&url)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    // should return something simiar to the previous call...
    assert_eq!(resp.status(), Status::Ok);
    let json = resp.into_string().unwrap();
    // should be a StatementResult w/ 1 Statement and more field...
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert_eq!(sr.statements().len(), 1);
    // did we get expected UUID?
    let received = &sr.statements()[0];
    let id2 = received.id().unwrap();
    assert!(uuids.contains(id2));
    uuids.retain(|x| x != id2);
    assert!(sr.more().is_some());
    let more_url = sr.more().expect("Missing 2nd 'more' URL");
    assert!(more_url.as_str().starts_with(&config().external_url));

    // 4. finally GET the last Statement.  should be ok w/ no `more` URL...
    let url = more_url.as_str().replace(&config().external_url, "");
    let req = client
        .get(&url)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);
    let json = resp.into_string().unwrap();
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert_eq!(sr.statements().len(), 1);
    // did we get expected UUID?
    let received = &sr.statements()[0];
    let id3 = received.id().unwrap();
    assert!(uuids.contains(id3));
    assert!(sr.more().is_none());

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_get_params(ctx: &mut MyTestContext) -> Result<(), MyError> {
    let client = &ctx.client;

    // a call w/ no query string parameters is ok...
    let req = client
        .get("/statements/")
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);
    let json = resp.into_string().unwrap();
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert!(sr.statements().is_empty());

    // so is one w/ valid parameters...
    let req = client
        .get("/statements/?attachments=false")
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);
    let json = resp.into_string().unwrap();
    let sr: StatementResult = serde_json::from_str(&json).unwrap();
    assert!(sr.statements().is_empty());

    // but not one w/ unknown or wrong-cased parameters...
    let req = client
        .get("/statements/?Attachments=false&foo=bar")
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    // so is one that includes valid and invalid parameters...
    let req = client
        .get("/statements/?format=canonical&Ascending=false")
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_missing_attachment(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"a86@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended","en-US":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"},
"attachments":[{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US":"A test attachment"},
    "description":{"en-US":"A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"
},{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US":"A test attachment"},
    "description":{"en-US":"A test attachment (description)"},
    "contentType":"text/plain",
    "length":33,
    "sha2":"7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a"
}]}"#;

    let client = &ctx.client;

    // POST a Statement w/ 2 Attachments but include contents of only one...
    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_ok1()), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_wrong_attachment(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"a86@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended","en-US":"attended"}},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"},
"attachments":[{
    "usageType":"http://example.com/attachment-usage/test",
    "display":{"en-US":"A test attachment"},
    "description":{"en-US":"A test attachment (description)"},
    "contentType":"text/plain; charset=ascii",
    "length":27,
    "sha2":"b018994f8bbe0f08992a65c48c8c8c56f09e9baceaa6227ed85c90ae52b73c89"
}]}"#;

    let client = &ctx.client;

    // POST a Statement w/ 1 Attachment and 1 binary part w/ wrong hash...
    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_ok1()), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_invalid_statement(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"BOND James","mbox":"mailto:a007@xapi.net"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended"}},
"context":{
    "registration":"ec531277-b57b-4c15-8d91-d292c5b2b8f7",
    "instructor":{
    "objectType":"Agent",
    "name":"Ian Fleming",
    "mbox_sha1sum":"cd9b00a5611f94eaa7b1661edab976068e374975",
    "openid":"http://openid.com/424242"
    },
    "platform":"Virtual meeting",
    "language":"fr-CA",
    "statement":{"objectType":"StatementRef","id":"6690e6c9-3ef0-4ed3-8b37-7f3964730bff"}},
"object":{"objectType":"Activity","id":"http://www.example.org/meetings/123456"}
}"#;

    let client = &ctx.client;

    // POST Statement w/ invalid Context should fail...
    let req = client
        .post("/statements")
        .body(S)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_canonical_fmt(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"id":"01928d4f-487c-7a72-a38b-f5097c07d203",
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"mailto:xapi@adlnet.gov"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended","display":{"en-GB":"attended","en-US":"attended"}},
"context":{
  "registration":"ec531277-b57b-4c15-8d91-d292c5b2b8f7",
  "platform":"Example virtual meeting software",
  "language":"tlh",
  "statement":{"objectType":"StatementRef","id":"6690e6c9-3ef0-4ed3-8b37-7f3964730bee"},
  "contextActivities":{
    "category":{
      "objectType":"Activity",
      "id":"http://www.example.com/test/array/statements/pri",
      "definition":{
        "name":{"en-GB":"example meeting","en-US":"example meeting"},
        "description":{
          "en-GB":"An example meeting.",
          "en-US":"An example meeting with certain people present."
        },
        "moreInfo":"http://virtualmeeting.example.com/345256",
        "extensions":{"http://example.com/profiles/meetings/extension/location":"X:\\\\meetings\\\\minutes\\\\examplemeeting.one","http://example.com/profiles/meetings/extension/reporter":{"name":"Thomas","id":"http://openid.com/342"}}
      }
    }
  },
  "instructor":{"objectType":"Agent","name":"xAPI mbox","mbox":"mailto:pri@adlnet.gov"}
},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"}}"#;

    let client = &ctx.client;

    // 1. POST Statement w/ Context containing LMs w/ 2 LTs...
    let req = client
        .post("/statements")
        .body(S)
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    // 2. GET w/ canonical format + an Accept-Language header should be honored...
    let req = client
        .get("/statements/?statementId=01928d4f487c7a72a38bf5097c07d203&format=canonical")
        .header(ContentType::JSON)
        .header(accept_json())
        .header(v2())
        .header(Header::new(header::ACCEPT_LANGUAGE.as_str(), "en-US"))
        .header(authorization());

    let resp = req.dispatch();
    // should return OK w/ the ID(s) of the now persisted Statement(s) and
    // a Consistent-Through xAPI header...
    assert_eq!(resp.status(), Status::Ok);

    let received = resp.into_json::<Statement>().unwrap();
    // everything LM in the single activity under contextActivities/category
    // should now contain entries for en-US LT and none for en-GB...
    let definition = received
        .context()
        .unwrap()
        .context_activities()
        .unwrap()
        .category()[0]
        .definition()
        .unwrap();
    let us = MyLanguageTag::from_str("en-US").unwrap();
    let gb = MyLanguageTag::from_str("en-GB").unwrap();
    assert!(definition.name(&us).is_some());
    assert!(definition.name(&gb).is_none());
    assert!(definition.description(&us).is_some());
    assert!(definition.description(&gb).is_none());

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_multipart_wo_boundary(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = "foo";

    let client = &ctx.client;

    // POST with `multipart` content-type w/o boundary parameter...
    let (_, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_ok1()), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(Header::new(
            header::CONTENT_TYPE.as_str(),
            "multipart/mixed",
        ))
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_multipart_w_invalid_multipart_ct(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = "foo";

    let client = &ctx.client;

    // POST with syntactically incorrect `multipart` content-type...
    let (_, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, Some(att_ok1()), None);
    let req = client
        .post("/statements")
        .body(body)
        .header(Header::new(
            header::CONTENT_TYPE.as_str(),
            "multipart/mixed;", // notice ';'
        ))
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::BadRequest);

    Ok(())
}

#[test_context(MyTestContext)]
#[traced_test]
#[test]
fn test_attachment_w_file_url(ctx: &mut MyTestContext) -> Result<(), MyError> {
    const S: &str = r#"{
"actor":{"objectType":"Agent","name":"xAPI account","mbox":"mailto:xapi@adlnet.gov"},
"verb":{"id":"http://adlnet.gov/expapi/verbs/attended"},
"object":{"objectType":"Activity","id":"http://www.example.com/meetings/occurances/34534"},
"attachments":[{
  "usageType":"http://example.com/attachment-usage/test",
  "display":{"en-US":"A test attachment"},
  "description":{"en-US":"A test attachment (description)"},
  "contentType":"text/plain; charset=ascii",
  "length":27,
  "sha2":"495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a",
  "fileUrl":"http://over.there.com/file.txt"}
]}"#;

    let client = &ctx.client;

    // POST a Statement w/ 1 Attachment and no additional parts...
    let (header, delimiter) = boundary_delimiter_line(BOUNDARY);
    let body = multipart(&delimiter, S, None, None);
    let req = client
        .post("/statements")
        .body(body)
        .header(content_type(&header))
        .header(accept_json())
        .header(v2())
        .header(authorization());

    let resp = req.dispatch();
    assert_eq!(resp.status(), Status::Ok);

    Ok(())
}