Adds unit tests for Utils

This commit is contained in:
Manu Mtz-Almeida 2015-04-08 15:32:50 +02:00
parent c61c547539
commit ac1ee3fb86
3 changed files with 63 additions and 6 deletions

View File

@ -81,8 +81,7 @@ func processAccounts(accounts Accounts) authPairs {
if len(user) == 0 { if len(user) == 0 {
panic("User can not be empty") panic("User can not be empty")
} }
base := user + ":" + password value := authorizationHeader(user, password)
value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
pairs = append(pairs, authPair{ pairs = append(pairs, authPair{
Value: value, Value: value,
User: user, User: user,

View File

@ -89,10 +89,11 @@ func joinPaths(absolutePath, relativePath string) string {
if len(relativePath) == 0 { if len(relativePath) == 0 {
return absolutePath return absolutePath
} }
absolutePath = path.Join(absolutePath, relativePath)
appendSlash := lastChar(relativePath) == '/' && lastChar(absolutePath) != '/' finalPath := path.Join(absolutePath, relativePath)
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
if appendSlash { if appendSlash {
return absolutePath + "/" return finalPath + "/"
} }
return absolutePath return finalPath
} }

57
utils_test.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
SetMode(TestMode)
}
func TestLastChar(t *testing.T) {
assert.Equal(t, lastChar("hola"), uint8('a'))
assert.Equal(t, lastChar("adios"), uint8('s'))
assert.Panics(t, func() { lastChar("") })
}
func TestParseAccept(t *testing.T) {
parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
assert.Len(t, parts, 4)
assert.Equal(t, parts[0], "text/html")
assert.Equal(t, parts[1], "application/xhtml+xml")
assert.Equal(t, parts[2], "application/xml")
assert.Equal(t, parts[3], "*/*")
}
func TestChooseData(t *testing.T) {
A := "a"
B := "b"
assert.Equal(t, chooseData(A, B), A)
assert.Equal(t, chooseData(nil, B), B)
assert.Panics(t, func() { chooseData(nil, nil) })
}
func TestFilterFlags(t *testing.T) {
result := filterFlags("text/html ")
assert.Equal(t, result, "text/html")
result = filterFlags("text/html;")
assert.Equal(t, result, "text/html")
}
func TestJoinPaths(t *testing.T) {
assert.Equal(t, joinPaths("/a", ""), "/a")
assert.Equal(t, joinPaths("/a/", ""), "/a/")
assert.Equal(t, joinPaths("/a/", "/"), "/a/")
assert.Equal(t, joinPaths("/a", "/"), "/a/")
assert.Equal(t, joinPaths("/a", "/hola"), "/a/hola")
assert.Equal(t, joinPaths("/a/", "/hola"), "/a/hola")
assert.Equal(t, joinPaths("/a/", "/hola/"), "/a/hola/")
assert.Equal(t, joinPaths("/a/", "/hola//"), "/a/hola/")
}