Skip to content

Commit

Permalink
ensure file:// URIs have leading slash if creating from scheme-less URI
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Jul 13, 2021
1 parent 54d5123 commit 296cfdf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
5 changes: 3 additions & 2 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
func TestFileURI(t *testing.T) {

ctx := context.Background()

candidates := []string{
"/tmp/example.jpg",
"tmp/example.jpg",
"file:///tmp/example.jpg",
}

Expand All @@ -36,5 +37,5 @@ func TestFileURI(t *testing.T) {
t.Fatalf("Unexpected target for '%s': '%s'", str_uri, target)
}
}

}
28 changes: 14 additions & 14 deletions idsecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package uri

import (
"context"
"testing"
"fmt"
"net/url"
"regexp"
"path/filepath"
"fmt"
"regexp"
"testing"
)

func TestIdSecretURI(t *testing.T) {

ctx := context.Background()

candidates := []string{
"idsecret:///tmp/example.jpg?id=1234",
}
Expand All @@ -33,9 +33,9 @@ func TestIdSecretURI(t *testing.T) {

net_q := net_u.Query()

id := net_q.Get("id")
id := net_q.Get("id")
secret := net_q.Get("secret")
secret_o := net_q.Get("secret_o")
secret_o := net_q.Get("secret_o")

if id != "1234" {
t.Fatalf("Unexpected id value for '1234': '%s'", id)
Expand All @@ -51,10 +51,10 @@ func TestIdSecretURI(t *testing.T) {

format := "jpg"
label := "x"

values := &url.Values{}
values.Set("format", format)
values.Set("label", label)
values.Set("label", label)
target, err := u.Target(values)

if err != nil {
Expand All @@ -65,22 +65,22 @@ func TestIdSecretURI(t *testing.T) {

root := filepath.Dir(target)
fname := filepath.Base(target)

if root != tree {
t.Fatalf("Unexpected root: '%s'", root)
}

fname_pat := fmt.Sprintf("%s_([^_]+)_%s.%s", id, label, format)
fname_re, err := regexp.Compile(fname_pat)

if err != nil {
t.Fatalf("Failed to compile fname pattern, %v", err)
}
if !fname_re.MatchString(fname){

if !fname_re.MatchString(fname) {
t.Fatalf("Filename failed pattern match: '%s'", fname)
}

}

}
4 changes: 2 additions & 2 deletions rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestRewriteURI(t *testing.T) {

ctx := context.Background()

candidates := []string{
"rewrite:///tmp/bob.jpg?target=alice.jpg",
}
Expand All @@ -35,5 +35,5 @@ func TestRewriteURI(t *testing.T) {
t.Fatalf("Unexpected target for '%s': '%s'", str_uri, target)
}
}

}
6 changes: 6 additions & 0 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ func NewURI(ctx context.Context, uri string) (URI, error) {
scheme := u.Scheme

if scheme == "" {

scheme = "file"
u.Scheme = scheme

if !strings.HasPrefix(u.Path, "/") {
u.Path = fmt.Sprintf("/%s", u.Path)
}

uri = u.String()
}

Expand Down
4 changes: 2 additions & 2 deletions uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
func TestNewURI(t *testing.T) {

ctx := context.Background()

candidates := []string{
"/tmp/example.jpg",
"file:///tmp/example.jpg",
"idsecret:///tmp/example.jpg?id=1234",
"rewrite:///tmp/example.jpg?target=bob.jpg",
"rewrite:///tmp/example.jpg?target=bob.jpg",
}

for _, str_uri := range candidates {
Expand Down

0 comments on commit 296cfdf

Please sign in to comment.