guile-pastebin/tests/pastebin-http

pastebin-http

1
#!/usr/bin/env sh
2
exec guile -s "$0" "$@"
3
!#
4
5
;;; SPDX-FileCopyrightText: 2025 Evgeny Pisemsky <mail@pisemsky.site>
6
;;;
7
;;; SPDX-License-Identifier: GPL-3.0-only
8
9
(use-modules (rnrs bytevectors)
10
             (srfi srfi-64)
11
             (web uri)
12
             (web request)
13
             (web response)
14
             (pastebin http))
15
16
(test-begin "pastebin-http")
17
18
(let* ((dir (tmpnam))
19
       (handler (make-pastebin-handler dir)))
20
  (mkdir dir)
21
22
  (let ((request (build-request
23
                  (string->uri "http://localhost:8080/post")
24
                  #:method 'POST
25
                  #:headers '((content-type . (multipart/form-data (boundary . "------------------------s1eMqEtEr3zEuOSkwyAiLi"))))
26
                  #:port (%make-void-port "w+"))))
27
    (define-values (resp body)
28
      (handler request (string->utf8 "--------------------------s1eMqEtEr3zEuOSkwyAiLi\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\nPaste 0\r\n--------------------------s1eMqEtEr3zEuOSkwyAiLi\r\nContent-Disposition: form-data; name=\"showUrl\"\r\n\r\n1\r\n--------------------------s1eMqEtEr3zEuOSkwyAiLi--")))
29
    (test-equal 200 (response-code resp))
30
    (test-equal "http://localhost:8080/raw/00000\r\n" (call-with-output-string body)))
31
32
  (let ((request (build-request (string->uri "http://localhost:8080/raw/00000"))))
33
    (define-values (resp body)
34
      (handler request ""))
35
    (test-equal 200 (response-code resp))
36
    (test-equal "Paste 0" (call-with-output-string body)))
37
38
  (let ((request (build-request
39
                  (string->uri "http://localhost:8080/post")
40
                  #:method 'POST
41
                  #:headers '((content-type . (multipart/form-data (boundary . "------------------------WemU5jJZTowf8qPbKbZ6qE"))))
42
                  #:port (%make-void-port "w+"))))
43
    (define-values (resp body)
44
      (handler request (string->utf8 "--------------------------WemU5jJZTowf8qPbKbZ6qE\r\nContent-Disposition: form-data; name=\"text\"\r\n\r\nPaste 1\r\n--------------------------WemU5jJZTowf8qPbKbZ6qE--")))
45
    (test-equal 303 (response-code resp))
46
    (test-equal "/" (uri-path (assq-ref (response-headers resp) 'location))))
47
48
  (let ((request (build-request (string->uri "http://localhost:8080/raw/00001"))))
49
    (define-values (resp body)
50
      (handler request ""))
51
    (test-equal 200 (response-code resp))
52
    (test-equal "Paste 1" (call-with-output-string body)))
53
54
  (let* ((uri (string->uri "http://localhost:8080/"))
55
         (request (build-request uri)))
56
    (define-values (resp body)
57
      (handler request ""))
58
    (test-equal 200 (response-code resp))))
59
60
(test-end "pastebin-http")
61
62
;; Local Variables:
63
;; mode: scheme
64
;; End:
65