mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
first commit
This commit is contained in:
113
parser/configurationsParser_test.go
Normal file
113
parser/configurationsParser_test.go
Normal file
@ -0,0 +1,113 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mockReadfilebytesConfigurationsCore(filePath string) ([]byte, error) {
|
||||
configurationsCoreBytes := []byte(`
|
||||
core:
|
||||
logging:
|
||||
debug: false
|
||||
debugReportCaller: false
|
||||
logDisableTimestamp: true
|
||||
logsPath: ./logs`)
|
||||
return configurationsCoreBytes, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesFormatError(filePath string) ([]byte, error) {
|
||||
configurationsCoreBytes := []byte(`{{}`)
|
||||
return configurationsCoreBytes, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesError(filePath string) ([]byte, error) {
|
||||
return nil, errors.New("mockErrorReadFileBytes")
|
||||
}
|
||||
|
||||
func mockReadDirError(dirPath string) ([]string, error) {
|
||||
return nil, errors.New("mockErrorReadFileBytes")
|
||||
}
|
||||
|
||||
func mockReadDirValid(dirPath string) ([]string, error) {
|
||||
return []string{""}, nil
|
||||
}
|
||||
|
||||
func mockReadfilebytesBeelzebubServiceConfiguration(filePath string) ([]byte, error) {
|
||||
beelzebubServiceConfiguration := []byte(`
|
||||
apiVersion: "v1"
|
||||
protocol: "http"
|
||||
address: ":8080"
|
||||
commands:
|
||||
- regex: "wp-admin"
|
||||
handler: "login"
|
||||
headers:
|
||||
- "Content-Type: text/html"`)
|
||||
return beelzebubServiceConfiguration, nil
|
||||
}
|
||||
|
||||
func TestReadConfigurationsCoreError(t *testing.T) {
|
||||
configurationsParser := Init("mockConfigurationsCorePath", "mockConfigurationsServicesDirectory")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
||||
beelzebubCoreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||
|
||||
assert.Nil(t, beelzebubCoreConfigurations)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "in file mockConfigurationsCorePath: mockErrorReadFileBytes", err.Error())
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesFormatError
|
||||
|
||||
beelzebubCoreConfigurations, err = configurationsParser.ReadConfigurationsCore()
|
||||
assert.Nil(t, beelzebubCoreConfigurations)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "in file mockConfigurationsCorePath: yaml: line 1: did not find expected ',' or '}'", err.Error())
|
||||
}
|
||||
|
||||
func TestReadConfigurationsCoreValid(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
configurationsParser.readFileBytes = mockReadfilebytesConfigurationsCore
|
||||
|
||||
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, coreConfigurations.Core)
|
||||
assert.NotNil(t, coreConfigurations.Core.Logging)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.Debug, false)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.LogDisableTimestamp, true)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.DebugReportCaller, false)
|
||||
assert.Equal(t, coreConfigurations.Core.Logging.LogsPath, "./logs")
|
||||
}
|
||||
|
||||
func TestReadConfigurationsServicesFail(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesError
|
||||
configurationsParser.readDir = mockReadDirError
|
||||
|
||||
beelzebubServiceConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||
assert.Nil(t, beelzebubServiceConfiguration)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestReadConfigurationsServicesValid(t *testing.T) {
|
||||
configurationsParser := Init("", "")
|
||||
|
||||
configurationsParser.readFileBytes = mockReadfilebytesBeelzebubServiceConfiguration
|
||||
configurationsParser.readDir = mockReadDirValid
|
||||
|
||||
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
||||
|
||||
firstBeelzebubServiceConfiguration := beelzebubServicesConfiguration[0]
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Protocol, "http")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.ApiVersion, "v1")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Address, ":8080")
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands), 1)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Regex, "wp-admin")
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Handler, "login")
|
||||
assert.Equal(t, len(firstBeelzebubServiceConfiguration.Commands[0].Headers), 1)
|
||||
assert.Equal(t, firstBeelzebubServiceConfiguration.Commands[0].Headers[0], "Content-Type: text/html")
|
||||
}
|
Reference in New Issue
Block a user