# nginx-test.scm -rw-r--r-- 6.0 KiB View raw
                                                                                
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
;;; rde --- Reproducible development environment.
;;;
;;; Copyright © 2023 Andrew Tropin <andrew@trop.in>
;;;
;;; This file is part of rde.
;;;
;;; rde is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; rde is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with rde.  If not, see <http://www.gnu.org/licenses/>.

(define-module (rde serializers nginx-test)
  #:use-module (guix gexp)
  #:use-module (rde serializers nginx)
  #:use-module (rde tests)
  #:use-module (rde tests store)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-64))

(define (serialize-config config)
  (eval-with-store (serialize-nginx-config #f config)))

(define (serialize-term term)
  ((@@ (rde serializers nginx) serialize-nginx-term) term))

(define (serialize-nginx-vector v)
  ((@@ (rde serializers nginx) serialize-nginx-vector) v))

(define-test nginx-terms
  (test-group "nginx terms"
    (test-equal "number"
      "123"
      (serialize-term 123))
    (test-equal "string"
      "\"string here\""
      (serialize-term "string here"))
    (test-equal "symbol"
      "symbol-here"
      (serialize-term 'symbol-here))
    (test-assert "gexp"
      (gexp?
       (serialize-term #~"gexp")))
    (test-assert "file-like"
      (file-like?
       (serialize-term (plain-file "name" "content"))))

    (test-error "true" #t (serialize-term #t))
    (test-error "false" #t (serialize-term #f))
    (test-error "list" #t (serialize-term '(a b c)))
    (test-error "vector" #t (serialize-term #(a b c)))))

(define-test nginx-vectors
  (test-group "nginx vectors (conditions)"
    (test-assert "vector with a few terms"
      (match (serialize-nginx-vector
              `#(symbol "string" 123 ,#~"gexp"))
        (("(" "symbol" " " "\"string\"" " " "123" " " (? gexp? _) ")") #t)
        (_ #f)))

    (test-error "nested list" #t (serialize-nginx-vector #(())))
    (test-error "nested vector" #t (serialize-nginx-vector #(#())))))


(define-test nginx-contexts
  (test-group "nginx contexts"
    (test-assert "simple nested config"
      (match (serialize-nginx-context
              `((a b ((c d)))))
        (("" "a" " " "b" " {\n"
          "  " "c" " " "d" ";" "\n"
          "" "}\n") #t)
        (_ #f)))

    (test-assert "double nested config"
      (match (serialize-nginx-context
              `((a ((b ((c d)))))))
        (("" "a" " {\n"
          "  " "b" " {\n"
          "    " "c" " " "d" ";" "\n"
          "  " "}\n"
          "" "}\n") #t)
        (_ #f)))

    (test-assert "double nested config with vector"
      (match (serialize-nginx-context
              `((a ((b #() ((c)))))))
        (("" "a" " {\n"
          "  " "b" " " "(" ")" " {\n"
          "    " "c" ";" "\n"
          "  " "}\n"
          "" "}\n") #t)
        (_ #f)))))

;; (display
;;  (serialize-config
;;   `((a ((if #() ((c))))))))

(define-test basic-config
  (test-group "basic config"
    ;; (test-expect-fail 1)
    (test-equal "key-value pairs"
      "\
a b;
c d;
"
      (serialize-config '((a b)
                          (c d))))
    ;; (test-expect-fail 1)
    (test-equal "nested context"
      "\
a b {
  c d;
}
"
      (serialize-config '((a b ((c d))))))

    (test-equal "simple if statement"
      "\
if (a ~ b) {
  c d;
}
"
      (serialize-config '((if #(a ~ b) ((c d))))))))

(define-test gexps
  (test-group "gexps"
    (test-equal "simple gexps"
      "\
a hehe;
"
      (serialize-config
       `((a ,#~"hehe"))))

    (test-expect-fail 1)
    (test-equal "identation of gexps"
      "\
a {
    # nothing
# unindented
}
"
      (serialize-config
       `((a ((,#~"# nothing")
             ,#~"# unindented")))))))

;; (display       (serialize-config
;;        `((location ~*
;;                    #{^/if-and-alias/(?<file>.*)}#
;;                    ;; #~"^/if-and-alias/(?<file>.*)"
;;                    ((alias /tmp/$file)
;;                     (set $true 1)
;;                     (if #($true) ((,#~"# nothing"))))))))
(define-test example-config
  (test-group "example config"
    ;; (test-expect-fail 1)
    (test-equal "location with nested if and empty body"
      "\
location ~* ^/if-and-alias/(?<file>.*) {
  alias /tmp/$file;
  set $true 1;
  if ($true) {
    # nothing;
  }
}
"
      (serialize-config
       `((location ~*
                   #{^/if-and-alias/(?<file>.*)}#
                   ;; #~"^/if-and-alias/(?<file>.*)"
                   ((alias /tmp/$file)
                    (set $true 1)
                    (if #($true) ((,#~"# nothing"))))))))

    (test-equal "location with nested if 2"
      "\
location / {
  error_page 418 = @other;
  recursive_error_pages on;
  if ($something) {
    return 418;
  }
}
"
      (serialize-config
       '((location / ((error_page 418 = @other)
                      (recursive_error_pages on)
                      (if #($something)
                          ((return 418))))))))))


;; if ($http_user_agent ~ MSIE) {
;;     rewrite ^(.*)$ /msie/$1 break;
;; }

;; if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
;;     set $id $1;
;; }

;; if ($request_method = POST) {
;;     return 405;
;; }

;; if ($slow) {
;;     limit_rate 10k;
;; }

;; if ($invalid_referer) {
;;     return 403;
;; }

;; location ~* ^/if-and-alias/(?<file>.*) {
;;     alias /tmp/$file;
;;     set $true 1;
;;     if ($true) {
;;         # nothing
;;     }
;; }

;; if ($args ~ post=140){
;;   rewrite ^ http://example.com/ permanent;
;; }

;; https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts
;; https://stackoverflow.com/questions/2936260/what-language-are-nginx-conf-files
;; https://www.nginx.com/blog/using-free-ssltls-certificates-from-lets-encrypt-with-nginx/