aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/net/http/client.lux
blob: cf500597752e42802bf85fee87c54fad891c2c77 (plain)
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
(.using
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [abstract
    [monad (.only Monad do)]]
   [control
    ["[0]" pipe]
    ["[0]" io (.only IO)]
    ["[0]" try (.only Try)]
    ["[0]" function]
    [concurrency
     ["[0]" async (.open: "[1]#[0]" functor)]]]
   [data
    ["[0]" binary]
    ["[0]" product]
    ["[0]" text (.only)
     ["%" format (.only format)]
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" dictionary]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["[0]" nat]]]]]
 [\\library
  ["[0]" / (.only)
   ["/[1]" // (.only)
    ["[1][0]" status]]]])

(def: (verification ! expected response)
  (All (_ !)
    (-> (Monad !) Nat (! (Try (//.Response !)))
        (! Bit)))
  (do !
    [response response]
    (case response
      {try.#Success response}
      (|> response
          product.right
          (the //.#body)
          (function.on {.#None})
          (# ! each (|>> (pipe.do try.monad
                           []
                           [product.right (# utf8.codec decoded)]
                           [(# nat.decimal decoded)]
                           [(nat.= expected) in])
                         (try.else false))))
      
      {try.#Failure error}
      (in false))))

(def: .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Client])
      (do [! random.monad]
        [on_post random.nat
         on_get random.nat
         on_put random.nat
         on_patch random.nat
         on_delete random.nat
         on_head random.nat
         on_connect random.nat
         on_options random.nat
         on_trace random.nat
         num_headers (# ! each (nat.% 10) random.nat)
         headers (random.dictionary text.hash num_headers (random.lower_case 3) (random.lower_case 3))
         .let [mock (is (/.Client IO)
                        (implementation
                         (def: (request method url headers data)
                           (io.io (let [value (case method
                                                {//.#Post} on_post
                                                {//.#Get} on_get
                                                {//.#Put} on_put
                                                {//.#Patch} on_patch
                                                {//.#Delete} on_delete
                                                {//.#Head} on_head
                                                {//.#Connect} on_connect
                                                {//.#Options} on_options
                                                {//.#Trace} on_trace)
                                        data (|> value
                                                 (# nat.decimal encoded)
                                                 (# utf8.codec encoded))]
                                    {try.#Success [//status.ok
                                                   [//.#headers headers
                                                    //.#body (function (_ ?wanted_bytes)
                                                               (io.io {try.#Success [(binary.size data)
                                                                                     data]}))]]})))))]]
        (with_expansions [<cases> (these [/.post on_post]
                                         [/.get on_get]
                                         [/.put on_put]
                                         [/.patch on_patch]
                                         [/.delete on_delete]
                                         [/.head on_head]
                                         [/.connect on_connect]
                                         [/.options on_options]
                                         [/.trace on_trace])]
          (`` (all _.and
                   (~~ (template [<definition> <expected>]
                         [(_.coverage [<definition>]
                            (|> (<definition> "" //.empty {.#None} mock)
                                (verification io.monad <expected>)
                                io.run!))]

                         <cases>
                         ))
                   (_.coverage [/.headers]
                     (nat.= (dictionary.size headers)
                            (|> headers
                                dictionary.entries
                                /.headers
                                dictionary.size)))
                   (in (do [! async.monad]
                         [.let [mock (/.async mock)]
                          (~~ (template [<definition> <expected>]
                                [<expected> (|> (<definition> "" //.empty {.#None} mock)
                                                (verification ! <expected>))]

                                <cases>))]
                         (_.coverage' [/.async]
                           (and (~~ (template [<definition> <expected>]
                                      [<expected>]

                                      <cases>))))))
                   ))))))