|
| 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 | + |
| 5 | +package validation |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/setting" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_IsValidURL(t *testing.T) { |
| 16 | + cases := []struct { |
| 17 | + description string |
| 18 | + url string |
| 19 | + valid bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + description: "Empty URL", |
| 23 | + url: "", |
| 24 | + valid: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + description: "Loobpack IPv4 URL", |
| 28 | + url: "http://127.0.1.1:5678/", |
| 29 | + valid: true, |
| 30 | + }, |
| 31 | + { |
| 32 | + description: "Loobpack IPv6 URL", |
| 33 | + url: "https://[::1]/", |
| 34 | + valid: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + description: "Missing semicolon after schema", |
| 38 | + url: "http//meh/", |
| 39 | + valid: false, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, testCase := range cases { |
| 44 | + t.Run(testCase.description, func(t *testing.T) { |
| 45 | + assert.Equal(t, testCase.valid, IsValidURL(testCase.url)) |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func Test_IsValidExternalURL(t *testing.T) { |
| 51 | + setting.AppURL = "https://try.gitea.io/" |
| 52 | + |
| 53 | + cases := []struct { |
| 54 | + description string |
| 55 | + url string |
| 56 | + valid bool |
| 57 | + }{ |
| 58 | + { |
| 59 | + description: "Current instance URL", |
| 60 | + url: "https://try.gitea.io/test", |
| 61 | + valid: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + description: "Loobpack IPv4 URL", |
| 65 | + url: "http://127.0.1.1:5678/", |
| 66 | + valid: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "Current instance API URL", |
| 70 | + url: "https://try.gitea.io/api/v1/user/follow", |
| 71 | + valid: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + description: "Local network URL", |
| 75 | + url: "http://192.168.1.2/api/v1/user/follow", |
| 76 | + valid: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "Local URL", |
| 80 | + url: "http://LOCALHOST:1234/whatever", |
| 81 | + valid: false, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, testCase := range cases { |
| 86 | + t.Run(testCase.description, func(t *testing.T) { |
| 87 | + assert.Equal(t, testCase.valid, IsValidExternalURL(testCase.url)) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments