Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 28, 2019
1 parent 60fda4a commit 484a3c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 7 additions & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ func resolveAddress(addr []string) (fullAddr string) {
return ip + ":8080"
case 1:
var port string

// "IP:PORT"
if strings.IndexByte(addr[0], ':') != -1 {
ss := strings.SplitN(addr[0], ":", 2)
if ss[0] != "" {
return addr[0]
}
port = ss[1]
} else {
} else { // Only port
port = addr[0]
}

Expand Down Expand Up @@ -131,8 +133,11 @@ func debugPrint(f string, v ...interface{}) {

// from gin framework
func parseAccept(acceptHeader string) []string {
parts := strings.Split(acceptHeader, ",")
if acceptHeader == "" {
return []string{}
}

parts := strings.Split(acceptHeader, ",")
if len(parts) == 0 {
return []string{}
}
Expand Down
37 changes: 37 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package rux

import (
"errors"
"testing"

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

func TestHelper(t *testing.T) {

// resolveAddress
addr := resolveAddress(nil)
assert.Equal(t, "0.0.0.0:8080", addr)

addr = resolveAddress([]string{"9090"})
assert.Equal(t, "0.0.0.0:9090", addr)

addr = resolveAddress([]string{"127.0.0.1:9090"})
assert.Equal(t, "127.0.0.1:9090", addr)

// use ENV

// debugPrintError
debugPrintError(errors.New("error msg"))

// parseAccept
ss := parseAccept("")
assert.Len(t, ss, 0)

ss = parseAccept(",")
assert.Len(t, ss, 0)

ss = parseAccept("application/json")
assert.Len(t, ss, 1)
assert.Equal(t, []string{"application/json"}, ss)
}

0 comments on commit 484a3c8

Please sign in to comment.