swh.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 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. "{ \"id\": 42,
  26. \"visits_url\": \"/visits/42\",
  27. \"type\": \"git\",
  28. \"url\": \"http://example.org/guix.git\" }")
  29. (define %directory-entries
  30. "[ { \"name\": \"one\",
  31. \"type\": \"regular\",
  32. \"length\": 123,
  33. \"dir_id\": 1 }
  34. { \"name\": \"two\",
  35. \"type\": \"regular\",
  36. \"length\": 456,
  37. \"dir_id\": 2 } ]")
  38. (define-syntax-rule (with-json-result str exp ...)
  39. (with-http-server `((200 ,str))
  40. (parameterize ((%swh-base-url (%local-url)))
  41. exp ...)))
  42. (test-begin "swh")
  43. (test-equal "lookup-origin"
  44. (list 42 "git" "http://example.org/guix.git")
  45. (with-json-result %origin
  46. (let ((origin (lookup-origin "http://example.org/guix.git")))
  47. (list (origin-id origin)
  48. (origin-type origin)
  49. (origin-url origin)))))
  50. (test-equal "lookup-origin, not found"
  51. #f
  52. (with-http-server `((404 "Nope."))
  53. (parameterize ((%swh-base-url (%local-url)))
  54. (lookup-origin "http://example.org/whatever"))))
  55. (test-equal "lookup-directory"
  56. '(("one" 123) ("two" 456))
  57. (with-json-result %directory-entries
  58. (map (lambda (entry)
  59. (list (directory-entry-name entry)
  60. (directory-entry-length entry)))
  61. (lookup-directory "123"))))
  62. (test-equal "rate limit reached"
  63. 3000000000
  64. (let ((too-many (build-response
  65. #:code 429
  66. #:reason-phrase "Too many requests"
  67. ;; Pretend we've reached the limit and it'll be reset in
  68. ;; June 2065.
  69. #:headers '((x-ratelimit-remaining . "0")
  70. (x-ratelimit-reset . "3000000000")))))
  71. (with-http-server `((,too-many "Too bad."))
  72. (parameterize ((%swh-base-url (%local-url)))
  73. (catch 'swh-error
  74. (lambda ()
  75. (lookup-origin "http://example.org/guix.git"))
  76. (lambda (key url method response)
  77. ;; Ensure the reset time was recorded.
  78. (@@ (guix swh) %general-rate-limit-reset-time)))))))
  79. (test-assert "%allow-request? and request-rate-limit-reached?"
  80. ;; Here we test two things: that the rate limit set above is in effect and
  81. ;; that %ALLOW-REQUEST? is called, and that 'request-rate-limit-reached?'
  82. ;; returns true.
  83. (let* ((key (gensym "skip-request"))
  84. (skip-if-limit-reached
  85. (lambda (url method)
  86. (or (not (request-rate-limit-reached? url method))
  87. (throw key #t)))))
  88. (parameterize ((%allow-request? skip-if-limit-reached))
  89. (catch key
  90. (lambda ()
  91. (lookup-origin "http://example.org/guix.git")
  92. #f)
  93. (const #t)))))
  94. (test-end "swh")
  95. ;; Local Variables:
  96. ;; eval: (put 'with-json-result 'scheme-indent-function 1)
  97. ;; eval: (put 'with-http-server 'scheme-indent-function 1)
  98. ;; End: