swh.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-swh)
  19. #:use-module (guix swh)
  20. #:use-module (guix tests http)
  21. #:use-module (web response)
  22. #:use-module (srfi srfi-64))
  23. ;; Test the JSON mapping machinery used in (guix swh).
  24. (define %origin
  25. "{ \"visits_url\": \"/visits/42\",
  26. \"type\": \"git\",
  27. \"url\": \"http://example.org/guix.git\" }")
  28. (define %directory-entries
  29. "[ { \"name\": \"one\",
  30. \"type\": \"regular\",
  31. \"length\": 123,
  32. \"dir_id\": 1 }
  33. { \"name\": \"two\",
  34. \"type\": \"regular\",
  35. \"length\": 456,
  36. \"dir_id\": 2 } ]")
  37. (define-syntax-rule (with-json-result str exp ...)
  38. (with-http-server `((200 ,str))
  39. (parameterize ((%swh-base-url (%local-url)))
  40. exp ...)))
  41. (test-begin "swh")
  42. (test-equal "lookup-origin"
  43. (list "git" "http://example.org/guix.git")
  44. (with-json-result %origin
  45. (let ((origin (lookup-origin "http://example.org/guix.git")))
  46. (list (origin-type origin)
  47. (origin-url origin)))))
  48. (test-equal "lookup-origin, not found"
  49. #f
  50. (with-http-server `((404 "Nope."))
  51. (parameterize ((%swh-base-url (%local-url)))
  52. (lookup-origin "http://example.org/whatever"))))
  53. (test-equal "lookup-directory"
  54. '(("one" 123) ("two" 456))
  55. (with-json-result %directory-entries
  56. (map (lambda (entry)
  57. (list (directory-entry-name entry)
  58. (directory-entry-length entry)))
  59. (lookup-directory "123"))))
  60. (test-equal "rate limit reached"
  61. 3000000000
  62. (let ((too-many (build-response
  63. #:code 429
  64. #:reason-phrase "Too many requests"
  65. ;; Pretend we've reached the limit and it'll be reset in
  66. ;; June 2065.
  67. #:headers '((x-ratelimit-remaining . "0")
  68. (x-ratelimit-reset . "3000000000")))))
  69. (with-http-server `((,too-many "Too bad."))
  70. (parameterize ((%swh-base-url (%local-url)))
  71. (catch 'swh-error
  72. (lambda ()
  73. (lookup-origin "http://example.org/guix.git"))
  74. (lambda (key url method response)
  75. ;; Ensure the reset time was recorded.
  76. (@@ (guix swh) %general-rate-limit-reset-time)))))))
  77. (test-assert "%allow-request? and request-rate-limit-reached?"
  78. ;; Here we test two things: that the rate limit set above is in effect and
  79. ;; that %ALLOW-REQUEST? is called, and that 'request-rate-limit-reached?'
  80. ;; returns true.
  81. (let* ((key (gensym "skip-request"))
  82. (skip-if-limit-reached
  83. (lambda (url method)
  84. (or (not (request-rate-limit-reached? url method))
  85. (throw key #t)))))
  86. (parameterize ((%allow-request? skip-if-limit-reached))
  87. (catch key
  88. (lambda ()
  89. (lookup-origin "http://example.org/guix.git")
  90. #f)
  91. (const #t)))))
  92. (test-end "swh")
  93. ;; Local Variables:
  94. ;; eval: (put 'with-json-result 'scheme-indent-function 1)
  95. ;; eval: (put 'with-http-server 'scheme-indent-function 1)
  96. ;; End: