You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers_test.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. func Test_IsValidURL(t *testing.T) {
  11. cases := []struct {
  12. description string
  13. url string
  14. valid bool
  15. }{
  16. {
  17. description: "Empty URL",
  18. url: "",
  19. valid: false,
  20. },
  21. {
  22. description: "Loobpack IPv4 URL",
  23. url: "http://127.0.1.1:5678/",
  24. valid: true,
  25. },
  26. {
  27. description: "Loobpack IPv6 URL",
  28. url: "https://[::1]/",
  29. valid: true,
  30. },
  31. {
  32. description: "Missing semicolon after schema",
  33. url: "http//meh/",
  34. valid: false,
  35. },
  36. }
  37. for _, testCase := range cases {
  38. t.Run(testCase.description, func(t *testing.T) {
  39. assert.Equal(t, testCase.valid, IsValidURL(testCase.url))
  40. })
  41. }
  42. }
  43. func Test_IsValidExternalURL(t *testing.T) {
  44. setting.AppURL = "https://try.gitea.io/"
  45. cases := []struct {
  46. description string
  47. url string
  48. valid bool
  49. }{
  50. {
  51. description: "Current instance URL",
  52. url: "https://try.gitea.io/test",
  53. valid: true,
  54. },
  55. {
  56. description: "Loobpack IPv4 URL",
  57. url: "http://127.0.1.1:5678/",
  58. valid: false,
  59. },
  60. {
  61. description: "Current instance API URL",
  62. url: "https://try.gitea.io/api/v1/user/follow",
  63. valid: false,
  64. },
  65. {
  66. description: "Local network URL",
  67. url: "http://192.168.1.2/api/v1/user/follow",
  68. valid: true,
  69. },
  70. {
  71. description: "Local URL",
  72. url: "http://LOCALHOST:1234/whatever",
  73. valid: false,
  74. },
  75. }
  76. for _, testCase := range cases {
  77. t.Run(testCase.description, func(t *testing.T) {
  78. assert.Equal(t, testCase.valid, IsValidExternalURL(testCase.url))
  79. })
  80. }
  81. }