aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/repository/remote.lux
blob: 1999598fe66fa0563db185c5ee31886bd7f08193 (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
128
129
130
131
132
133
134
135
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["[0]" io (.only IO)]
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" try (.use "[1]#[0]" monad)]
    ["[0]" exception]
    ["[0]" function]]
   [data
    ["[0]" binary (.use "[1]#[0]" equivalence)]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]
     [encoding
      ["[0]" utf8]]]
    [collection
     ["[0]" dictionary]]]
   [math
    ["[0]" random (.only Random)]]
   [world
    [net (.only URL)
     ["[0]" http
      ["[1]" client]
      ["[1]/[0]" status]
      ["@[1]" /]]]]
   [test
    ["_" property (.only Test)]]]]
 [\\program
  ["[0]" / (.only)
   ["/[1]" //
    ["[1][0]" identity]]]])

(def (url_body url)
  (-> URL (@http.Body IO))
  (let [url (of utf8.codec encoded url)]
    (function (_ _)
      (io.io {try.#Success [(binary.size url) url]}))))

(def (good_http user password)
  (-> //identity.User //identity.Password (http.Client IO))
  (implementation
   (def (request method url headers input)
     (with_expansions [<failure> [http/status.bad_request
                                  [@http.#headers (http.headers (list))
                                   @http.#body (..url_body "")]]]
       (<| io.io
           {try.#Success}
           (if (|> headers
                   (dictionary.value "User-Agent")
                   (maybe#each (same? /.user_agent))
                   (maybe.else false))
             (when [method input]
               [{@http.#Get} {.#None}]
               [http/status.ok
                [@http.#headers (http.headers (list))
                 @http.#body (..url_body url)]]
               
               [{@http.#Put} {.#Some input}]
               (if (|> headers
                       (dictionary.value "Authorization")
                       (maybe#each (text#= (//identity.basic_auth user password)))
                       (maybe.else false))
                 [http/status.created
                  [@http.#headers (http.headers (list))
                   @http.#body (..url_body url)]]
                 <failure>)
               
               _
               <failure>)
             <failure>))))))

(def bad_http
  (http.Client IO)
  (implementation
   (def (request method url headers input)
     (<| io.io
         {try.#Success}
         [http/status.bad_request
          [@http.#headers (http.headers (list))
           @http.#body (..url_body "")]]))))

(def .public test
  Test
  (<| (_.covering /._)
      (do [! random.monad]
        [address (random.upper_cased 10)
         uri (random.lower_cased 10)

         user (random.lower_cased 10)
         password (random.lower_cased 10)

         content (of ! each (of utf8.codec encoded)
                     (random.lower_cased 10))]
        (all _.and
             (_.coverage [/.repository /.user_agent /.Address]
               (let [repo (/.repository (..good_http user password)
                                        {.#Some [//identity.#user user
                                                 //identity.#password password]}
                                        address)]
                 (and (|> (of repo download uri)
                          io.run!
                          (try#each (of utf8.codec decoded))
                          try#conjoint
                          (try#each (text#= (format address uri)))
                          (try.else false))
                      (|> (of repo upload uri content)
                          io.run!
                          (try#each (function.constant true))
                          (try.else false)))))
             (_.coverage [/.upload_failure]
               (let [repo (/.repository (..good_http user password)
                                        {.#None}
                                        address)]
                 (when (io.run! (of repo upload uri content))
                   {try.#Failure error}
                   (exception.match? /.upload_failure error)
                   
                   {try.#Success _}
                   false)))
             (_.coverage [/.download_failure]
               (let [repo (/.repository ..bad_http
                                        {.#None}
                                        address)]
                 (when (io.run! (of repo download uri))
                   {try.#Failure error}
                   (exception.match? /.download_failure error)
                   
                   {try.#Success _}
                   false)))
             ))))