~cosmicrose/liberator

liberator/test/liberator/resource_defaults_test.exs -rw-r--r-- 18.4 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
# SPDX-FileCopyrightText: 2021 Rosa Richter
#
# SPDX-License-Identifier: MIT

defmodule Liberator.ResourceDefaultsTest do
  use ExUnit.Case, async: true
  use Plug.Test
  alias Liberator.HTTPDateTime

  defmodule MyDefaultResource do
    use Liberator.Resource
  end

  describe "method_allowed?/1" do
    test "allows GET" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows HEAD" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows PUT" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows POST" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows DELETE" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows OPTIONS" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows TRACE" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "allows PATCH" do
      assert MyDefaultResource.method_allowed?(conn(:get, "/"))
    end

    test "disallows asdf" do
      refute MyDefaultResource.method_allowed?(conn(:asdf, "/"))
    end
  end

  describe "is_options?" do
    test "returns true for options type" do
      assert MyDefaultResource.is_options?(conn(:options, "/"))
    end

    test "returns false for non-options type" do
      refute MyDefaultResource.is_options?(conn(:get, "/"))
    end
  end

  describe "method_put?" do
    test "returns true for put type" do
      assert MyDefaultResource.method_put?(conn(:put, "/"))
    end

    test "returns false for non-put type" do
      refute MyDefaultResource.method_put?(conn(:get, "/"))
    end
  end

  describe "method_post?" do
    test "returns true for post type" do
      assert MyDefaultResource.method_post?(conn(:post, "/"))
    end

    test "returns false for non-post type" do
      refute MyDefaultResource.method_post?(conn(:get, "/"))
    end
  end

  describe "method_delete?" do
    test "returns true for delete type" do
      assert MyDefaultResource.method_delete?(conn(:delete, "/"))
    end

    test "returns false for non-delete type" do
      refute MyDefaultResource.method_delete?(conn(:get, "/"))
    end
  end

  describe "method_patch?" do
    test "returns true for patch type" do
      assert MyDefaultResource.method_patch?(conn(:patch, "/"))
    end

    test "returns false for non-patch type" do
      refute MyDefaultResource.method_patch?(conn(:get, "/"))
    end
  end

  describe "body_exists?/1" do
    test "returns true if the body is present" do
      conn = conn(:get, "/", "test body") |> put_req_header("content-type", "text/plain")
      assert MyDefaultResource.body_exists?(conn)
    end

    test "returns false if the body is not present" do
      refute MyDefaultResource.body_exists?(conn(:get, "/"))
    end
  end

  describe "accept_exists?/1" do
    test "returns true if the accept header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "application/json")

      assert MyDefaultResource.accept_exists?(conn)
    end

    test "returns true if the accept header is */*" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "*/*")

      assert MyDefaultResource.accept_exists?(conn)
    end

    test "returns false if the accept header is not present" do
      refute MyDefaultResource.accept_exists?(conn(:get, "/"))
    end
  end

  describe "accept_language_exists?/1" do
    test "returns true if the accept-language header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "en")

      assert MyDefaultResource.accept_language_exists?(conn)
    end

    test "returns false if the accept-language header is not present" do
      refute MyDefaultResource.accept_language_exists?(conn(:get, "/"))
    end
  end

  defmodule LanguageAgnosticResource do
    use Liberator.Resource
    def available_languages(_conn), do: ["*"]
  end

  defmodule EnglishOnlyResource do
    use Liberator.Resource
    def available_languages(_conn), do: ["en"]
  end

  defmodule GermanOnlyResource do
    use Liberator.Resource
    def available_languages(_conn), do: ["de"]
  end

  describe "language_available?/1" do
    test "returns the user's requested language if acceptable languges is *" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "de")

      assert LanguageAgnosticResource.language_available?(conn) == %{language: "de"}
    end

    test "returns * if the user accepts * and only * is available" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "*")

      assert LanguageAgnosticResource.language_available?(conn) == %{language: "*"}
    end

    test "returns false if acceptable languges is different from requested language" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "de")

      refute EnglishOnlyResource.language_available?(conn)
    end

    test "returns a map containing the matching language" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "en")

      assert EnglishOnlyResource.language_available?(conn) == %{language: "en"}
    end

    test "sets the Gettext locale to the given locale when it is available" do
      # Using the GermanOnlyResource here just in case my default language is set to "en".
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "de")

      GermanOnlyResource.language_available?(conn)

      assert Gettext.get_locale() == "de"
    end

    test "does not set the Gettext locale if language is *" do
      # The best I can do is assert that Gettext.get_locale() is "en",
      # because it always returns the default locale if one isn't set
      conn =
        conn(:get, "/")
        |> put_req_header("accept-language", "*")

      LanguageAgnosticResource.language_available?(conn)

      assert Gettext.get_locale() == "en"
    end
  end

  describe "accept_charset_exists?/1" do
    test "returns true if the accept-charset header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-charset", "en")

      assert MyDefaultResource.accept_charset_exists?(conn)
    end

    test "returns false if the accept-charset header is not present" do
      refute MyDefaultResource.accept_charset_exists?(conn(:get, "/"))
    end
  end

  describe "accept_encoding_exists?/1" do
    test "returns true if the accept-encoding header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-encoding", "en")

      assert MyDefaultResource.accept_encoding_exists?(conn)
    end

    test "returns false if the accept-encoding header is not present" do
      refute MyDefaultResource.accept_encoding_exists?(conn(:get, "/"))
    end
  end

  defmodule Utf8EncodingAvailableResource do
    use Liberator.Resource

    def available_encodings(_), do: ["UTF-8"]
  end

  describe "encoding_available?/1" do
    test "disallows UTF-16" do
      refute Utf8EncodingAvailableResource.encoding_available?(
               conn(:get, "/")
               |> put_req_header("accept-encoding", "UTF-16")
             )
    end

    test "returns a map containing the matching encoding" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-encoding", "UTF-8")

      assert Utf8EncodingAvailableResource.encoding_available?(conn) == %{encoding: "UTF-8"}
    end
  end

  defmodule JsonMediaTypeAvailableResource do
    use Liberator.Resource

    def available_media_types(_), do: ["application/json", "text/html"]
  end

  describe "media_type_available?/1" do
    test "disallows text/nonexistent-media-type" do
      refute MyDefaultResource.media_type_available?(
               conn(:get, "/")
               |> put_req_header("accept", "text/nonexistent-media-type")
             )
    end

    test "returns a map containing the matching media type" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "application/json")

      assert JsonMediaTypeAvailableResource.media_type_available?(conn) == %{
               media_type: "application/json"
             }
    end

    test "returns a map containing the matching media type with the highest q" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "application/json;q=1.0, text/html;q=0.8")

      assert JsonMediaTypeAvailableResource.media_type_available?(conn) == %{
               media_type: "application/json"
             }
    end

    test "returns a map containing the matching media type with the highest q even if they're out of order" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "text/html;q=0.8, application/json;q=1.0")

      assert JsonMediaTypeAvailableResource.media_type_available?(conn) == %{
               media_type: "application/json"
             }
    end

    test "values with plus modifiers aren't the same" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "text/html;q=0.8, application/json+myspecialschema;q=1.0")

      assert JsonMediaTypeAvailableResource.media_type_available?(conn) == %{
               media_type: "text/html"
             }
    end

    test "values with no q are the highest ranked" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept", "text/html;q=0.8, application/json")

      assert JsonMediaTypeAvailableResource.media_type_available?(conn) == %{
               media_type: "application/json"
             }
    end
  end

  defmodule CharsetUtf8AvailableResource do
    use Liberator.Resource

    def available_charsets(_), do: ["UTF-8"]
  end

  defmodule CharsetUtf16UnavailableResource do
    use Liberator.Resource

    def available_charsets(_), do: ["UTF-8"]
  end

  defmodule SetsCharsetResource do
    use Liberator.Resource

    def available_charsets(_), do: ["UTF-8"]
  end

  describe "charset_available?/1" do
    test "returns true if charset is present in `available_charsets/1`" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-charset", "UTF-8")

      assert CharsetUtf8AvailableResource.charset_available?(conn)
    end

    test "returns trfalseue if charset is not present in `available_charsets/1`" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-charset", "UTF-16")

      refute CharsetUtf16UnavailableResource.charset_available?(conn)
    end

    test "returns a map containing the matching charset" do
      conn =
        conn(:get, "/")
        |> put_req_header("accept-charset", "UTF-8")

      assert SetsCharsetResource.charset_available?(conn) == %{charset: "UTF-8"}
    end
  end

  describe "if_modified_since_exists?" do
    test "returns true if the if-modified-since header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-modified-since", "*")

      assert MyDefaultResource.if_modified_since_exists?(conn)
    end

    test "returns false if the if-modified-since header is not present" do
      refute MyDefaultResource.if_modified_since_exists?(conn(:get, "/"))
    end
  end

  describe "modified_since?" do
    test "returns true if last modification date is after modification_since" do
      time_str =
        Timex.Timezone.get("GMT")
        |> Timex.now()
        |> Timex.shift(days: -1)
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-modified-since", time_str)

      assert MyDefaultResource.modified_since?(conn)
    end

    test "returns false if last modification date is before modification_since" do
      time_str =
        Timex.Timezone.get("GMT")
        |> Timex.now()
        |> Timex.shift(days: 1)
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-modified-since", time_str)

      refute MyDefaultResource.modified_since?(conn)
    end
  end

  describe "if_modified_since_valid_date?" do
    test "returns true if if_modified_since header contains a valid date" do
      time_str =
        Timex.now()
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-modified-since", time_str)

      assert MyDefaultResource.if_modified_since_valid_date?(conn)
    end

    test "returns false if if_modified_since header contains an invalid date" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-modified-since", "asdf")

      refute MyDefaultResource.if_modified_since_valid_date?(conn)
    end
  end

  describe "if_unmodified_since_exists?" do
    test "returns true if the if-unmodified-since header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-unmodified-since", "*")

      assert MyDefaultResource.if_unmodified_since_exists?(conn)
    end

    test "returns false if the if-unmodified-since header is not present" do
      refute MyDefaultResource.if_unmodified_since_exists?(conn(:get, "/"))
    end
  end

  describe "if_unmodified_since_valid_date?" do
    test "returns true if if_unmodified_since header contains a valid date" do
      time_str =
        Timex.now()
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-unmodified-since", time_str)

      assert MyDefaultResource.if_unmodified_since_valid_date?(conn)
    end

    test "returns false if if_unmodified_since header contains an invalid date" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-unmodified-since", "asdf")

      refute MyDefaultResource.if_unmodified_since_valid_date?(conn)
    end
  end

  describe "unmodified_since?" do
    test "returns false if last modification date is after modification_since" do
      time_str =
        Timex.Timezone.get("GMT")
        |> Timex.now()
        |> Timex.shift(days: -1)
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-unmodified-since", time_str)

      refute MyDefaultResource.unmodified_since?(conn)
    end

    test "returns true if last modification date is before modification_since" do
      time_str =
        Timex.Timezone.get("GMT")
        |> Timex.now()
        |> Timex.shift(days: 1)
        |> HTTPDateTime.format!()

      conn =
        conn(:get, "/")
        |> put_req_header("if-unmodified-since", time_str)

      assert MyDefaultResource.unmodified_since?(conn)
    end
  end

  describe "if_match_exists?" do
    test "returns true if the if-match header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "*")

      assert MyDefaultResource.if_match_exists?(conn)
    end

    test "returns false if the if-match header is not present" do
      refute MyDefaultResource.if_match_exists?(conn(:get, "/"))
    end
  end

  describe "if_match_star?" do
    test "returns true if the if-match * header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "*")

      assert MyDefaultResource.if_match_star?(conn)
    end

    test "returns false if the if-match * header is not present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "abcdefg")

      refute MyDefaultResource.if_match_star?(conn)
    end
  end

  describe "if_match_star_exists_for_missing?" do
    test "returns true if the if-match * header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "*")

      assert MyDefaultResource.if_match_star_exists_for_missing?(conn)
    end

    test "returns false if the if-match * header is not present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "abcdefg")

      refute MyDefaultResource.if_match_star_exists_for_missing?(conn)
    end
  end

  describe "if_none_match_exists?" do
    test "returns true if the if-none-match header is present" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-none-match", "asdf")

      assert MyDefaultResource.if_none_match_exists?(conn)
    end

    test "returns false if the if-none-match header is not present" do
      refute MyDefaultResource.if_none_match_exists?(conn(:get, "/"))
    end
  end

  describe "if_none_match_star?" do
    test "returns true if the if-none-match header is *" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-none-match", "*")

      assert MyDefaultResource.if_none_match_star?(conn)
    end

    test "returns false if the if-none-match header is not *" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-none-match", "asdf")

      refute MyDefaultResource.if_none_match_star?(conn)
    end
  end

  defmodule IfMatchModule do
    use Liberator.Resource

    def etag(_), do: "1"
  end

  describe "etag_matches_for_if_match?" do
    test "returns false by default" do
      refute MyDefaultResource.etag_matches_for_if_match?(conn(:get, "/"))
    end

    test "returns the etag if etag matches" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-match", "1")

      assert IfMatchModule.etag_matches_for_if_match?(conn) == %{etag: "1"}
    end
  end

  defmodule IfNoneMatchModule do
    use Liberator.Resource

    def etag(_), do: "1"
  end

  describe "etag_matches_for_if_none?" do
    test "returns false by default" do
      refute MyDefaultResource.etag_matches_for_if_none?(conn(:get, "/"))
    end

    test "returns the etag if etag matches" do
      conn =
        conn(:get, "/")
        |> put_req_header("if-none-match", "1")

      assert IfNoneMatchModule.etag_matches_for_if_none?(conn) == %{etag: "1"}
    end
  end

  describe "post_to_missing?" do
    test "returns true for POST" do
      assert MyDefaultResource.post_to_missing?(conn(:post, "/"))
    end

    test "returns true for GET" do
      refute MyDefaultResource.post_to_missing?(conn(:get, "/"))
    end
  end

  describe "post_to_existing?" do
    test "returns true for POST" do
      assert MyDefaultResource.post_to_existing?(conn(:post, "/"))
    end

    test "returns true for GET" do
      refute MyDefaultResource.post_to_existing?(conn(:get, "/"))
    end
  end

  describe "post_to_gone?" do
    test "returns true for POST" do
      assert MyDefaultResource.post_to_gone?(conn(:post, "/"))
    end

    test "returns true for GET" do
      refute MyDefaultResource.post_to_gone?(conn(:get, "/"))
    end
  end

  describe "put_to_existing?" do
    test "returns true for PUT" do
      assert MyDefaultResource.put_to_existing?(conn(:put, "/"))
    end

    test "returns true for GET" do
      refute MyDefaultResource.put_to_existing?(conn(:get, "/"))
    end
  end
end