Skip to content

Commit

Permalink
fix: return file protocol by default
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed Jul 2, 2024
1 parent d154bd3 commit aaf1554
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/utils/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import "net/url"

func ParseProtocol(uri string) (protocol string, err error) {
parsed, err := url.Parse(uri)
if parsed.Scheme == "" {
return "file", nil
}
return parsed.Scheme, err
}
48 changes: 48 additions & 0 deletions pkg/utils/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils_test

import (
"testing"

"github.com/WangYihang/gojob/pkg/utils"
)

func TestParseProtocol(t *testing.T) {
testcases := []struct {
uri string
expected string
}{
{
uri: "http://example.com",
expected: "http",
},
{
uri: "https://example.com",
expected: "https",
},
{
uri: "ftp://example.com",
expected: "ftp",
},
{
uri: "file://example.com",
expected: "file",
},
{
uri: "s3://bucket/shakespeare.txt",
expected: "s3",
},
{
uri: "/etc/passwd",
expected: "file",
},
}
for _, tc := range testcases {
protocol, err := utils.ParseProtocol(tc.uri)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if protocol != tc.expected {
t.Errorf("expected %s, got %s", tc.expected, protocol)
}
}
}

0 comments on commit aaf1554

Please sign in to comment.