Skip to content

Commit

Permalink
Merge pull request #29 from antavelos/manage-rel-url
Browse files Browse the repository at this point in the history
Write fix to handle relative URL
  • Loading branch information
creekorful authored Mar 12, 2021
2 parents ff16c52 + a9987b6 commit 569fbd2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"fmt"
"log"
"net/url"
"os/exec"
"strings"

Expand Down Expand Up @@ -655,8 +656,11 @@ func LoadContent(g *c.Gui, v *c.View) error {
if currItem == nil {
return nil
}

site := SitesList.CurrentItem().(db.Site)
event := currItem.(db.Event)
CurrentContent, _ = GetContent(event.Url)

CurrentContent, _ = GetContent(getContentURL(site, event.Url))
if err := UpdateContent(g, CurrentContent); err != nil {
log.Println("Error on UpdateContent", err)
return err
Expand Down Expand Up @@ -712,3 +716,16 @@ func Help(g *c.Gui, v *c.View) error {

return nil
}

func getContentURL(site db.Site, contentUrl string) string {
if !strings.HasPrefix(contentUrl, "http") {
u, err := url.Parse(site.Url)
if err != nil {
return contentUrl // it would have failed anyway (todo: maybe log error message and display it to end user?)
}

return fmt.Sprintf("%s://%s/%s", u.Scheme, u.Host, strings.TrimPrefix(contentUrl, "/"))
}

return contentUrl
}
44 changes: 44 additions & 0 deletions ctrl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"github.com/antavelos/terminews/db"
"testing"
)

func TestGetContentURL(t *testing.T) {
type test struct {
site db.Site
url string
expected string
}

tests := []test{
{
site: db.Site{Url: "https://blog.example.org/index.xml"},
url: "/2021/02/a-blog-post",
expected: "https://blog.example.org/2021/02/a-blog-post",
},
{
site: db.Site{Url: "http://blog.example.org/index.xml"},
url: "2021/02/a-blog-post",
expected: "http://blog.example.org/2021/02/a-blog-post",
},
{
site: db.Site{Url: "https://blog.example.org/feed.rss"},
url: "https://blog.example.org/super-blog-post",
expected: "https://blog.example.org/super-blog-post",
},
{
site: db.Site{Url: "http://blog.example.org/feed.rss"},
url: "http://blog.example.org/super-blog-post",
expected: "http://blog.example.org/super-blog-post",
},
}

for _, test := range tests {
value := getContentURL(test.site, test.url)
if value != test.expected {
t.Errorf("got: %s want: %s", value, test.expected)
}
}
}

0 comments on commit 569fbd2

Please sign in to comment.