~cosmicrose/liberator

liberator/test/liberator/resource_decision_tree_test.exs -rw-r--r-- 33.8 KiB
95bf1d41Rosa Richter Credo fixes 2 months ago
                                                                                
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
# SPDX-FileCopyrightText: 2021 Rosa Richter
#
# SPDX-License-Identifier: MIT

defmodule Liberator.ResourceDecisionTreeTest do
  use ExUnit.Case, async: true
  use Plug.Test

  defmodule ShortcutResource do
    use Liberator.Resource,
      decision_tree_overrides: %{
        service_available?: {:handle_ok, :handle_service_unavailable}
      }
  end

  test "can override the decision tree" do
    conn = conn(:get, "/")

    conn = ShortcutResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"

    assert List.last(conn.private.liberator_trace)[:step] == :stop
  end

  defmodule WillBreakLiberatorResource do
    use Liberator.Resource,
      decision_tree_overrides: %{
        service_available?: {:i_dont_exist, :handle_service_unavailable}
      }
  end

  test "exception test" do
    conn = conn(:get, "/")

    message = """
      Liberator encountered an unknown step called :i_dont_exist

      In module: Liberator.ResourceDecisionTreeTest.WillBreakLiberatorResource

      A couple things could be wrong:

      - If you have overridden part of the decision tree with :decision_tree_overrides,
        make sure that the atoms in the {true, false} tuple values have their own entries in the map.

      - If you have overridden part of the handler tree with :handler_status_overrides,
        or the action followups with :action_followup_overrides,
        make sure that the handler the atoms you passed in are spelled correctly,
        and match what the decision tree is calling.
    """

    assert_raise Liberator.UnknownStepException, message, fn ->
      WillBreakLiberatorResource.call(conn, [])
    end
  end

  defmodule UnavailableResource do
    use Liberator.Resource
    @impl true
    def service_available?(_conn), do: false
  end

  test "returns 503 when service_available? returns false" do
    conn = conn(:get, "/")
    conn = UnavailableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 503
    assert conn.resp_body == "Service Unavailable"
  end

  defmodule UnknownMethodResource do
    use Liberator.Resource
    @impl true
    def known_method?(_conn), do: false
  end

  test "returns 501 when known_method? returns false" do
    conn = conn(:get, "/")
    conn = UnknownMethodResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 501
    assert conn.resp_body == "Unknown Method"
  end

  defmodule UriTooLongResource do
    use Liberator.Resource
    @impl true
    def uri_too_long?(_conn), do: true
  end

  test "returns 414 when uri_too_long? returns true" do
    conn = conn(:get, "/")
    conn = UriTooLongResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 414
    assert conn.resp_body == "URI Too Long"
  end

  defmodule MethodNotAllowedResource do
    use Liberator.Resource
    @impl true
    def method_allowed?(_conn), do: false
  end

  test "returns 405 when method_allowed? returns false" do
    conn = conn(:get, "/")
    conn = MethodNotAllowedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 405
    assert conn.resp_body == "Method Not Allowed"
  end

  defmodule RaisingWellFormedResource do
    use Liberator.Resource
    @impl true
    def well_formed?(_conn), do: raise("shouldn't have called me!")
  end

  test "does not call well_formed? when body is nil" do
    conn = conn(:get, "/")
    conn = RaisingWellFormedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"
  end

  defmodule NotWellFormedResource do
    use Liberator.Resource
    @impl true
    def well_formed?(_conn), do: false
  end

  test "returns 400 when well_formed? returns false" do
    conn = conn(:get, "/", "test") |> put_req_header("content-type", "text/plain")
    conn = NotWellFormedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 400
    assert conn.resp_body == "Malformed"
  end

  defmodule MalformedResource do
    use Liberator.Resource
    @impl true
    def malformed?(_conn), do: true
  end

  test "returns 400 when malformed? returns true" do
    conn = conn(:get, "/", "test") |> put_req_header("content-type", "text/plain")
    conn = MalformedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 400
    assert conn.resp_body == "Malformed"
  end

  defmodule UnauthorizedResource do
    use Liberator.Resource
    @impl true
    def authorized?(_conn), do: false
  end

  test "returns 401 when authorized? returns false" do
    conn = conn(:get, "/")
    conn = UnauthorizedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 401
    assert conn.resp_body == "Unauthorized"
  end

  defmodule ForbiddenResource do
    use Liberator.Resource
    @impl true
    def allowed?(_conn), do: false
  end

  test "returns 403 when allowed? returns false" do
    conn = conn(:get, "/")
    conn = ForbiddenResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 403
    assert conn.resp_body == "Forbidden"
  end

  defmodule NotImplementedResource do
    use Liberator.Resource
    @impl true
    def valid_content_header?(_conn), do: false
  end

  test "returns 501 when valid_content_header? returns false" do
    conn = conn(:get, "/")
    conn = NotImplementedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 501
    assert conn.resp_body == "Not Implemented"
  end

  defmodule UnsupportedMediaResource do
    use Liberator.Resource, trace: :log
    @impl true
    def known_content_type?(_conn), do: false
  end

  test "returns 415 when known_content_type? returns false" do
    conn = conn(:get, "/", "body") |> put_req_header("content-type", "something weird idk")
    conn = UnsupportedMediaResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 415
    assert conn.resp_body == "Unsupported Media Type"
  end

  defmodule EntityTooLongResource do
    use Liberator.Resource
    @impl true
    def valid_entity_length?(_conn), do: false
  end

  test "returns 413 when valid_entity_length? returns false" do
    conn = conn(:get, "/", "test") |> put_req_header("content-type", "text/plain")
    conn = EntityTooLongResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 413
    assert conn.resp_body == "Request Entity Too Large"
  end

  defmodule OptionsResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["OPTIONS"]
    def is_options?(_conn), do: true
  end

  test "returns 200-options for an options request" do
    conn = conn(:options, "/")
    conn = OptionsResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "Options"
  end

  defmodule PayGatedResource do
    use Liberator.Resource
    @impl true
    def payment_required?(_conn), do: true
  end

  test "returns 402 when payment_required? returns true" do
    conn = conn(:get, "/")
    conn = PayGatedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 402
    assert conn.resp_body == "Payment Required"
  end

  defmodule NotAcceptableResource do
    use Liberator.Resource
    @impl true
    def accept_exists?(_conn), do: true
    def media_type_available?(_conn), do: false
  end

  test "returns 406 when accept_exists? returns true but media_type_available? returns false" do
    conn = conn(:get, "/")
    conn = NotAcceptableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 406
    assert conn.resp_body == "Not Acceptable"
  end

  defmodule LanguageUnavailableResource do
    use Liberator.Resource
    @impl true
    def accept_language_exists?(_conn), do: true
    def language_available?(_conn), do: false
  end

  test "returns 406 when accept_language_exists? returns true but language_available? returns false" do
    conn = conn(:get, "/")
    conn = LanguageUnavailableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 406
    assert conn.resp_body == "Not Acceptable"
  end

  defmodule CharsetUnavailableResource do
    use Liberator.Resource
    @impl true
    def accept_charset_exists?(_conn), do: true
    def charset_available?(_conn), do: false
  end

  test "returns 406 when accept_charset_exists? returns true but charset_available? returns false" do
    conn = conn(:get, "/")
    conn = CharsetUnavailableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 406
    assert conn.resp_body == "Not Acceptable"
  end

  defmodule EncodingUnavailableResource do
    use Liberator.Resource
    @impl true
    def accept_encoding_exists?(_conn), do: true
    def encoding_available?(_conn), do: false
  end

  test "returns 406 when accept_encoding_exists? returns true but encoding_available? returns false" do
    conn = conn(:get, "/")
    conn = EncodingUnavailableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 406
    assert conn.resp_body == "Not Acceptable"
  end

  defmodule UnprocessableResource do
    use Liberator.Resource
    @impl true
    def processable?(_conn), do: false
  end

  test "returns 422 when processable? returns false" do
    conn = conn(:get, "/")
    conn = UnprocessableResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 422
    assert conn.resp_body == "Unprocessable Entity"
  end

  defmodule RateLimitedResource do
    use Liberator.Resource
    @impl true
    def too_many_requests?(_conn), do: true
  end

  test "returns 429 when too_many_requests? returns true" do
    conn = conn(:get, "/")
    conn = RateLimitedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 429
    assert conn.resp_body == "Too Many Requests"
  end

  defmodule UnavailableForLegalReasonsResource do
    use Liberator.Resource
    @impl true
    def unavailable_for_legal_reasons?(_conn), do: true
  end

  test "returns 451 when unavailable_for_legal_reasons? returns true" do
    conn = conn(:get, "/")
    conn = UnavailableForLegalReasonsResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 451
    assert conn.resp_body == "Unavailable for Legal Reasons"
  end

  defmodule NotFoundResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: false
  end

  test "returns 404 if entity does not exist" do
    conn = conn(:get, "/")
    conn = NotFoundResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 404
    assert conn.resp_body == "Not Found"
  end

  defmodule NotFoundNoPostResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: false
  end

  test "returns 404 if entity does not exist and can't post to missing" do
    conn = conn(:get, "/")
    conn = NotFoundNoPostResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 404
    assert conn.resp_body == "Not Found"
  end

  defmodule PostedNotFoundRedirectResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: true
  end

  test "returns 303 if entity does not exist and we can post to missing, and have want a post redirect" do
    conn = conn(:get, "/")
    conn = PostedNotFoundRedirectResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 303
    assert conn.resp_body == "See Other"
  end

  defmodule PostedNotFoundNewResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: true
  end

  test "returns 201 if entity does not exist and we can post to missing, and create a new resource" do
    conn = conn(:get, "/")
    conn = PostedNotFoundNewResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 201
    assert conn.resp_body == "Created"
  end

  defmodule PostedNotFoundAcceptedResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: false
  end

  test "returns 202 if entity does not exist and we can post to missing, and post is not immediately enacted" do
    conn = conn(:get, "/")
    conn = PostedNotFoundAcceptedResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 202
    assert conn.resp_body == "Accepted"
  end

  defmodule PostedNotFoundNoContentResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: false
  end

  test "returns 204 if entity does not exist and we can post to missing, the entity isn't new and we won't respond with entities" do
    conn = conn(:get, "/")
    conn = PostedNotFoundNoContentResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 204
    assert conn.resp_body == "No Content"
  end

  defmodule PostedNotFoundMultipleRepresentationsResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: true
    def multiple_representations?(_conn), do: true
  end

  test "returns 300 if entity does not exist and we can post to missing, the entity isn't new and we have multiple entity representations" do
    conn = conn(:get, "/")
    conn = PostedNotFoundMultipleRepresentationsResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 300
    assert conn.resp_body == "Multiple Representations"
  end

  defmodule PostedNotFoundSingleRepresentationResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: false
    def post_to_missing?(_conn), do: true
    def can_post_to_missing?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: true
    def multiple_representations?(_conn), do: false
  end

  test "returns 200 if entity does not exist and we can post to missing, the entity isn't new" do
    conn = conn(:get, "/")
    conn = PostedNotFoundSingleRepresentationResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"
  end

  defmodule MovedPermanentlyResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: true
    def moved_permanently?(_conn), do: true
  end

  test "returns 301 for permanently moved resource" do
    conn = conn(:get, "/")
    conn = MovedPermanentlyResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 301
    assert conn.resp_body == "Moved Permanently"
  end

  defmodule MovedTemporarilyResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: true
    def moved_permanently?(_conn), do: false
    def moved_temporarily?(_conn), do: true
  end

  test "returns 307" do
    conn = conn(:get, "/")
    conn = MovedTemporarilyResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 307
    assert conn.resp_body == "Moved Temporarily"
  end

  defmodule GoneResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: true
    def moved_permanently?(_conn), do: false
    def moved_temporarily?(_conn), do: false
    def post_to_gone?(_conn), do: false
  end

  test "returns 410 if the resource is gone" do
    conn = conn(:get, "/")
    conn = GoneResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 410
    assert conn.resp_body == "Gone"
  end

  defmodule CantPostToGoneResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: true
    def moved_permanently?(_conn), do: false
    def moved_temporarily?(_conn), do: false
    def post_to_gone?(_conn), do: true
    def can_post_to_gone?(_conn), do: false
  end

  test "returns 410 when can't post to gone" do
    conn = conn(:get, "/")
    conn = CantPostToGoneResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 410
    assert conn.resp_body == "Gone"
  end

  defmodule NewPostToGoneResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: false
    def existed?(_conn), do: true
    def moved_permanently?(_conn), do: false
    def moved_temporarily?(_conn), do: false
    def post_to_gone?(_conn), do: true
    def can_post_to_gone?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: true
  end

  test "returns 201 when resource is gone but we can post to it" do
    conn = conn(:post, "/")
    conn = NewPostToGoneResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 201
    assert conn.resp_body == "Created"
  end

  defmodule PutToDifferentUrlResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PUT"]
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: true
    def put_to_different_url?(_conn), do: true
  end

  test "returns 301 when put to a different url but entity doesn't exist" do
    conn = conn(:put, "/")
    conn = PutToDifferentUrlResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 301
    assert conn.resp_body == "Moved Permanently"
  end

  defmodule CantPutToMissingResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PUT"]
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: true
    def put_to_different_url?(_conn), do: false
    def can_put_to_missing?(_conn), do: false
  end

  test "returns 501 when put to a different url but entity doesn't exist and can't put to missing" do
    conn = conn(:put, "/")
    conn = CantPutToMissingResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 501
    assert conn.resp_body == "Not Implemented"
  end

  defmodule CanPutToMissingConflictResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PUT"]
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: false
    def method_put?(_conn), do: true
    def put_to_different_url?(_conn), do: false
    def can_put_to_missing?(_conn), do: true
    def conflict?(_conn), do: true
  end

  test "returns 409 when put to a different url but entity doesn't exist, and we can put to missing, but there's a conflict" do
    conn = conn(:put, "/")
    conn = CanPutToMissingConflictResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 409
    assert conn.resp_body == "Conflict"
  end

  defmodule MissingMatchStarResource do
    use Liberator.Resource
    @impl true
    def exists?(_conn), do: false
    def if_match_star_exists_for_missing?(_conn), do: true
  end

  test "returns 412 when entity doesn't exist but if_match_star_exists_for_missing is true" do
    conn = conn(:get, "/")
    conn = MissingMatchStarResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 412
    assert conn.resp_body == "Precondition Failed"
  end

  defmodule MismatchedIfMatchEtagResource do
    use Liberator.Resource
    @impl true
    def if_match_exists?(_conn), do: true
    def if_match_star?(_conn), do: false
    def etag_matches_for_if_match?(_conn), do: false
  end

  test "returns 412 if If-Match <etag> doesn't match an etag" do
    conn = conn(:get, "/")
    conn = MismatchedIfMatchEtagResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 412
    assert conn.resp_body == "Precondition Failed"
  end

  defmodule UnmodifiedSinceResource do
    use Liberator.Resource
    @impl true
    def if_unmodified_since_exists?(_conn), do: true
    def if_unmodified_since_valid_date?(_conn), do: true
    def unmodified_since?(_conn), do: true
  end

  test "returns 412 if If-Unmodified-Since <date> and entity has not been modified since" do
    conn = conn(:get, "/")
    conn = UnmodifiedSinceResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 412
    assert conn.resp_body == "Precondition Failed"
  end

  defmodule IfNoneMatchButDoesMatchResource do
    use Liberator.Resource
    @impl true
    def if_none_match_exists?(_conn), do: true
    def if_none_match_star?(_conn), do: false
    def etag_matches_for_if_none?(_conn), do: true
    def if_none_match?(_conn), do: false
  end

  test "returns 412 if If-None-Match <etag> etag does match" do
    conn = conn(:get, "/")
    conn = IfNoneMatchButDoesMatchResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 412
    assert conn.resp_body == "Precondition Failed"
  end

  defmodule IfNoneMatchStarButMatchesResource do
    use Liberator.Resource
    @impl true
    def if_none_match_exists?(_conn), do: true
    def if_none_match_star?(_conn), do: true
    def if_none_match?(_conn), do: false
  end

  test "returns 412 if If-None-Match * etag does match" do
    conn = conn(:get, "/")
    conn = IfNoneMatchStarButMatchesResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 412
    assert conn.resp_body == "Precondition Failed"
  end

  defmodule NotModifiedIfNoneMatchResource do
    use Liberator.Resource
    @impl true
    def if_none_match_exists?(_conn), do: true
    def if_none_match_star?(_conn), do: false
    def etag_matches_for_if_none?(_conn), do: true
    def if_none_match?(_conn), do: true
  end

  test "returns 304 if If-None-Match <etag> etag does't match" do
    conn = conn(:get, "/")
    conn = NotModifiedIfNoneMatchResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 304
    assert conn.resp_body == "Not Modified"
  end

  defmodule ModifiedSinceResource do
    use Liberator.Resource
    @impl true
    def if_modified_since_exists?(_conn), do: true
    def if_modified_since_valid_date?(_conn), do: true
    def modified_since?(_conn), do: false
  end

  test "returns 304 if If-Modified-Since <date> and resource has not been modified" do
    conn = conn(:get, "/")
    conn = ModifiedSinceResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 304
    assert conn.resp_body == "Not Modified"
  end

  defmodule SuccessfulDeleteResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["DELETE"]
    def method_delete?(_conn), do: true
    def delete!(_conn), do: nil
  end

  test "returns 200 if method is delete" do
    conn = conn(:delete, "/")
    conn = SuccessfulDeleteResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"
  end

  defmodule DelayedDeleteResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["DELETE"]
    def method_delete?(_conn), do: true
    def delete!(_conn), do: nil
    def delete_enacted?(_conn), do: false
  end

  test "returns 202 if method is delete but delete is not immediately enacted" do
    conn = conn(:delete, "/")
    conn = DelayedDeleteResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 202
    assert conn.resp_body == "Accepted"
  end

  defmodule SuccessfulDeleteNoEntityResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["DELETE"]
    def method_delete?(_conn), do: true
    def delete!(_conn), do: nil
    def respond_with_entity?(_conn), do: false
  end

  test "returns 204 if method is delete and no content is returned" do
    conn = conn(:delete, "/")
    conn = SuccessfulDeleteNoEntityResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 204
    assert conn.resp_body == "No Content"
  end

  defmodule SuccessfulPatchResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PATCH"]
    def method_delete?(_conn), do: false
    def method_patch?(_conn), do: true
    def patch!(_conn), do: nil
    def patch_enacted?(_conn), do: true
    def respond_with_entity?(_conn), do: true
    def multiple_representations?(_conn), do: false
  end

  test "returns 200 if method is patch" do
    conn = conn(:patch, "/")
    conn = SuccessfulPatchResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"
  end

  defmodule AcceptedPatchResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PATCH"]
    def method_delete?(_conn), do: false
    def method_patch?(_conn), do: true
    def patch!(_conn), do: nil
    def patch_enacted?(_conn), do: false
  end

  test "returns 202 if method is patch and patch is not immediately enacted" do
    conn = conn(:patch, "/")
    conn = AcceptedPatchResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 202
    assert conn.resp_body == "Accepted"
  end

  defmodule AcceptedPatchNoContentResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PATCH"]
    def method_delete?(_conn), do: false
    def method_patch?(_conn), do: true
    def patch!(_conn), do: nil
    def patch_enacted?(_conn), do: true
    def respond_with_entity?(_conn), do: false
  end

  test "returns 204 if method is patch and no content is returned" do
    conn = conn(:patch, "/")
    conn = AcceptedPatchNoContentResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 204
    assert conn.resp_body == "No Content"
  end

  defmodule ConflictedPostToExistingResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def method_delete?(_conn), do: false
    def method_patch?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: true
  end

  test "returns 409 if post-to-existing has a conflict" do
    conn = conn(:post, "/")
    conn = ConflictedPostToExistingResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 409
    assert conn.resp_body == "Conflict"
  end

  defmodule ConflictedPutToExistingResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["PUT"]
    def method_delete?(_conn), do: false
    def method_patch?(_conn), do: false
    def post_to_existing?(_conn), do: false
    def put_to_existing?(_conn), do: true
    def conflict?(_conn), do: true
  end

  test "returns 409 if put-to-existing has a conflict" do
    conn = conn(:put, "/")
    conn = ConflictedPutToExistingResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 409
    assert conn.resp_body == "Conflict"
  end

  defmodule PostRedirectResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: true
  end

  test "returns 303 if post with post-redirect enabled" do
    conn = conn(:post, "/")
    conn = PostRedirectResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 303
    assert conn.resp_body == "See Other"
  end

  defmodule PostCreatedNewResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: true
  end

  test "returns 201 if post when resource is created" do
    conn = conn(:post, "/")
    conn = PostCreatedNewResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 201
    assert conn.resp_body == "Created"
  end

  defmodule PostNewNoEntityResponseResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: false
  end

  test "returns 204 if post when resource is not new and we want no entity response" do
    conn = conn(:post, "/")
    conn = PostNewNoEntityResponseResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 204
    assert conn.resp_body == "No Content"
  end

  defmodule PostNewSingleEntityResponseResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: true
    def multiple_representations?(_conn), do: false
  end

  test "returns 200 if post when resource is not new and we want an entity response with one representation" do
    conn = conn(:post, "/")
    conn = PostNewSingleEntityResponseResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 200
    assert conn.resp_body == "OK"
  end

  defmodule PostNewMultipleEntityResponseResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: false
    def respond_with_entity?(_conn), do: true
    def multiple_representations?(_conn), do: true
  end

  test "returns 300 if post when resource is not new and we want an entity response with multiple representations" do
    conn = conn(:post, "/")
    conn = PostNewMultipleEntityResponseResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 300
    assert conn.resp_body == "Multiple Representations"
  end

  defmodule PostNewResource do
    use Liberator.Resource
    @impl true
    def allowed_methods(_conn), do: ["POST"]
    def exists?(_conn), do: true
    def if_match_exists?(_conn), do: false
    def if_unmodified_since_exists?(_conn), do: false
    def if_none_match_exists?(_conn), do: false
    def post_to_existing?(_conn), do: true
    def conflict?(_conn), do: false
    def method_post?(_conn), do: true
    def post_enacted?(_conn), do: true
    def post_redirect?(_conn), do: false
    def new?(_conn), do: true
  end

  test "returns 201 if put when resource is new" do
    conn = conn(:post, "/")
    conn = PostNewResource.call(conn, [])

    assert conn.state == :sent
    assert conn.status == 201
    assert conn.resp_body == "Created"
  end
end