summaryrefslogtreecommitdiff
path: root/tests/requests.scm
blob: 727a88617489c73157eb8f05e10195aba186835d (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
;; tests/requests --- neocities requests tester -*- coding: utf-8 -*-
;;
;; Copyright (C) 2023 Ekaitz Zarraga <ekaitz@elenq.tech>
;;
;; Author: Ekaitz Zarraga <ekaitz@elenq.tech>
;;
;; This file is part of guile-neocities.
;;
;; guile-neocities 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.
;;
;; guile-neocities 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 guile-neocities; if not, contact:
;;
;; Free Software Foundation           Voice:  +1-617-542-5942
;; 59 Temple Place - Suite 330        Fax:    +1-617-542-2652
;; Boston, MA  02111-1307,  USA       gnu@gnu.org

(define-module (tests requests)
  #:use-module (neocities requests)
  #:use-module (scheme base)
  #:use-module (web uri)
  #:use-module (web server)
  #:use-module (ice-9 threads)
  #:use-module (srfi srfi-64))


(test-begin "URL")

  (test-group "Creates url"
              (define url (neocities-url "info" #:hostname "neocities.org"))
              (test-assert (uri? url)))


  (test-group "Creates url with port and host"
              (define url (neocities-url "info" #:hostname "neocities.org" #:port 80))
              (test-assert (= (uri-port url) 80))
              (test-assert (string=? (uri-host url) "neocities.org")))


  (test-group "Encodes simple querystring"
              (define url (neocities-url
                            "info" #:hostname "neocities.org" #:port 80
                            #:querystring '(("file" . "COSA CON ESPACIOS"))))
              (test-assert (string=? (uri-query url) "file=COSA%20CON%20ESPACIOS")))


  (test-group "Encodes array in querystring"
              (define url (neocities-url
                            "info" #:hostname "neocities.org" #:port 80
                            #:querystring '(("file[]" . ("COSA CON ESPACIOS" "otra cosa")))))
              (test-assert (string=?
                             (uri-query url)
                             "file%5B%5D=COSA%20CON%20ESPACIOS&file%5B%5D=otra%20cosa")))
(test-end "URL")



(test-begin "Request")

(define test-port 8080)

(define* (serve-one-client-in-thread handler)
         (let* ((impl      (lookup-server-impl 'http))
                (server    (open-server impl `(#:port ,test-port))))
           (begin-thread (serve-one-client handler impl server '())
                         (close-server impl server))))

(test-group "Request arrived with empty body"
  (let* ((request-ret #f)
         (request-body-ret #f)
         (handler (lambda (request body)
                    (set! request-ret request)
                    (set! request-body-ret body)
                    (values '((content-type . (text/plain)))
                            "\"Hello World!\"")))
        (th (serve-one-client-in-thread handler)))
    (neocities-request
      'GET
      (neocities-url "info"
                     #:hostname "localhost"
                     #:port test-port
                     #:insecure #t))
    (join-thread th)
    (test-assert (and request-ret (not request-body-ret)))))


(test-end "Request")