Skip to content

Commit

Permalink
tests: up the test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo committed Aug 3, 2020
1 parent 8f9b60b commit 1541c4d
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 186 deletions.
137 changes: 0 additions & 137 deletions .coverprofile

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Output of the go coverage tool
*.coverprofile

# Dependency directories (remove the comment below to include it)
# vendor/
27 changes: 27 additions & 0 deletions convutil/conv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package convutil

import (
"testing"

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

func TestS2bAndB2s(t *testing.T) {
r := "s2b and b2s"
b := S2b(r)
s := B2s(b)

assert.Equal(t, []byte(r), b)
assert.Equal(t, r, s)
}

func TestNextNumberPowerOf2(t *testing.T) {
assert.Equal(t, uint64(16), NextNumberPowerOf2(10))
assert.Equal(t, uint64(32), NextNumberPowerOf2(20))
assert.Equal(t, uint64(32), NextNumberPowerOf2(30))
assert.Equal(t, uint64(64), NextNumberPowerOf2(40))
assert.Equal(t, uint64(128), NextNumberPowerOf2(100))
assert.Equal(t, uint64(256), NextNumberPowerOf2(200))
assert.Equal(t, uint64(512), NextNumberPowerOf2(500))
assert.Equal(t, uint64(1024), NextNumberPowerOf2(1000))
}
11 changes: 11 additions & 0 deletions cryptoutil/md5.go → cryptoutil/cryptoutil.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
package cryptoutil

import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
)

// Md5Hex
func Md5Hex(str string) string {
hbs := md5.Sum([]byte(str))
return hex.EncodeToString(hbs[:])
}

// Md5HexShort
func Md5HexShort(str string) string {
hbs := md5.Sum([]byte(str))
return hex.EncodeToString(hbs[4:12])
}

// HmacHex
func HmacHex(data, key []byte) string {
hmacSha1 := hmac.New(sha1.New, key)
hmacSha1.Write(data)
return hex.EncodeToString(hmacSha1.Sum(nil))
}
22 changes: 22 additions & 0 deletions cryptoutil/cryptoutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cryptoutil

import (
"testing"

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

func TestHmacHex(t *testing.T) {
hex := HmacHex([]byte("123"), []byte("key"))
assert.Equal(t, "d4a5b6721d75a5ac15ec698818c77fe1f6e40187", hex)
}

func TestMd5Hex(t *testing.T) {
hex := Md5Hex("123")
assert.Equal(t, "202cb962ac59075b964b07152d234b70", hex)
}

func TestMd5HexShort(t *testing.T) {
hex := Md5HexShort("123")
assert.Equal(t, "ac59075b964b0715", hex)
}
13 changes: 0 additions & 13 deletions cryptoutil/hmac.go

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/saltbo/gopkg
go 1.14

require (
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.15.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand Down
26 changes: 26 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package strutil

import (
"testing"

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

func TestStrInSlice(t *testing.T) {
s := []string{"test1", "test2", "test3", "test4", "test5"}
assert.False(t, StrInSlice("test", s))
assert.True(t, StrInSlice("test3", s))
}

func TestBoolFromStr(t *testing.T) {
for _, trueString := range TrueStrings {
assert.True(t, BoolFromStr(trueString, false))
}

for _, trueString := range FalseStrings {
assert.False(t, BoolFromStr(trueString, true))
}

assert.True(t, BoolFromStr("ok1", true))
assert.False(t, BoolFromStr("ok1", false))
}
13 changes: 6 additions & 7 deletions testutil/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/http/httputil"
)

var defaultMux = new(testSimpleMux)
var defaultMux = &mockServer{}

type testRequest struct {
req *http.Request
Expand All @@ -32,19 +32,18 @@ func (tr *testRequest) Debug() *testRequest {
return tr
}

func (tr *testRequest) Host(host string) *testRequest {
tr.req.Host = host
func (tr *testRequest) Mux(mux http.Handler) *testRequest {
tr.mux = mux
return tr
}

func (tr *testRequest) Mux(mux http.Handler) *testRequest {
tr.mux = mux
func (tr *testRequest) Host(host string) *testRequest {
tr.req.Host = host
return tr
}

func (tr *testRequest) Body(body []byte) *testRequest {
tr.BodyReader(bytes.NewBuffer(body))
return tr
return tr.BodyReader(bytes.NewBuffer(body))
}

func (tr *testRequest) BodyReader(br io.Reader) *testRequest {
Expand Down
21 changes: 16 additions & 5 deletions testutil/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ type mockServer struct {
*httptest.Server

req *http.Request
host string
duration time.Duration
respBody []byte
}

func NewMockServer() *mockServer {
ms := new(mockServer)
ms.Server = httptest.NewServer(http.HandlerFunc(ms.handler))
ms.Server = httptest.NewServer(ms)

ms.host = ms.URL().Host
ms.respBody = []byte("mock succ!")
return ms
}

func (m *mockServer) SetHost(host string) {
m.host = host
}

func (m *mockServer) SetDuration(d time.Duration) {
m.duration = d
}
Expand All @@ -36,18 +43,22 @@ func (m *mockServer) URL() *url.URL {
return u
}

func (m *mockServer) handler(w http.ResponseWriter, r *http.Request) {
func (m *mockServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Host != m.host {
w.WriteHeader(http.StatusServiceUnavailable)
return
}

m.req = r
if m.duration > 0 {
time.Sleep(m.duration)
}

for _, cookie := range r.Cookies() {
w.Header().Add("Set-Cookie", cookie.String())
http.SetCookie(w, cookie)
}

_, err := w.Write(m.respBody)
if err != nil {
if _, err := w.Write(m.respBody); err != nil {
log.Printf("[mockServer] response failed: %s", err)
return
}
Expand Down
18 changes: 0 additions & 18 deletions testutil/testmux.go

This file was deleted.

Loading

0 comments on commit 1541c4d

Please sign in to comment.