From ac1ee3fb86ec6eed256dccadbe1dd06aeda8bfd5 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 8 Apr 2015 15:32:50 +0200 Subject: [PATCH] Adds unit tests for Utils --- auth.go | 3 +-- utils.go | 9 ++++---- utils_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 utils_test.go diff --git a/auth.go b/auth.go index 077aca34..dc37a85f 100644 --- a/auth.go +++ b/auth.go @@ -81,8 +81,7 @@ func processAccounts(accounts Accounts) authPairs { if len(user) == 0 { panic("User can not be empty") } - base := user + ":" + password - value := "Basic " + base64.StdEncoding.EncodeToString([]byte(base)) + value := authorizationHeader(user, password) pairs = append(pairs, authPair{ Value: value, User: user, diff --git a/utils.go b/utils.go index 19fef551..e4d144fc 100644 --- a/utils.go +++ b/utils.go @@ -89,10 +89,11 @@ func joinPaths(absolutePath, relativePath string) string { if len(relativePath) == 0 { return absolutePath } - absolutePath = path.Join(absolutePath, relativePath) - appendSlash := lastChar(relativePath) == '/' && lastChar(absolutePath) != '/' + + finalPath := path.Join(absolutePath, relativePath) + appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/' if appendSlash { - return absolutePath + "/" + return finalPath + "/" } - return absolutePath + return finalPath } diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 00000000..ad7d1be7 --- /dev/null +++ b/utils_test.go @@ -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/") +}