Skip to content

Commit

Permalink
tests: improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zostay committed Jan 18, 2024
1 parent af7c654 commit 6fcca56
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/ost/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"github.com/zostay/today/pkg/text/esv"
)

const rootURL = `https://openscripture.today`
const DefaultBaseURL = `https://openscripture.today`

type Client struct {
svc *text.Service
Client *http.Client
Service *text.Service
BaseURL string
}

func New() (*Client, error) {
Expand All @@ -26,12 +28,14 @@ func New() (*Client, error) {

svc := text.NewService(res)
return &Client{
svc: svc,
Client: http.DefaultClient,
BaseURL: DefaultBaseURL,
Service: svc,
}, nil
}

func (c *Client) TodayVerse() (*Verse, error) {
ru, err := url.Parse(rootURL)
ru, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +59,7 @@ func (c *Client) Today() (string, error) {
return "", err
}

return c.svc.Verse(verse.Reference)
return c.Service.Verse(verse.Reference)
}

func (c *Client) TodayHTML() (template.HTML, error) {
Expand All @@ -64,5 +68,5 @@ func (c *Client) TodayHTML() (template.HTML, error) {
return "", err
}

return c.svc.VerseHTML(verse.Reference)
return c.Service.VerseHTML(verse.Reference)
}
78 changes: 78 additions & 0 deletions pkg/ost/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ost_test

import (
"html/template"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"github.com/zostay/today/pkg/ost"
"github.com/zostay/today/pkg/ref"
"github.com/zostay/today/pkg/text"
)

var today = ost.Verse{
Reference: "Luke 10:25",
Content: "And behold, a lawyer stood up to put him to the test, saying, “Teacher, what shall I do to inherit eternal life?”",
Version: ost.Version{
Name: "ESV",
Link: "https://www.esv.org/Luke+10:25",
},
}

type testResolver struct {
lastRef *ref.Resolved
}

func (t *testResolver) Verse(ref *ref.Resolved) (string, error) {
t.lastRef = ref
return string(today.Content), nil
}

func (t *testResolver) VerseHTML(ref *ref.Resolved) (template.HTML, error) {
t.lastRef = ref
return template.HTML(today.Content), nil

Check failure on line 37 in pkg/ost/client_test.go

View workflow job for this annotation

GitHub Actions / Test and Sanity

G203: The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input. (gosec)
}

var _ text.Resolver = (*testResolver)(nil)

func testServer() *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
enc := yaml.NewEncoder(w)
err := enc.Encode(today)
if err != nil {
panic(err)
}
},
))
return ts
}

func TestClient(t *testing.T) {
t.Parallel()

ts := testServer()
defer ts.Close()

c := &ost.Client{
BaseURL: ts.URL,
Client: http.DefaultClient,
Service: text.NewService(&testResolver{}),
}

v, err := c.TodayVerse()
assert.NoError(t, err)
assert.Equal(t, &today, v)

txt, err := c.Today()
assert.NoError(t, err)
assert.Equal(t, string(today.Content), txt)

htxt, err := c.TodayHTML()
assert.NoError(t, err)
assert.Equal(t, template.HTML(today.Content), htxt)

Check failure on line 77 in pkg/ost/client_test.go

View workflow job for this annotation

GitHub Actions / Test and Sanity

G203: The used method does not auto-escape HTML. This can potentially lead to 'Cross-site Scripting' vulnerabilities, in case the attacker controls the input. (gosec)
}

0 comments on commit 6fcca56

Please sign in to comment.