forked from webmachine/webmachine
/
decision_core_test.erl
1583 lines (1397 loc) · 62.3 KB
/
decision_core_test.erl
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
%% @author Macneil Shonle <mshonle@basho.com>
%% @copyright 2007-2014 Basho Technologies
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(decision_core_test).
-ifdef(TEST).
-include("wm_compat.hrl").
-include("wm_reqdata.hrl").
-include_lib("eunit/include/eunit.hrl").
-export([size_stream_raises_error/2, process_post_for_created_p11/3, get_streamed_body/2, send_streamed_body/2,
accept_text/2, writer_response/2, known_length_body/2, range_response/2, stream_content_md5/0,
validate_checksum_for_md5stream/3, process_post_for_md5_stream/3, init/1, service_available/2,
validate_content_checksum/2, is_authorized/2, allowed_methods/2, known_methods/2, uri_too_long/2,
known_content_type/2, valid_entity_length/2, malformed_request/2, forbidden/2, valid_content_headers/2,
content_types_provided/2, content_types_accepted/2, language_available/2, charsets_provided/2,
encodings_provided/2, resource_exists/2, generate_etag/2, last_modified/2, moved_permanently/2,
moved_temporarily/2, previously_existed/2, allow_missing_post/2, post_is_create/2, process_post/2,
create_path/2, is_conflict/2, multiple_choices/2, base_uri/2, base_uri_add_slash/1, expires/2,
delete_resource/2, delete_completed/2, to_html/2]).
-define(RESOURCE, atom_to_list(?MODULE)).
-define(RESOURCE_PATH, "/" ++ ?RESOURCE).
-define(HTML_CONTENT, "<html><body>Foo</body></html>").
-define(TEXT_CONTENT, ?HTML_CONTENT).
-ifndef(old_hash).
md5(Bin) ->
crypto:hash(md5,Bin).
-else.
md5(Bin) ->
crypto:md5(Bin).
-endif.
%% Suppress Erlang/OTP 21 warnings about the new method to retrieve
%% stacktraces.
-ifdef(OTP_RELEASE).
-compile({nowarn_deprecated_function, [{erlang, get_stacktrace, 0}]}).
-endif.
-define(HTTP_1_0_METHODS, ['GET', 'POST', 'HEAD']).
-define(HTTP_1_1_METHODS, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'TRACE',
'CONNECT', 'OPTIONS']).
-define(DEFAULT_ALLOWED_METHODS, ['GET', 'HEAD', 'PUT']).
%% It doesn't matter if the date is current, it just needs to be valid
-define(PRESENT_YEAR, 2013).
-define(FIRST_DAY_OF_PRESENT_YEAR, {{?PRESENT_YEAR, 1, 1}, {12, 0, 0}}).
-define(FIRST_DAY_OF_LAST_YEAR, {{?PRESENT_YEAR - 1, 1, 1}, {12, 0, 0}}).
-define(FIRST_DAY_OF_NEXT_YEAR, {{?PRESENT_YEAR + 1, 1, 1}, {12, 0, 0}}).
%% DECISION TRACE PATHS
%%
%% Not all possible paths will be tested at this point. The current testing
%% goal is decision/condition coverage: "It requires sufficient test cases that
%% each condition in a decision takes on all possible outcomes at least once,
%% and each point of entry is invoked at least once" (The Art of Software
%% Testing, 2nd, p 49). (Exercising all possible paths is called
%% multiple-condition coverage.)
-define(DECISION_ID_PATTERN, "v3[[:alpha:]]+\\d+[[:alpha:]]*").
-define(DECISION_ID_SUBSTATE_PATTERN, "v3[[:alpha:]]+\\d+[[:alpha:]]+").
%% B13-B3 -- All decision-trace-paths start at B13. There is a linear path
%% crossing the B nodes if you ignore substates.
-define(PATH_TO_B13, [v3b13]).
-define(PATH_TO_B12, ?PATH_TO_B13++[v3b12]).
-define(PATH_TO_B11, ?PATH_TO_B12++[v3b11]).
-define(PATH_TO_B10, ?PATH_TO_B11++[v3b10]).
-define(PATH_TO_B9, ?PATH_TO_B10++[v3b9]).
-define(PATH_TO_B8, ?PATH_TO_B9++[v3b8]).
-define(PATH_TO_B7, ?PATH_TO_B8++[v3b7]).
-define(PATH_TO_B6, ?PATH_TO_B7++[v3b6]).
-define(PATH_TO_B5, ?PATH_TO_B6++[v3b5]).
-define(PATH_TO_B4, ?PATH_TO_B5++[v3b4]).
-define(PATH_TO_B3, ?PATH_TO_B4++[v3b3]).
%% C3 - There is one path to state C3
-define(PATH_TO_C3, ?PATH_TO_B3++[v3c3]).
%% C4 - There is one path to state C4
-define(PATH_TO_C4, ?PATH_TO_C3++[v3c4]).
%% D4 - There are two paths to D4: via C3 or via C4
-define(PATH_TO_D4_VIA_C3, ?PATH_TO_C3++[v3d4]).
-define(PATH_TO_D4_VIA_C4, ?PATH_TO_C4++[v3d4]).
%% D5 - There are two paths to D5: via C3 or via C4
-define(PATH_TO_D5_VIA_C3, ?PATH_TO_D4_VIA_C3++[v3d5]).
-define(PATH_TO_D5_VIA_C4, ?PATH_TO_D4_VIA_C4++[v3d5]).
%% E5 - There are four paths to E5: via D5 (via C3 or via C4) or via D4 (via C3
%% or via C4). Only some of these paths are tested.
-define(PATH_TO_E5_VIA_D5_C3, ?PATH_TO_D5_VIA_C3++[v3e5]).
-define(PATH_TO_E5_VIA_D5_C4, ?PATH_TO_D5_VIA_C4++[v3e5]).
-define(PATH_TO_E5_VIA_D4_C3, ?PATH_TO_D4_VIA_C3++[v3e5]).
%% E6 - There are four paths to E6: via D5 (via C3 or via C4) or via D4 (via C3
%% or via C4). Only two of these paths to E6 are tested
-define(PATH_TO_E6_VIA_D5_C3, ?PATH_TO_E5_VIA_D5_C3++[v3e6]).
-define(PATH_TO_E6_VIA_D5_C4, ?PATH_TO_E5_VIA_D5_C4++[v3e6]).
%% F6 - Selection of the paths to F6
-define(PATH_TO_F6_VIA_E6_D5_C4, ?PATH_TO_E6_VIA_D5_C4++[v3f6]).
-define(PATH_TO_F6_VIA_E5_D4_C3, ?PATH_TO_E5_VIA_D4_C3++[v3f6]).
%% F7 - A path to F7
-define(PATH_TO_F7_VIA_E6_D5_C4, ?PATH_TO_F6_VIA_E6_D5_C4++[v3f7]).
%% G7 - The path to G7, without accept headers in the request
-define(PATH_TO_G7_VIA_F6_E6_D5_C4, ?PATH_TO_F6_VIA_E5_D4_C3++[v3g7]).
-define(PATH_TO_G7_NO_ACPTHEAD, ?PATH_TO_G7_VIA_F6_E6_D5_C4).
%% G9 - The path to G9, without accept headers in the request
-define(PATH_TO_G9_VIA_F6_E6_D5_C4,
?PATH_TO_G7_VIA_F6_E6_D5_C4++[v3g8,v3g9]).
%% G11 - The path to G11, without accept headers in the request
-define(PATH_TO_G11_VIA_F6_E6_D5_C4,
?PATH_TO_G7_VIA_F6_E6_D5_C4++[v3g8,v3g9,v3g11]).
-define(PATH_TO_G11_NO_ACPTHEAD, ?PATH_TO_G11_VIA_F6_E6_D5_C4).
%% H7 - The path to H7 without accept headers
-define(PATH_TO_H7_NO_ACPTHEAD, ?PATH_TO_G7_NO_ACPTHEAD++[v3h7]).
%% I7 - The path to I7 without accept headers
-define(PATH_TO_I7_NO_ACPTHEAD, ?PATH_TO_H7_NO_ACPTHEAD++[v3i7]).
%% I4 - The path to I4 without accept headers
-define(PATH_TO_I4_NO_ACPTHEAD, ?PATH_TO_I7_NO_ACPTHEAD++[v3i4]).
%% K7 - The path to K7 without accept headers
-define(PATH_TO_K7_NO_ACPTHEAD, ?PATH_TO_I7_NO_ACPTHEAD++[v3k7]).
%% L7 - The path to L7 without accept headers
-define(PATH_TO_L7_NO_ACPTHEAD, ?PATH_TO_K7_NO_ACPTHEAD++[v3l7]).
%% M7 - The path to M7 without accept headers
-define(PATH_TO_M7_NO_ACPTHEAD, ?PATH_TO_L7_NO_ACPTHEAD++[v3m7]).
%% N11 - Two paths to N11 without accept headers
-define(PATH_TO_N11_VIA_M7_NO_ACPTHEAD, ?PATH_TO_M7_NO_ACPTHEAD++[v3n11]).
-define(PATH_TO_N11_VIA_N5_NO_ACPTHEAD, ?PATH_TO_N5_NO_ACPTHEAD++[v3n11]).
%% P3 - The path to P3 without accept headers
-define(PATH_TO_P3_NO_ACPTHEAD, ?PATH_TO_I4_NO_ACPTHEAD++[v3p3]).
%% K5 - The path to K5 without accept headers
-define(PATH_TO_K5_NO_ACPTHEAD, ?PATH_TO_K7_NO_ACPTHEAD++[v3k5]).
%% L5 - The path to L5 without accept headers
-define(PATH_TO_L5_NO_ACPTHEAD, ?PATH_TO_K5_NO_ACPTHEAD++[v3l5]).
%% M5 - The path to M5 without accept headers
-define(PATH_TO_M5_NO_ACPTHEAD, ?PATH_TO_L5_NO_ACPTHEAD++[v3m5]).
%% N5 - The path to N5 without accept headers
-define(PATH_TO_N5_NO_ACPTHEAD, ?PATH_TO_M5_NO_ACPTHEAD++[v3n5]).
%% H10 - The path to H10 without accept headers
-define(PATH_TO_H10_VIA_G8_F6_E6_D5_C4,
?PATH_TO_G7_VIA_F6_E6_D5_C4++[v3g8,v3h10]).
%% H11 - The path to H11 without accept headers, via G11
-define(PATH_TO_H11_VIA_G11_F6_E6_D5_C4,
?PATH_TO_G11_NO_ACPTHEAD++[v3h10,v3h11]).
%% H12 - Two paths to H12 without accept headers
-define(PATH_TO_H12_VIA_G8_F6_E6_D5_C4,
?PATH_TO_H10_VIA_G8_F6_E6_D5_C4++[v3h11,v3h12]).
-define(PATH_TO_H12_VIA_G9_F6_E6_D5_C4,
?PATH_TO_G9_VIA_F6_E6_D5_C4++[v3h10,v3h11,v3h12]).
-define(PATH_TO_H12_NO_ACPTHEAD, ?PATH_TO_H12_VIA_G8_F6_E6_D5_C4).
-define(PATH_TO_H12_NO_ACPTHEAD_2, ?PATH_TO_H12_VIA_G9_F6_E6_D5_C4).
%% I12 - Two paths to I12 without accept headers
-define(PATH_TO_I12_VIA_H10_G8_F6_E6_D5_C4,
?PATH_TO_H10_VIA_G8_F6_E6_D5_C4++[v3i12]).
-define(PATH_TO_I12_VIA_H11_G11_F6_E6_D5_C4,
?PATH_TO_H11_VIA_G11_F6_E6_D5_C4++[v3i12]).
%% L13 - A path to L13 without accept headers
-define(PATH_TO_L13_NO_ACPTHEAD, ?PATH_TO_I12_VIA_H10_G8_F6_E6_D5_C4++[v3l13]).
%% M16 - A path to M16 without accept headers
-define(PATH_TO_M16_NO_ACPTHEAD, ?PATH_TO_L13_NO_ACPTHEAD++[v3m16]).
%% M20 - A path to M20 without accept headers
-define(PATH_TO_M20_NO_ACPTHEAD, ?PATH_TO_M16_NO_ACPTHEAD++[v3m20]).
%% N16 - A path to N16 without accept headers
-define(PATH_TO_N16_NO_ACPTHEAD, ?PATH_TO_M16_NO_ACPTHEAD++[v3n16]).
%% O16 - A path to O16 without accept headers
-define(PATH_TO_O16_NO_ACPTHEAD, ?PATH_TO_N16_NO_ACPTHEAD++[v3o16]).
%% O14 - A path to O14 without accept headers
-define(PATH_TO_O14_NO_ACPTHEAD, ?PATH_TO_O16_NO_ACPTHEAD++[v3o14]).
%% O18 - A path to O18 without accept headers
-define(PATH_TO_O18_NO_ACPTHEAD, ?PATH_TO_O16_NO_ACPTHEAD++[v3o18]).
%% O20 - A path to O20 without accept headers
-define(PATH_TO_O20_NO_ACPTHEAD, PATH_TO_P11).
%% L17 - A path to L17 without accept headers
-define(PATH_TO_L17_NO_ACPTHEAD,
?PATH_TO_L13_NO_ACPTHEAD++[v3l14,v3l15,v3l17]).
%% I13 - Two paths to I13 without accept headers
-define(PATH_TO_I13_VIA_H10_G8_F6_E6_D5_C4,
?PATH_TO_I12_VIA_H10_G8_F6_E6_D5_C4++[v3i13]).
-define(PATH_TO_I13_VIA_H11_G11_F6_E6_D5_C4,
?PATH_TO_I12_VIA_H11_G11_F6_E6_D5_C4++[v3i13]).
%% K13 - The path to K13 without accept headers, via I13, I12, H11, G11
-define(PATH_TO_K13_VIA_H11_G11_F6_E6_D5_C4,
?PATH_TO_I13_VIA_H11_G11_F6_E6_D5_C4++[v3k13]).
%% J18 - Three paths to J18 without accept headers (one via H10; one via H11
%% and K13; one via H12)
-define(PATH_TO_J18_VIA_I13_H10_G8_F6_E6_D5_C4,
?PATH_TO_I13_VIA_H10_G8_F6_E6_D5_C4++[v3j18]).
-define(PATH_TO_J18_VIA_K13_H11_G11_F6_E6_D5_C4,
?PATH_TO_K13_VIA_H11_G11_F6_E6_D5_C4++[v3j18]).
-define(PATH_TO_J18_NO_ACPTHEAD, ?PATH_TO_J18_VIA_I13_H10_G8_F6_E6_D5_C4).
-define(PATH_TO_J18_NO_ACPTHEAD_2, ?PATH_TO_J18_VIA_K13_H11_G11_F6_E6_D5_C4).
-define(PATH_TO_J18_NO_ACPTHEAD_3,
?PATH_TO_H12_NO_ACPTHEAD_2++[v3i12,v3i13,v3j18]).
%% P11 - Three paths to P11 without accept headers, via N11, P3, or O14
-define(PATH_TO_P11_VIA_N11_NO_ACPTHEAD,
?PATH_TO_N11_VIA_M7_NO_ACPTHEAD++[v3p11]).
-define(PATH_TO_P11_VIA_P3_NO_ACPTHEAD, ?PATH_TO_P3_NO_ACPTHEAD++[v3p11]).
-define(PATH_TO_P11_VIA_O14_NO_ACPTHEAD, ?PATH_TO_O14_NO_ACPTHEAD++[v3p11]).
%% O20 - The path to O20 via P11 via O14
-define(PATH_TO_O20_VIA_P11_VIA_O14_NO_ACPTHEAD,
?PATH_TO_P11_VIA_O14_NO_ACPTHEAD++[v3o20]).
%%
%% TEST SETUP AND CLEANUP
%%
core_tests() ->
[fun service_unavailable/0,
fun internal_server_error_o18/0,
fun not_implemented_b12/0,
fun not_implemented_b6/0,
fun uri_too_long_b11/0,
fun unsupported_media_type_b5/0,
fun request_entity_too_large_b4/0,
fun head_method_allowed/0,
fun head_method_not_allowed/0,
fun non_standard_method_501/0,
fun non_standard_method_200/0,
fun bad_request_b9/0,
fun simple_get/0,
fun not_acceptable_c4/0,
fun not_acceptable_d5_c4/0,
fun not_acceptable_d5_c3/0,
fun not_acceptable_e6_d5_c3/0,
fun not_acceptable_f7_e6_d5_c4/0,
fun precond_fail_no_resource/0,
fun precond_fail_g11/0,
fun precond_fail_h12/0,
fun precond_fail_j18/0,
fun precond_fail_j18_via_k13/0,
fun precond_fail_j18_via_h12/0,
fun content_md5_valid_b9a/0,
fun content_md5_valid_b9a_validated/0,
fun content_md5_invalid_b9a/0,
fun content_md5_custom_inval_b9a/0,
fun authorized_b8/0,
fun forbidden_b7/0,
fun options_b3/0,
fun variances_o18/0,
fun variances_o18_2/0,
fun ok_o18b/0,
fun multiple_choices_o18/0,
fun moved_permanently_i4/0,
fun moved_permanently_k5/0,
fun moved_temporarily_l5/0,
fun not_modified_j18/0,
fun not_modified_j18_via_k13/0,
fun not_modified_j18_via_h12/0,
fun not_modified_l17/0,
fun see_other_n11/0,
fun internal_server_error_n11/0,
fun see_other_n11_resource_calls/0,
fun see_other_n11_custom_base_uri/0,
fun see_other_n11_wrq_base_uri/0,
fun see_other_n5/0,
fun not_found_l7/0,
fun not_found_m7/0,
fun created_p11_post/0,
fun created_p11_put/0,
fun conflict_p3/0,
fun conflict_o14/0,
fun gone_m5/0,
fun gone_n5/0,
fun accepted_m20/0,
fun unsupported_media_type_accept_helper/0,
fun created_p11_streamed/0,
fun created_p11_accept_helper/0,
fun writer_callback/0,
fun head_length_access_for_cs/0,
fun get_known_length_for_cs/0,
fun get_for_range_capable_stream/0
% known_failure -- fun stream_content_md5/0
].
decision_core_test_() ->
{foreach, local, fun setup/0, fun cleanup/1,
[{spawn, Test} || Test <- core_tests()]}.
setup() ->
try
initialize_resource_settings(),
DispatchList = [{[?RESOURCE, '*'], ?MODULE, []}],
Ctx = wm_integration_test_util:start(?MODULE, "0.0.0.0", DispatchList),
set_context(Ctx),
MeckOpts = [passthrough, no_link, no_passthrough_cover],
meck:new(webmachine_resource, MeckOpts),
Ctx
catch
?STPATTERN(T:E) ->
io:format(user, "~n~p : ~p : ~p", [T, E, ?STACKTRACE]),
error(setup_failed)
end.
cleanup(Ctx) ->
meck:unload(webmachine_resource),
wm_integration_test_util:stop(Ctx),
clear_resource_settings().
get_decision_ids() ->
History = meck:history(webmachine_resource),
Result = [DecisionID || {_, {webmachine_resource, log_d, [DecisionID|_]}, _}
<- History, not is_substate(DecisionID)],
Result.
%% Is the decision ID a sub-state? Sub-states are not on the HTTP/1.1 activity
%% diagram and exist as an implementation detail for control flow. The decision
%% traces can be simplified by ignoring these sub-states.
is_substate(DecisionID) ->
?assert(is_decision_id(DecisionID)),
atom_matches(DecisionID, ?DECISION_ID_SUBSTATE_PATTERN).
is_decision_id(Atom) ->
atom_matches(Atom, ?DECISION_ID_PATTERN).
%% Does the given atom match the regular expression?
atom_matches(Atom, RE) when is_atom(Atom) ->
case re:run(atom_to_list(Atom), RE, [{capture, none}]) of
match ->
true;
nomatch ->
false
end.
%%
%% TEST CASES
%%
%% The decision trace is the path through the HTTP/1.1 activity diagram, which
%% has nodes labeled from A-P on the x-axis and 1-26 on the y-axis.
%%
%% The expected decision traces in these tests are hand verified.
%% 503 result via B13B (predicate: service_available)
service_unavailable() ->
put_setting(service_available, false),
{ok, Result} = httpc:request(head, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 503, "Service Unavailable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B13,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 500 error response via O18 from a callback raising an error
internal_server_error_o18() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(content_types_provided, [{"text/plain",
size_stream_raises_error}]),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 500, "Internal Server Error"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% It seems that a callback that returns a callback is what's necessary to get
%% the top-level `catch` in webmachine_decision_core:handle_request to be
%% covered, hence the use of the range form of stream (other callbacks can also
%% cover the catch clause, but not without cluttering the test output with
%% badmatch errors from other modules)
size_stream_raises_error(ReqData, Context) ->
Error = fun(_Start, _End) ->
error(foobar)
end,
{{stream, 1, Error}, ReqData, Context}.
%% 501 result via B12
not_implemented_b12() ->
put_setting(allowed_methods, ?HTTP_1_0_METHODS),
put_setting(known_methods, ?HTTP_1_0_METHODS),
{ok, Result} = httpc:request(delete, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 501, "Not Implemented"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B12,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 501 result via B6
not_implemented_b6() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(valid_content_headers, false),
{ok, Result} = httpc:request(get, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 501, "Not Implemented"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B6,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 414 result via B11
uri_too_long_b11() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(uri_too_long, true),
TooLong = url("notreallythatlongactually"),
{ok, Result} = httpc:request(get, {TooLong, []}, [], []),
?assertMatch({{"HTTP/1.1", 414, "Request-URI Too Large"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B11,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 415 result via B5
unsupported_media_type_b5() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(known_content_type, false),
{ok, Result} = httpc:request(get, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 415, "Unsupported Media Type"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B5,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 413 result via B4
request_entity_too_large_b4() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(valid_entity_length, false),
{ok, Result} = httpc:request(get, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 413, "Request Entity Too Large"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B4,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 from head method allowed
head_method_allowed() ->
put_setting(allowed_methods, ['GET', 'HEAD']),
{ok, Result} = httpc:request(head, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 405 from head method not allowed
head_method_not_allowed() ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
{ok, Result} = httpc:request(head, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 405, "Method Not Allowed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B10,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 501 from non-standard method
non_standard_method_501() ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
Ctx = get_context(),
Port = wm_integration_test_util:get_port(Ctx),
Url = wm_integration_test_util:url(Ctx, "foo"),
{ok, Sock} = gen_tcp:connect("localhost", Port, [binary, {active,false}]),
ok = gen_tcp:send(Sock, ["FOO ", Url, " HTTP/1.1\r\n",
"Host: http://localhost:", integer_to_list(Port),
"\r\n\r\n"]),
?assertMatch({ok, <<"HTTP/1.1 501 Not Implemented", _/binary>>},
gen_tcp:recv(Sock, 0, 2000)),
ok = gen_tcp:close(Sock),
ok.
%% 200 from non-standard method
non_standard_method_200() ->
Method = "FOO",
put_setting(known_methods, [Method|?HTTP_1_1_METHODS]),
put_setting(allowed_methods, ['GET', 'POST', 'PUT', Method]),
Ctx = get_context(),
Port = wm_integration_test_util:get_port(Ctx),
Url = wm_integration_test_util:url(Ctx, "foo"),
{ok, Sock} = gen_tcp:connect("localhost", Port, [binary, {active,false}]),
ok = gen_tcp:send(Sock, [Method, " ", Url, " HTTP/1.1\r\n",
"Host: http://localhost:", integer_to_list(Port),
"\r\n\r\n"]),
?assertMatch({ok, <<"HTTP/1.1 200 OK\r\n", _/binary>>},
gen_tcp:recv(Sock, 0, 2000)),
ok = gen_tcp:close(Sock),
ok.
%% 400 result via B9
bad_request_b9() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(malformed_request, true),
{ok, Result} = httpc:request(get, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 400, "Bad Request"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B9,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 from a get
simple_get() ->
put_setting(allowed_methods, ['GET']),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, ?HTML_CONTENT}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 406 via C4
not_acceptable_c4() ->
put_setting(allowed_methods, ['GET']),
Headers = [{"Accept", "video/mp4"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 406, "Not Acceptable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_C4,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 406 via D5 via C4
not_acceptable_d5_c4() ->
put_setting(allowed_methods, ['GET']),
put_setting(content_types_provided, [{"text/plain", to_html}]),
put_setting(language_available, false),
Headers = [{"Accept", "text/plain"},
{"Accept-Language", "x-pig-latin"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 406, "Not Acceptable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_D5_VIA_C4,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 406 result via D5 via C3
not_acceptable_d5_c3() ->
put_setting(allowed_methods, ['GET']),
put_setting(content_types_provided, [{"text/plain", to_html}]),
put_setting(language_available, false),
Headers = [{"Accept-Language", "x-pig-latin"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 406, "Not Acceptable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_D5_VIA_C3,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 406 result via E6 via D5 via C3
not_acceptable_e6_d5_c3() ->
put_setting(allowed_methods, ['GET']),
put_setting(content_types_provided, [{"text/plain", to_html}]),
put_setting(charsets_provided, [{"utf-8", fun identity/1}]),
Headers = [{"Accept-Language", "en-US"},
{"Accept-Charset", "ISO-8859-1"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 406, "Not Acceptable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_E6_VIA_D5_C3,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 406 result via F7 via E6 via D5 via C4
not_acceptable_f7_e6_d5_c4() ->
put_setting(allowed_methods, ['GET']),
put_setting(content_types_provided, [{"text/plain", to_html}]),
put_setting(language_available, true),
put_setting(charsets_provided, [{"utf-8", fun identity/1}]),
put_setting(encodings_provided, none),
Headers = [{"Accept", "text/plain"},
{"Accept-Language", "en-US"},
{"Accept-Charset", "utf-8"},
{"Accept-Encoding", "gzip"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 406, "Not Acceptable"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_F7_VIA_E6_D5_C4,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via H7, no accept headers and no resource
precond_fail_no_resource() ->
put_setting(allowed_methods, ['GET']),
put_setting(resource_exists, false),
Headers = [{"If-Match", "*"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_H7_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via G11, no accept headers
precond_fail_g11() ->
put_setting(allowed_methods, ['GET']),
put_setting(generate_etag, "v2"),
Headers = [{"If-Match", "\"v0\", \"v1\""}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_G11_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via H12, greater last modified
precond_fail_h12() ->
put_setting(allowed_methods, ['GET']),
TenAM = "Wed, 20 Feb 2013 10:00:00 GMT",
FivePM = "Wed, 20 Feb 2013 17:00:00 GMT",
ResErlDate = httpd_util:convert_request_date(FivePM),
put_setting(last_modified, ResErlDate),
Headers = [{"If-Unmodified-Since", TenAM}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_H12_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via J18 via I13 via I12 via H10
precond_fail_j18() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
Headers = [{"If-None-Match", "*"}],
PutRequest = {url(), Headers, "text/plain", "foo"},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via J18 via K13 via H11 via G11
precond_fail_j18_via_k13() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(generate_etag, "v1"),
Headers = [{"If-Match", "\"v1\""},
{"If-None-Match", "\"v1\""},
{"If-Unmodified-Since", "{{INVALID DATE}}"}],
PutRequest = {url(), Headers, "text/plain", "foo"},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD_2,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 412 result via J18 via I13 via I12 via H12
precond_fail_j18_via_h12() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
TenAM = "Wed, 20 Feb 2013 10:00:00 GMT",
FivePM = "Wed, 20 Feb 2013 17:00:00 GMT",
ResErlDate = httpd_util:convert_request_date(TenAM),
put_setting(last_modified, ResErlDate),
Headers = [{"If-Match", "*"},
{"If-None-Match", "*"},
{"If-Unmodified-Since", FivePM}],
PutRequest = {url(), Headers, "text/plain", "foo"},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 412, "Precondition Failed"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD_3,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 204 result, content-md5 header matches
content_md5_valid_b9a() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
ContentType = "text/plain",
put_setting(content_types_accepted, [{ContentType, to_html}]),
Body = "foo",
MD5Sum = base64:encode_to_string(md5(Body)),
Headers = [{"Content-MD5", MD5Sum}],
PutRequest = {url("new"), Headers, ContentType, Body},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 204, "No Content"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O20_VIA_P11_VIA_O14_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 204 result, content-md5 header matches, but checked by
%% validate_content_checksum instead of webmachine_decision_core itself.
content_md5_valid_b9a_validated() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(validate_content_checksum, true),
ContentType = "text/plain",
put_setting(content_types_accepted, [{ContentType, to_html}]),
Body = "foo",
MD5Sum = base64:encode_to_string(md5(Body)),
Headers = [{"Content-MD5", MD5Sum}],
PutRequest = {url("new"), Headers, ContentType, Body},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 204, "No Content"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O20_VIA_P11_VIA_O14_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 400 result, content-md5 header does not match
content_md5_invalid_b9a() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
ContentType = "text/plain",
Body = "foo",
InvalidMD5Sum = base64:encode_to_string("this is invalid for foo"),
Headers = [{"Content-MD5", InvalidMD5Sum}],
PutRequest = {url(), Headers, ContentType, Body},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 400, "Bad Request"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B9,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 400 result, resource's custom validate_content_checksum function rejects it
content_md5_custom_inval_b9a() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(validate_content_checksum, false),
ContentType = "text/plain",
Body = "foo",
InvalidMD5Sum = base64:encode_to_string("this is invalid for foo"),
Headers = [{"Content-MD5", InvalidMD5Sum}],
PutRequest = {url(), Headers, ContentType, Body},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 400, "Bad Request"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B9,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 401 result via B8
authorized_b8() ->
put_setting(is_authorized, "Basic"),
put_setting(allowed_methods, ['GET']),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
{{Protocol, Code, Status}, Headers, _} = Result,
?assertEqual("HTTP/1.1", Protocol),
?assertEqual(401, Code),
?assertEqual("Unauthorized", Status),
?assertEqual({"www-authenticate", "Basic"}, lists:keyfind("www-authenticate", 1, Headers)),
ExpectedDecisionTrace = ?PATH_TO_B8,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 403 result via B7
forbidden_b7() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(forbidden, true),
{ok, Result} = httpc:request(get, {url("forbiddenfoo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 403, "Forbidden"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B7,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 result, via OPTIONS
options_b3() ->
put_setting(allowed_methods, ['GET', 'HEAD', 'PUT', 'OPTIONS']),
{ok, Result} = httpc:request(options, {url(), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_B3,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 result with Vary
variances_o18() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
Charsets = [{"utf-8", fun identity/1},
{"iso-8859-5", fun identity/1},
{"unicode-1-1", fun identity/1}],
put_setting(charsets_provided, Charsets),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 result with other Vary
variances_o18_2() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
ContentTypes = [{"text/html", to_html},
{"text/plain", to_html}],
put_setting(content_types_provided, ContentTypes),
Charsets = [{"utf-8", fun identity/1}],
put_setting(charsets_provided, Charsets),
put_setting(encodings_provided, use_identity_or_gzip),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 200 result with body generation
ok_o18b() ->
put_setting(allowed_methods, ['GET']),
put_setting(generate_etag, "v1"),
put_setting(last_modified, ?FIRST_DAY_OF_LAST_YEAR),
put_setting(expires, ?FIRST_DAY_OF_NEXT_YEAR),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 200, "OK"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 300 result via O18
multiple_choices_o18() ->
put_setting(allowed_methods, ['GET']),
put_setting(multiple_choices, true),
Charsets = [{"utf-8", fun identity/1},
{"iso-8859-5", fun identity/1},
{"unicode-1-1", fun identity/1}],
put_setting(charsets_provided, Charsets),
{ok, Result} = httpc:request(get, {url("foo"), []}, [], []),
?assertMatch({{"HTTP/1.1", 300, "Multiple Choices"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_O18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 301 result via I4
moved_permanently_i4() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(resource_exists, false),
put_setting(moved_permanently, {true, url("new")}),
PutRequest = {url("old"), [], "text/plain", "foo"},
{ok, Result} = httpc:request(put, PutRequest, [], []),
?assertMatch({{"HTTP/1.1", 301, "Moved Permanently"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_I4_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 301 result via K5
moved_permanently_k5() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(resource_exists, false),
put_setting(previously_existed, true),
put_setting(moved_permanently, {true, url("new")}),
%% We just want to get the 301 from httpc, we don't want it to actually
%% try redirecting, so we turn off autoredirect
HTTPOptions = [{autoredirect, false}],
{ok, Result} = httpc:request(get, {url("old"), []}, HTTPOptions, []),
?assertMatch({{"HTTP/1.1", 301, "Moved Permanently"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_K5_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 307 result via L5
moved_temporarily_l5() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(resource_exists, false),
put_setting(previously_existed, true),
put_setting(moved_temporarily, {true, url("new")}),
%% We just want to get the 307 from httpc - similar to note about 301 above
HTTPOptions = [{autoredirect, false}],
{ok, Result}= httpc:request(get, {url("old"), []}, HTTPOptions, []),
?assertMatch({{"HTTP/1.1", 307, "Temporary Redirect"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_L5_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 304 result via J18 via K13 via H11 via G11
not_modified_j18() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
Headers = [{"If-None-Match", "*"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 304, "Not Modified"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 304 result via J18 via K13 via H11 via G11
not_modified_j18_via_k13() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(generate_etag, "v1"),
Headers = [{"If-Match", "\"v1\""},
{"If-None-Match", "\"v1\""},
{"If-Unmodified-Since", "{{INVALID DATE}}"}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 304, "Not Modified"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD_2,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 304 result via J18 via I13 via I12 via H12
not_modified_j18_via_h12() ->
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
TenAM = "Wed, 20 Feb 2013 10:00:00 GMT",
FivePM = "Wed, 20 Feb 2013 17:00:00 GMT",
ResErlDate = httpd_util:convert_request_date(TenAM),
put_setting(last_modified, ResErlDate),
Headers = [{"If-Match", "*"},
{"If-None-Match", "*"},
{"If-Unmodified-Since", FivePM}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 304, "Not Modified"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_J18_NO_ACPTHEAD_3,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 304 result via L17
not_modified_l17() ->
% Because httpd_util:rfc1123_date converts the date to GMT, it is offset by
% the local time zone. To get a Last-Modified date equal to the
% If-Modified-Since date we must convert it back from the RFC 1123 format.
% This avoids the test case failing in any time zone "east" of GMT :-P
RFC1123LastYear = httpd_util:rfc1123_date(?FIRST_DAY_OF_LAST_YEAR),
DateTimeLastYear = httpd_util:convert_request_date(RFC1123LastYear),
put_setting(allowed_methods, ?DEFAULT_ALLOWED_METHODS),
put_setting(last_modified, DateTimeLastYear),
put_setting(expires, ?FIRST_DAY_OF_NEXT_YEAR),
Headers = [{"If-Modified-Since", RFC1123LastYear}],
{ok, Result} = httpc:request(get, {url(), Headers}, [], []),
?assertMatch({{"HTTP/1.1", 304, "Not Modified"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_L17_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 303 result via N11 using request data rewriting
see_other_n11() ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
put_setting(resource_exists, false),
put_setting(allow_missing_post, true),
ContentType = "text/html",
put_setting(content_types_accepted, [{ContentType, to_html}]),
put_setting(process_post, {set_resp_redirect, ?RESOURCE_PATH ++ "/new1"}),
PostRequest = {url("post"), [], ContentType, "foo"},
{ok, Result} = httpc:request(post, PostRequest, [{autoredirect,false}], []),
?assertMatch({{"HTTP/1.1", 303, "See Other"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_N11_VIA_M7_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 500 result via N11 - Setting do_redirect without a Location
internal_server_error_n11() ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
put_setting(resource_exists, false),
put_setting(allow_missing_post, true),
ContentType = "text/html",
put_setting(content_types_accepted, [{ContentType, to_html}]),
put_setting(process_post, {set_resp_redirect_but_not_location}),
PostRequest = {url("post"), [], ContentType, "foo"},
{ok, Result} = httpc:request(post, PostRequest, [], []),
?assertMatch({{"HTTP/1.1", 500, "Internal Server Error"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_N11_VIA_M7_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 303 result via N11 using the result of resource calls
see_other_n11_resource_calls() ->
see_other_n11_resource_calls_base_uri(undefined).
%% 303 result via N11 using the result of resource calls and a custom base_uri
see_other_n11_custom_base_uri() ->
BaseURIFun = {decision_core_test, base_uri_add_slash},
see_other_n11_resource_calls_base_uri(BaseURIFun).
%% 303 result via N11 using the result of resource calls and a passthrough
%% base_uri
see_other_n11_wrq_base_uri() ->
BaseURIFun = {wrq, base_uri},
see_other_n11_resource_calls_base_uri(BaseURIFun).
%% helper function to remove common code
see_other_n11_resource_calls_base_uri(Value) ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
put_setting(resource_exists, false),
put_setting(allow_missing_post, true),
put_setting(post_is_create, true),
ContentType = "text/html",
put_setting(content_types_accepted, [{ContentType, to_html}]),
put_setting(create_path, {set_resp_redirect, ?RESOURCE_PATH ++ "/new1"}),
put_setting(base_uri, Value),
PostRequest = {url("post"), [], ContentType, "foo"},
{ok, Result} = httpc:request(post, PostRequest, [{autoredirect,false}], []),
?assertMatch({{"HTTP/1.1", 303, "See Other"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_N11_VIA_M7_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.
%% 303 result via N5
see_other_n5() ->
put_setting(allowed_methods, ['GET', 'POST', 'PUT']),
ContentType = "text/html",
put_setting(content_types_accepted, [{ContentType, to_html}]),
put_setting(resource_exists, false),
put_setting(previously_existed, true),
put_setting(allow_missing_post, true),
put_setting(process_post, {set_resp_redirect, ?RESOURCE_PATH ++ "/new1"}),
PostRequest = {url("post"), [], ContentType, "foo"},
{ok, Result} = httpc:request(post, PostRequest, [{autoredirect,false}], []),
?assertMatch({{"HTTP/1.1", 303, "See Other"}, _, _}, Result),
ExpectedDecisionTrace = ?PATH_TO_N11_VIA_N5_NO_ACPTHEAD,
?assertEqual(ExpectedDecisionTrace, get_decision_ids()),
ok.