;;; x100 --- test ‘url:parse’

;; Copyright (C) 2012 Thien-Thi Nguyen
;;
;; This file is part of Guile-WWW.
;;
;; Guile-WWW 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, or
;; (at your option) any later version.
;;
;; Guile-WWW 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-WWW.  If not, see <https://www.gnu.org/licenses/>.

(use-modules (www url))

(define (try string user host port path)
  (define (badness s . args)
    (apply fse
           (string-append "misparse: " s " (input: ~S)~%")
           (append args (list string)))
    (exit #f))
  (define (sub which want got)
    (or (equal? want got)
        (badness "~A mismatch, want ~S, got ~S" which want got)))
  (let ((got (url:parse string)))
    (vfso "got: ~S~%" got)
    (or (vector? got) (badness "not a vector: ~S" got))
    (sub 'scheme 'http (url:scheme got))
    (sub 'user user (url:user got))
    (sub 'host host (url:host got))
    (sub 'port port (url:port got))
    (sub 'path path (url:path got))))

(try "http://123.com"
     #f
     "123.com"
     #f
     #f)

(try "http://127.0.0.1:2345/so/be/it"
     #f
     "127.0.0.1"
     2345
     "so/be/it")

(try "http://user@host:42/path"
     "user"
     "host"
     42
     "path")

(try "http://fool@[::ffff:192.0.2.1]:42/foo"
     "fool"
     "[::ffff:192.0.2.1]"
     42
     "foo")

(try "http://fool@:42/foo"
     "fool"
     #f
     42
     "foo")

;;; x100 ends here
