Improve unit tests in url package
This commit is contained in:
parent
c1fe84521e
commit
d5ff4191b6
1 changed files with 46 additions and 94 deletions
140
url/url_test.go
140
url/url_test.go
|
@ -6,116 +6,68 @@ package url // import "miniflux.app/url"
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestGetAbsoluteURLWithAbsolutePath(t *testing.T) {
|
func TestAbsoluteURL(t *testing.T) {
|
||||||
expected := `https://example.org/path/file.ext`
|
scenarios := [][]string{
|
||||||
input := `/path/file.ext`
|
[]string{"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
|
||||||
output, err := AbsoluteURL("https://example.org/folder/", input)
|
[]string{"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
|
||||||
|
[]string{"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
|
||||||
if err != nil {
|
[]string{"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
|
||||||
t.Error(err)
|
[]string{"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if expected != output {
|
for _, scenario := range scenarios {
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
actual, err := AbsoluteURL(scenario[1], scenario[2])
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if actual != scenario[0] {
|
||||||
|
t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAbsoluteURLWithRelativePath(t *testing.T) {
|
func TestRootURL(t *testing.T) {
|
||||||
expected := `https://example.org/folder/path/file.ext`
|
scenarios := map[string]string{
|
||||||
input := `path/file.ext`
|
"https://example.org/path/file.ext": "https://example.org/",
|
||||||
output, err := AbsoluteURL("https://example.org/folder/", input)
|
"//static.example.org/path/file.ext": "https://static.example.org/",
|
||||||
|
"https://example|org/path/file.ext": "https://example|org/path/file.ext",
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if expected != output {
|
for input, expected := range scenarios {
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
actual := RootURL(input)
|
||||||
}
|
if actual != expected {
|
||||||
}
|
t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
|
||||||
|
}
|
||||||
func TestGetAbsoluteURLWithRelativePaths(t *testing.T) {
|
|
||||||
expected := `https://example.org/path/file.ext`
|
|
||||||
input := `path/file.ext`
|
|
||||||
output, err := AbsoluteURL("https://example.org/folder", input)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWhenInputIsAlreadyAbsolute(t *testing.T) {
|
|
||||||
expected := `https://example.org/path/file.ext`
|
|
||||||
input := `https://example.org/path/file.ext`
|
|
||||||
output, err := AbsoluteURL("https://example.org/folder/", input)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetAbsoluteURLWithProtocolRelative(t *testing.T) {
|
|
||||||
expected := `https://static.example.org/path/file.ext`
|
|
||||||
input := `//static.example.org/path/file.ext`
|
|
||||||
output, err := AbsoluteURL("https://www.example.org/", input)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetRootURL(t *testing.T) {
|
|
||||||
expected := `https://example.org/`
|
|
||||||
input := `https://example.org/path/file.ext`
|
|
||||||
output := RootURL(input)
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetRootURLWithProtocolRelativePath(t *testing.T) {
|
|
||||||
expected := `https://static.example.org/`
|
|
||||||
input := `//static.example.org/path/file.ext`
|
|
||||||
output := RootURL(input)
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsHTTPS(t *testing.T) {
|
func TestIsHTTPS(t *testing.T) {
|
||||||
if !IsHTTPS("https://example.org/") {
|
scenarios := map[string]bool{
|
||||||
t.Error("Unable to recognize HTTPS URL")
|
"https://example.org/": true,
|
||||||
|
"http://example.org/": false,
|
||||||
|
"https://example|org/": false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsHTTPS("http://example.org/") {
|
for input, expected := range scenarios {
|
||||||
t.Error("Unable to recognize HTTP URL")
|
actual := IsHTTPS(input)
|
||||||
}
|
if actual != expected {
|
||||||
|
t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
|
||||||
if IsHTTPS("") {
|
}
|
||||||
t.Error("Unable to recognize malformed URL")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetDomain(t *testing.T) {
|
func TestDomain(t *testing.T) {
|
||||||
expected := `static.example.org`
|
scenarios := map[string]string{
|
||||||
input := `http://static.example.org/`
|
"https://static.example.org/": "static.example.org",
|
||||||
output := Domain(input)
|
"https://example|org/": "https://example|org/",
|
||||||
|
}
|
||||||
|
|
||||||
if expected != output {
|
for input, expected := range scenarios {
|
||||||
t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
|
actual := Domain(input)
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue