Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jun 7, 2019
1 parent 11c1397 commit 73ae301
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 16 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: go
sudo: required

go:
- "1.11"
- "1.12"
- master

Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.formatTool": "goimports"
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ test:
test-cover:
go test -race -coverprofile=test.out ./... && go tool cover --html=test.out


bench:
go test -bench=. ./...

release:
go mod tidy
4 changes: 2 additions & 2 deletions etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type (
func gen(buf []byte) string {
size := len(buf)
if size == 0 {
return "\"0-2jmj7l5rSw0yVb_vlWAYkK_YBwk=\""
return `"0-2jmj7l5rSw0yVb_vlWAYkK_YBwk="`
}
h := sha1.New()
h.Write(buf)
hash := base64.URLEncoding.EncodeToString(h.Sum(nil))
return fmt.Sprintf("\"%x-%s\"", size, hash)
return fmt.Sprintf(`"%x-%s"`, size, hash)
}

// NewDefault create a default ETag middleware
Expand Down
98 changes: 86 additions & 12 deletions etag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,67 @@ package etag

import (
"bytes"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"math/rand"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vicanso/cod"
)

var testData []byte

func init() {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

fn := func(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
testData = []byte(fn(4096))
}

func TestGen(t *testing.T) {
assert := assert.New(t)
assert.Equal(gen([]byte("")), `"0-2jmj7l5rSw0yVb_vlWAYkK_YBwk="`)
}

func TestETag(t *testing.T) {
fn := NewDefault()
t.Run("curstom error", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
customErr := errors.New("abcd")
c.Next = func() error {
return customErr
}
err := fn(c)
assert.Equal(err, customErr)
})

t.Run("no body", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c.Next = func() error {
return nil
}
err := fn(c)
if err != nil {
t.Fatalf("eTag middleware fail, %v", err)
}
if c.GetHeader(cod.HeaderETag) != "" {
t.Fatalf("no body should not gen eTag")
}
assert.Nil(err)
assert.Empty(c.GetHeader(cod.HeaderETag))
})

t.Run("error status", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c.Next = func() error {
Expand All @@ -37,12 +74,8 @@ func TestETag(t *testing.T) {
return nil
}
err := fn(c)
if err != nil {
t.Fatalf("eTag middleware fail, %v", err)
}
if c.GetHeader(cod.HeaderETag) != "" {
t.Fatalf("error status should not gen eTag")
}
assert.Nil(err)
assert.Empty(c.GetHeader(cod.HeaderETag))
})

t.Run("gen eTag", func(t *testing.T) {
Expand All @@ -64,3 +97,44 @@ func TestETag(t *testing.T) {
}
})
}

func BenchmarkGenETag(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
gen(testData)
}
}

func BenchmarkMd5(b *testing.B) {
b.ReportAllocs()
fn := func(buf []byte) string {
size := len(buf)
if size == 0 {
return `"0-2jmj7l5rSw0yVb_vlWAYkK_YBwk="`
}
h := md5.New()
h.Write(buf)
hash := base64.URLEncoding.EncodeToString(h.Sum(nil))
return fmt.Sprintf(`"%x-%s"`, size, hash)
}
for i := 0; i < b.N; i++ {
fn(testData)
}
}

// https://stackoverflow.com/questions/50120427/fail-unit-tests-if-coverage-is-below-certain-percentage
func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
rc := m.Run()

// rc 0 means we've passed,
// and CoverMode will be non empty if run with -cover
if rc == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < 0.9 {
fmt.Println("Tests passed but coverage failed at", c)
rc = -1
}
}
os.Exit(rc)
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/vicanso/cod-etag

require github.com/vicanso/cod v0.0.7
go 1.12

require (
github.com/stretchr/testify v1.3.0
github.com/vicanso/cod v0.1.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/vicanso/cod v0.0.7 h1:CHyIL7zkNOhEv0JosDDyRHCUALovry1P+dLISaANj1w=
github.com/vicanso/cod v0.0.7/go.mod h1:tHGTs/JDTcp/OE4VaCutVMpYL69aR1rKUggsHviTuFc=
github.com/vicanso/cod v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I=
github.com/vicanso/cod v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw=
github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o=
github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo=
github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM=
Expand Down

0 comments on commit 73ae301

Please sign in to comment.