Skip to content

Commit

Permalink
Add tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDmitry committed Jan 15, 2024
1 parent ff9421e commit b815ce8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,16 @@ jobs:

- name: Build
run: make build

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21.1'

- name: Test
run: make test
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ build:
run: build css
./${BIN_DIR}/${BINARY_NAME} $(ARGS)

test:
go test -v ./...

clean:
rm -rf ./${BIN_DIR}
rm -rf ./web/dist
2 changes: 1 addition & 1 deletion pkg/monke/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewArticle(f string, c string, urlPrefix string, tags []string) (*Article,
article.Category = c
article.Root = dir.Name()
article.Id = filepath.Base(article.Root)
article.Url = SanitizePath(fmt.Sprintf("%s/%s/", urlPrefix, article.Id))
article.Url = SanitizeUrl(fmt.Sprintf("%s/%s/", urlPrefix, article.Id))
article.ReadmePath = readme
article.Summary = string(summary)
article.ArticleData = meta.Data
Expand Down
1 change: 0 additions & 1 deletion pkg/monke/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ func SitemapXml(urlPrefix string) ([]byte, error) {
}

return xml.MarshalIndent(urlset, "", " ")

}
18 changes: 11 additions & 7 deletions pkg/monke/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@ package monke
import (
"net/url"
"path/filepath"
"strings"
)

func SanitizePath(s string) string {
if len(s) == 1 {
func sanitizePath(s string) string {
if len(s) == 0 {
return s
}
if s[0] != '/' {
s = "/" + s
}
trailingSlash := s[len(s)-1] == '/'
s, err := filepath.Abs(s)
if err != nil {
return ""
}
switch trailingSlash {
case true:
s = strings.TrimRight(s, "/")

if trailingSlash {
return s + "/"
default:
return s
}
return s
}

func SanitizeUrl(s string) string {
u, err := url.Parse(s)
if err != nil {
return ""
}
u.Path = SanitizePath(u.Path)
u.Path = sanitizePath(u.Path)
return u.String()
}
30 changes: 30 additions & 0 deletions pkg/monke/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package monke

import (
"testing"
)

func TestSanitizeUrl(t *testing.T) {
testData := map[string]string{
"": "", // empty string stays empty
"a": "/a", // path becomes absolute
"a/": "/a/", // path becomes absolute
"/": "/", // root stays root
"/root": "/root", // path stays path
"/root/": "/root/", // trailing slash is preserved
"/root/../other": "/other", // relative paths are resolved
"/root/../../other": "/other", // relative paths are resolved
"/🤡": "/%F0%9F%A4%A1", // funny symbols are encoded
"/%F0%9F%A4%A1": "/%F0%9F%A4%A1", // encoded paths are preserved
"/path?query": "/path?query", // query is preserved
"/path/?query": "/path/?query", // query is preserved
"/path/?param=/path/": "/path/?param=/path/", // query is preserved
"/path/?param=/path/../../../": "/path/?param=/path/../../../", // query is preserved
}
for s, want := range testData {
got := SanitizeUrl(s)
if got != want {
t.Fatalf("Failed to sanitize %s: expected %s, got %s", s, want, got)
}
}
}

0 comments on commit b815ce8

Please sign in to comment.