Skip to content

Commit

Permalink
Fixes panic on printing user parts when non exist
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed May 2, 2018
1 parent adb58f7 commit 4b00659
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
69 changes: 30 additions & 39 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,58 @@ package main

import (
"net/url"
"runtime/debug"
"strconv"
"testing"
)

func TestFormat(t *testing.T) {
cases := []struct {
url string
format string
expected string
wantPanic bool
url string
format string
expected string
}{
{"https://example.com/foo", "%d", "example.com", false},
{"https://example.com/foo", "%d%p", "example.com/foo", false},
{"https://example.com/foo", "%s://%d%p", "https://example.com/foo", false},
{"https://example.com/foo", "%d", "example.com"},
{"https://example.com/foo", "%d%p", "example.com/foo"},
{"https://example.com/foo", "%s://%d%p", "https://example.com/foo"},

{"https://example.com:8080/foo", "%d", "example.com", false},
{"https://example.com:8080/foo", "%P", "8080", false},
{"https://example.com:8080/foo", "%d", "example.com"},
{"https://example.com:8080/foo", "%P", "8080"},

{"https://example.com/foo?a=b&c=d", "%p", "/foo", false},
{"https://example.com/foo?a=b&c=d", "%q", "a=b&c=d", false},
{"https://example.com/foo?a=b&c=d", "%p", "/foo"},
{"https://example.com/foo?a=b&c=d", "%q", "a=b&c=d"},

{"https://example.com/foo#bar", "%f", "bar", false},
{"https://example.com#bar", "%f", "bar", false},
{"https://example.com/foo#bar", "%f", "bar"},
{"https://example.com#bar", "%f", "bar"},

{"https://example.com#bar", "foo%%bar", "foo%bar", false},
{"https://example.com#bar", "%s://%%", "https://%", false},
{"https://example.com#bar", "foo%%bar", "foo%bar"},
{"https://example.com#bar", "%s://%%", "https://%"},

{"https://example.com:8080/?foo=bar#frag", "%:", ":", false},
{"https://example.com/", "%:", "", false},
{"https://example.com:8080/?foo=bar#frag", "%:", ":"},
{"https://example.com/", "%:", ""},

{"https://example.com:8080/?foo=bar#frag", "%?", "?", false},
{"https://example.com/", "%?", "", false},
{"https://example.com:8080/?foo=bar#frag", "%?", "?"},
{"https://example.com/", "%?", ""},

{"https://example.com:8080/?foo=bar#frag", "%#", "#", false},
{"https://example.com/", "%#", "", false},
{"https://example.com:8080/?foo=bar#frag", "%#", "#"},
{"https://example.com/", "%#", ""},

{"https://user:[email protected]:8080/?foo=bar#frag", "%u", "user:pass", false},
{"https://user:[email protected]:8080/?foo=bar#frag", "%@", "@", false},
{"https://example.com/", "%@", "", false},
{"https://example.com/", "%u", "", true},
{"https://user:[email protected]:8080/?foo=bar#frag", "%u", "user:pass"},
{"https://user:[email protected]:8080/?foo=bar#frag", "%@", "@"},
{"https://example.com/", "%@", ""},
{"https://example.com/", "%u", ""},

{"https://user:[email protected]:8080/?foo=bar#frag", "%a", "user:[email protected]:8080", false},
{"https://example.com:8080/?foo=bar#frag", "%a", "example.com:8080", true},
{"https://example.com/?foo=bar#frag", "%a", "example.com", true},
{"https://user:[email protected]:8080/?foo=bar#frag", "%a", "user:[email protected]:8080"},
{"https://example.com:8080/?foo=bar#frag", "%a", "example.com:8080"},
{"https://example.com/?foo=bar#frag", "%a", "example.com"},

{"https://sub.example.com:8080/foo", "%S", "sub", false},
{"https://sub.example.com:8080/foo", "%r", "example", false},
{"https://sub.example.com:8080/foo", "%t", "com", false},
{"https://sub.example.com:8080/foo", "%S", "sub"},
{"https://sub.example.com:8080/foo", "%r", "example"},
{"https://sub.example.com:8080/foo", "%t", "com"},
}

for i, c := range cases {

t.Run(strconv.Itoa(i), func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != c.wantPanic {
t.Errorf("panic recover = %v, wantPanic = %v", r, c.wantPanic)
debug.PrintStack()
}
}()

u, err := url.Parse(c.url)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ func format(u *url.URL, f string) []string {

// the userinfo; e.g. user:pass
case 'u':
out.WriteString(u.User.String())
if u.User != nil {
out.WriteString(u.User.String())
}

// the domain; e.g. sub.example.com
case 'd':
Expand Down

0 comments on commit 4b00659

Please sign in to comment.