Skip to content

Commit

Permalink
Updated new url mapping loading
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 19, 2022
1 parent ebd7f80 commit 6044382
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
31 changes: 31 additions & 0 deletions internal/config/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package config

import (
"errors"

"github.com/spf13/viper"
)

var (
ErrNoToPair = errors.New("`to` values are not set for every `from`")
ErrNoFromPair = errors.New("`from` values are not set for every `to`")
)

func ReadURLMapping(config *viper.Viper) (map[string]string, error) {
urlMappings := map[string]string{}
from, to := config.GetStringSlice("from"), config.GetStringSlice("to") // nolint: varnamelen

if len(from) > len(to) {
return nil, ErrNoToPair
}

if len(to) > len(from) {
return nil, ErrNoFromPair
}

for index, key := range from {
urlMappings[key] = to[index]
}

return urlMappings, nil
}
71 changes: 71 additions & 0 deletions internal/config/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config_test

import (
"testing"

"github.com/evg4b/uncors/internal/config"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestReadURLMapping(t *testing.T) {
t.Run("correctly map pairs", func(t *testing.T) {
viperConfig := viper.New()
viperConfig.Set("from", []string{"host1", "host2", "host3"})
viperConfig.Set("to", []string{"target-host1", "target-host2", "target-host3"})
actual, err := config.ReadURLMapping(viperConfig)

assert.NoError(t, err)
assert.EqualValues(t, map[string]string{
"host1": "target-host1",
"host2": "target-host2",
"host3": "target-host3",
}, actual)
})

t.Run("incorrect pairs", func(t *testing.T) {
tests := []struct {
name string
from []string
to []string
expectedErr string
}{
{
name: "from is not set",
from: []string{"host1"},
to: []string{},
expectedErr: "`to` values are not set for every `from`",
},
{
name: "to is not set",
from: []string{},
to: []string{"target-host1"},
expectedErr: "`from` values are not set for every `to`",
},
{
name: "count of from values greath then count of to",
from: []string{"host1", "host2"},
to: []string{"target-host1"},
expectedErr: "`to` values are not set for every `from`",
},
{
name: "count of to values greath then count of from",
from: []string{"host1"},
to: []string{"target-host1", "target-host2"},
expectedErr: "`from` values are not set for every `to`",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
viperConfig := viper.New()
viperConfig.Set("from", testCase.from)
viperConfig.Set("to", testCase.to)

actual, err := config.ReadURLMapping(viperConfig)

assert.Nil(t, actual)
assert.EqualError(t, err, testCase.expectedErr)
})
}
})
}
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/infrastructure"
"github.com/evg4b/uncors/internal/log"
"github.com/evg4b/uncors/internal/mock"
Expand All @@ -29,8 +30,8 @@ const (
)

func main() {
pflag.String("target", "https://github.com", "Target host with protocol for to the resource to be proxy")
pflag.String("source", "localhost", "Local host with protocol for to the resource from which proxying will take place") // nolint: lll
pflag.StringSlice("to", []string{}, "Target host with protocol for to the resource to be proxy")
pflag.StringSlice("from", []string{}, "Local host with protocol for to the resource from which proxying will take place") // nolint: lll
pflag.Int("http-port", defaultHTTPPort, "Local HTTP listening port")
pflag.Int("https-port", defaultHTTPSPort, "Local HTTPS listening port")
pflag.String("cert-file", "", "Path to HTTPS certificate file")
Expand Down Expand Up @@ -80,8 +81,12 @@ func main() {

mock.MakeMockedRoutes(router, ui.MockLogger, mocksDefs)

urlMappings, err := config.ReadURLMapping(viper.GetViper())
if err != nil {
panic(err)
}
mappings, err := urlreplacer.NormaliseMappings(
map[string]string{viper.GetString("source"): viper.GetString("target")},
urlMappings,
httpPort,
httpsPort,
len(certFile) > 0 && len(keyFile) > 0,
Expand Down

0 comments on commit 6044382

Please sign in to comment.