From 951fe1223472a411d96888dff6a5ca3e0b8bd14d Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Sun, 9 Feb 2020 16:30:29 -0500 Subject: [PATCH] test for checkURL --- methods/validation_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 methods/validation_test.go diff --git a/methods/validation_test.go b/methods/validation_test.go new file mode 100644 index 0000000..fa1f981 --- /dev/null +++ b/methods/validation_test.go @@ -0,0 +1,50 @@ +package methods + +import "testing" + +var checkURLCases = []struct { + name string + url string + expectErr bool +}{ + { + name: "valid http://", + url: "http://example.com", + expectErr: false, + }, + { + name: "valid https://", + url: "https://example.com", + expectErr: false, + }, + { + name: "empty url", + url: "", + expectErr: true, + }, + { + name: "invalid protocol", + url: "htp://example.com", + expectErr: true, + }, + { + name: "disallowed protocol", + url: "irc://example.com", + expectErr: true, + }, +} + +func Test_checkURL(t *testing.T) { + for _, tt := range checkURLCases { + out, err := checkURL(tt.url) + if err != nil && !tt.expectErr { + t.Errorf("%s :: %s", tt.name, err.Error()) + } + if out != tt.url && !tt.expectErr { + t.Errorf("URL mangled. Got %s - expected %s", out, tt.url) + } + if out != "" && err != nil && tt.expectErr { + t.Errorf("Didn't fail when expected") + } + } +}