Skip to content

Commit

Permalink
Handle missing URL scheme in base path correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mat committed Oct 25, 2015
1 parent 1e107b5 commit e379f05
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions besticon/besticon.go
Expand Up @@ -326,6 +326,10 @@ func absoluteURL(baseURL *url.URL, path string) (string, error) {
}

url.Scheme = baseURL.Scheme
if url.Scheme == "" {
url.Scheme = "http"
}

if url.Host == "" {
url.Host = baseURL.Host
}
Expand All @@ -335,6 +339,10 @@ func absoluteURL(baseURL *url.URL, path string) (string, error) {
func urlFromBase(baseURL *url.URL, path string) string {
url := *baseURL
url.Path = path
if url.Scheme == "" {
url.Scheme = "http"
}

return url.String()
}

Expand Down
11 changes: 11 additions & 0 deletions besticon/besticon_test.go
Expand Up @@ -101,6 +101,17 @@ func TestEat24WithBaseTag(t *testing.T) {
assertEquals(t, expectedImages, actualImages)
}

func TestAlibabaWithBaseTagWithoutScheme(t *testing.T) {
actualImages, err := fetchIconsWithVCR("alibaba.vcr", "alibaba.com")
assertEquals(t, nil, err)
expectedImages := []Icon{
{URL: "http://is.alicdn.com/simg/single/icon/favicon.ico", Width: 16, Height: 16, Format: "ico", Bytes: 1406, Sha1sum: "4ffbef9b6044c62cd6c8b1ee0913ba93e6e80072"},
{URL: "http://www.alibaba.com/favicon.ico", Width: 16, Height: 16, Format: "ico", Bytes: 1406, Sha1sum: "4ffbef9b6044c62cd6c8b1ee0913ba93e6e80072"},
}

assertEquals(t, expectedImages, actualImages)
}

func TestARDWithSortBySize(t *testing.T) {
actualImages, err := fetchIconsWithVCR("ard.vcr", "ard.de")
assertEquals(t, nil, err)
Expand Down
Binary file added besticon/testdata/alibaba.vcr
Binary file not shown.
3 changes: 3 additions & 0 deletions vcr/vcr.go
Expand Up @@ -104,6 +104,9 @@ func (t *recorderTransport) RoundTrip(r *http.Request) (*http.Response, error) {
defer t.mutex.Unlock()

resp, err := defaultTransport.RoundTrip(r)
if err != nil {
panic(err)
}

logRequest(t.writer, r)
logResponse(t.writer, resp, true)
Expand Down

0 comments on commit e379f05

Please sign in to comment.