;; 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 http://mozilla.org/MPL/2.0/. ;; bjomarks/tests/wayback.bjo — (bjomarks wayback): the snapshot in an ;; answer of the availability API, and the snapshot fetched from a server on ;; the loopback, which answers as the API does. ;; ;; bjo run tests/wayback.bjo (import (std prelude)) (import (std test)) (import (std datetime)) (import (std net)) (import (std ports)) (import (bjomarks wayback)) ;; ---- The snapshot in the answer ------------------------------------------------------ (def found (str "{\"url\": \"example.com\", \"archived_snapshots\": {\"closest\": {\"status\": \"200\", " "\"available\": true, \"url\": \"http://web.archive.org/web/20261118035140/https://example.com/\", " "\"timestamp\": \"20261118035140\"}}}")) (def none-found "{\"url\": \"nothing.example\", \"archived_snapshots\": {}}") (deftest "the snapshot in an answer" #:cases ((answer wanted) (found (Ok (Some "https://web.archive.org/web/20261118035140/https://example.com/"))) ;; An address that is https already is kept. ("{\"archived_snapshots\": {\"closest\": {\"available\": true, \"url\": \"https://web.archive.org/web/1/x\"}}}" (Ok (Some "https://web.archive.org/web/1/x"))) (none-found (Ok None)) ("{\"archived_snapshots\": {\"closest\": {\"available\": false, \"url\": \"http://web.archive.org/x\"}}}" (Ok None)) ("{\"archived_snapshots\": {\"closest\": {\"available\": true}}}" (Ok None)) ("{}" (Ok None)) ("[]" (Ok None))) (check (= (snapshot-in answer) wanted))) (deftest "an answer that is not JSON is an Err" (check-match (snapshot-in "Service Unavailable") (Err _))) ;; ---- A server on the loopback ---------------------------------------------------------- ;; The status, the body and the wait in ms of the answer to a request line. ;; The page asked for is in the query: `url=` and its address, in which `:` ;; and `/` are not encoded. (: (-> string (Tuple string string long))) (defun (answer-for line) (cond ((string-contains? line "url=https://example.com/here ") (Tuple "200 OK" found 0L)) ((string-contains? line "url=https://example.com/none ") (Tuple "200 OK" none-found 0L)) ((string-contains? line "url=https://example.com/broken ") (Tuple "503 Service Unavailable" "" 0L)) ((string-contains? line "url=https://example.com/html ") (Tuple "200 OK" "" 0L)) ((string-contains? line "url=https://example.com/slow ") (Tuple "200 OK" found 5000L)) (else (Tuple "404 Not Found" "" 0L)))) ;; Reads the request's head, to its empty line, and returns its first line. (: (-bjo-> InputPort string)) (defbjo (request-line in) (let go ((seen "")) (if (string-contains? seen "\r\n\r\n") (match (string-split-once seen "\r\n") ((Some (Tuple l _)) l) (None seen)) (match (read-some in) ((Some bs) (go (str seen (bytes->string/lossy bs)))) (None seen))))) ;; One connection: one request, one answer. The wait ends early when the ;; server stops. (: (-bjo-> Listener InputPort OutputPort void)) (defbjo (answer l in out) (def (Tuple status body wait) (answer-for (request-line in))) (when (> wait 0L) (ignore (sync (choose (wrap (timeout (milliseconds wait)) (fun (_) 0)) (wrap (listener-closed l) (fun (_) 0)))))) (def bytes (string->bytes body)) (ignore (try (let () (write-bytes! out (string->bytes (str "HTTP/1.1 " status "\r\n" "Content-Type: application/json\r\n" "Content-Length: " (int->string (array-length bytes)) "\r\n" "Connection: close\r\n\r\n"))) (write-bytes! out bytes) (flush-port out) (close-input-port in) (close-output-port out)) #:catch (System.Exception)))) ;; Accepts until the listener closes. (: (-bjo-> Listener void)) (defbjo (serve l) (match (sync (choose (wrap (accept-evt l) Some) (wrap (listener-closed l) (fun (_) None)))) ((Some (Ok (Tuple in out))) (ignore (bjo (answer l in out))) (serve l)) (_ unit))) ;; Starts a server, asks it for the snapshot of `url` with a deadline of ;; `within` seconds, and stops the server. (: (-bjo-> string int (Result string string))) (defbjo (fetch-served url within) (match (tcp-listen "127.0.0.1" 0) ((Err e) (Err (str "listen failed: " (.-Message e)))) ((Ok l) (with-open ((listening l)) (def server (bjo (serve l))) (def api (str "http://127.0.0.1:" (int->string (listener-port l)) "/wayback/available")) (def got (fetch-archive url #:within within #:api api)) (close-listener! l) (ignore (sync (promise-join server))) got)))) (: (-> (Result Exception (Result string string)) (Result string string))) (defun (joined r) (match r ((Ok got) got) ((Err e) (Err (str "raised: " (.-Message e)))))) ;; `fetch-archive` of `url` on a new server, with 3 seconds. (: (-> string (Result string string))) (defun (fetched url) (joined (sync/blocking (promise-join (bjo (fetch-served url 3)))))) ;; ---- Fetching ----------------------------------------------------------------------- (deftest "the latest snapshot, fetched" (check (= (fetched "https://example.com/here") (Ok "https://web.archive.org/web/20261118035140/https://example.com/"))) ;; The spaces at the ends are not sent. (check (= (fetched " https://example.com/here ") (Ok "https://web.archive.org/web/20261118035140/https://example.com/")))) (deftest "why there is no snapshot" (check (= (fetched "https://example.com/none") (Err "the Wayback Machine has no copy of this page"))) (check (= (fetched "https://example.com/broken") (Err "the Wayback Machine answered 503"))) (check-match (fetched "https://example.com/html") (Err _))) (deftest "a slow API meets the deadline" (def started (now)) (check (= (joined (sync/blocking (promise-join (bjo (fetch-served "https://example.com/slow" 1))))) (Err "no answer in 1 second"))) (check (< (instant->unix-milliseconds (now)) (+ (instant->unix-milliseconds started) 4000L)))) (deftest "only http and https are looked up" (check (= (joined (sync/blocking (promise-join (bjo (fetch-archive "ftp://example.org/"))))) (Err "only http and https addresses are looked up: ftp://example.org/"))) (check (= (joined (sync/blocking (promise-join (bjo (fetch-archive "example.org"))))) (Err "only http and https addresses are looked up: example.org")))) (run-tests)