Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janiltonmaciel committed Sep 20, 2020
1 parent 6da231b commit 0830133
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ builds:
- linux
ignore:
- goarch: 386
ldflags:
- -X main.version={{.Version}} -w -s
ldflags:
- -X main.version={{.Version}} -w -s

release:
github:
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ release: git-tag
echo "Release - OK"

push-release:
goreleaser release --rm-dist
export GITHUB_TOKEN=$(GITHUB_TOKEN); \
goreleaser release --rm-dist --skip-validate

## Prints this help
help:
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ EXAMPLES:
statiks --ssl --cert cert.pem --key key.pem
- start server at http://0.0.0.0:9080 serving "/tmp" with delay response 100ms
statiks -add-delay 100 /tmp
statiks --add-delay 100 /tmp
{{- if .Version }}
Expand Down
21 changes: 21 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ func (s *StatiksSuite) TestVersionPrinter(c *check.C) {
vp := cmd.VersionPrinter(commit, date)
vp(ctx)
}

func (s *StatiksSuite) TestRun(c *check.C) {
version := "v0.1"
commit := "da3c509"
date := "2020-09-03T14:45:36Z"
err := cmd.Run(version, commit, date)
c.Assert(err, check.NotNil)
}

func (s *StatiksSuite) TestAction(c *check.C) {
version := "v0.1"
app := cmd.NewApp(version)

set := flag.NewFlagSet("test", 0)
set.String("host", "localhost2332", "")

ctx := cli.NewContext(app, set, nil)

err := app.Action(ctx)
c.Assert(err, check.NotNil)
}
7 changes: 4 additions & 3 deletions lib/export_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package lib

var (
NoCacheHandler = noCacheHandler
CacheHandler = cacheHandler
DelayHandler = delayHandler
NoCacheHandler = noCacheHandler
CacheHandler = cacheHandler
DelayHandler = delayHandler
WriteNotModified = writeNotModified
)
69 changes: 69 additions & 0 deletions lib/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"net/http"

"github.com/janiltonmaciel/statiks/lib"
"github.com/urfave/cli/v2"
check "gopkg.in/check.v1"
)

Expand All @@ -20,11 +22,29 @@ func (s *StatiksSuite) TestServerPathDefault(c *check.C) {

func (s *StatiksSuite) TestServerEnabledIndex(c *check.C) {
set := s.newFlagSet()
set.Bool("no-index", false, "")
e := s.newHTTPTester(set)

resp := e.GET("/").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("README.md")

resp = e.GET("/").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("README.md")

resp = e.GET("index.html").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("README.md")

resp = e.GET("/index.html").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("README.md")

resp = e.GET("/cmd/").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("root.go")

}

func (s *StatiksSuite) TestServerDisabledIndex(c *check.C) {
Expand Down Expand Up @@ -148,3 +168,52 @@ func (s *StatiksSuite) TestServerEnabledSSL(c *check.C) {
resp.Status(http.StatusOK)
resp.Body().NotEmpty()
}

func (s *StatiksSuite) TestServerRun(c *check.C) {
set := s.newFlagSet()
ctx := cli.NewContext(nil, set, nil)
config := lib.NewConfig(ctx)
config.Address = "localhost:invalid"
server := lib.NewServer(config)
err := server.Run()
c.Assert(err, check.NotNil)

config.SSL = true
config.Address = "localhost:invalid"
server = lib.NewServer(config)
err = server.Run()
c.Assert(err, check.NotNil)
}

type FakeResponse struct {
headers http.Header
body []byte
status int
}

func (r *FakeResponse) Header() http.Header {
return r.headers
}

func (r *FakeResponse) Write(body []byte) (int, error) {
r.body = body
return len(body), nil
}

func (r *FakeResponse) WriteHeader(status int) {
r.status = status
}

func (s *StatiksSuite) TestWriteNotModified(c *check.C) {
fr := &FakeResponse{
headers: map[string][]string{
"Content-Type": {"json"},
"Content-Length": {"100"},
"Etag": {"Etagval"},
"Last-Modified": {"Lastval"},
},
}
lib.WriteNotModified(fr)
c.Assert(fr.Header(), check.HasLen, 1)
c.Assert(fr.Header()["Etag"][0], check.Equals, "Etagval")
}

0 comments on commit 0830133

Please sign in to comment.