Skip to content
Permalink
Browse files
unitary test
  • Loading branch information
jcabrerizo committed May 27, 2019
1 parent 628b9ae commit d3d4f0e68f48c45d720284086b08705e89c17b3b
Showing 2 changed files with 30 additions and 55 deletions.
@@ -19,6 +19,7 @@
package io

import (
"encoding/base64"
"path/filepath"
"testing"
)
@@ -28,13 +29,8 @@ func TestConfig(t *testing.T) {
testFileFormat(t, "testconfig.json")
testFileFormat(t, "legacyConfig.json")

testCredentialsRequired(t, "testconfig.json",true)
testCredentialsRequired(t, "legacyConfig.json", false)
testCredentialsRequired(t, "testConfigHeaders.json", false)

testHeaders(t, "testconfig.json", nil)
testHeaders(t, "legacyConfig.json", nil)
testHeaders(t, "testConfigHeaders.json", mockHeaders())
testAuthType(t, "testconfig.json", true)
testAuthType(t, "legacyConfig.json", false)
}

func mockHeaders() (expectedHeaders map[string]interface{}) {
@@ -45,40 +41,6 @@ func mockHeaders() (expectedHeaders map[string]interface{}) {
return
}

func testCredentialsRequired(t *testing.T,testFile string, expected bool) {
config :=getConfigFromFile(t,testFile)
isCredentialsRequired := config.GetCredentialsRequired()
assertBool(nil,t,"isCredentialsRequired",isCredentialsRequired,expected)
}

func testHeaders(t *testing.T, testFile string, expectedHeaders map[string]interface{}) {
config :=getConfigFromFile(t,testFile)
userHeaders := config.GetUserHeaders()

assertHeaders(nil, t, userHeaders, expectedHeaders)
}

func assertHeaders(err error, t *testing.T, actualHeaders map[string]interface{}, expectedHeaders map[string]interface{}) {
var expectedLen=len(expectedHeaders)

if len(actualHeaders) != expectedLen{
t.Errorf("Headers len != %d: %d ",expectedLen, len(actualHeaders))
}else{
i := 0
keysExpected := make([]string, len(expectedHeaders))
for k:= range expectedHeaders {
keysExpected[i] = k
i++
if val, found := actualHeaders[k]; found{
if expectedHeaders[k] != val{
t.Errorf("Header value for %s != %s:%s ",k,expectedHeaders[k],val)
}
}else{
t.Errorf("Header not found %s ",k)
}
}
}
}

func getConfigFromFile(t *testing.T, testFile string)(config *Config) {
config = new(Config)
@@ -97,32 +59,42 @@ func getConfigFromFile(t *testing.T, testFile string)(config *Config) {
}

func testFileFormat(t *testing.T, testFile string) {
config :=getConfigFromFile(t,testFile)
config := getConfigFromFile(t, testFile)

_, username, password, err := config.GetNetworkCredentials()
assertUserPassword(err, t, username, "user1", password, "password1")
_, credentials, err := config.GetNetworkCredentials()
assertCredentials(err, t, credentials, base64.StdEncoding.EncodeToString([]byte("user1:password1")))

credentials, err = config.GetNetworkCredentialsForTarget("http://another.one:8081")
assertCredentials(err, t, credentials, base64.StdEncoding.EncodeToString([]byte("user2"+":"+"password2")))

username, password, err = config.GetNetworkCredentialsForTarget("http://another.one:8081")
assertUserPassword(err, t, username, "user2", password, "password2")
}

func assertUserPassword(err error, t *testing.T, username string, expectedUser string, password string, expectedPassword string) {
func testAuthType(t *testing.T, testFile string, checkDefaultBehaviour bool) {
config := getConfigFromFile(t, testFile)

authType, err := config.GetAuthType("http://some.site:8081")
assertAuthType(err, t, authType, "Basic")

if checkDefaultBehaviour {
authType, err = config.GetAuthType("http://another.one:8081")
assertAuthType(err, t, authType, "Bearer")
}
}

func assertCredentials(err error, t *testing.T, credentials string, expectedCredentials string) {
if err != nil {
t.Error(err)
}
if username != expectedUser {
t.Errorf("username != %s: %s", expectedUser, username)
}
if password != expectedPassword {
t.Errorf("password != %s: %s", expectedPassword, username)
if credentials != expectedCredentials {
t.Errorf("credentials != %s: %s", credentials, expectedCredentials)
}
}

func assertBool(err error, t *testing.T, paramDescription string, expectedValue bool, actualValue bool) {
func assertAuthType(err error, t *testing.T, authType string, expectedAuthType string) {
if err != nil {
t.Error(err)
}
if actualValue != expectedValue {
t.Errorf("%s != %t: %t",paramDescription, expectedValue, actualValue )
if authType != expectedAuthType {
t.Errorf("authType != %s: %s", authType, expectedAuthType)
}
}
@@ -3,6 +3,9 @@
"http://some.site:8081": "dXNlcjE6cGFzc3dvcmQx",
"http://another.one:8081": "dXNlcjI6cGFzc3dvcmQy"
},
"authType": {
"http://another.one:8081": "Bearer"
},
"skipSslChecks": false,
"target": "http://some.site:8081"
}

0 comments on commit d3d4f0e

Please sign in to comment.