1
0
Fork 0

UDP support

Co-authored-by: Julien Salleyron <julien.salleyron@gmail.com>
This commit is contained in:
mpl 2020-02-11 01:26:04 +01:00 committed by GitHub
parent 8988c8f9af
commit 115d42e0f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
72 changed files with 4730 additions and 321 deletions

View file

@ -1,5 +1,10 @@
package static
import (
"fmt"
"strings"
)
// EntryPoint holds the entry point configuration.
type EntryPoint struct {
Address string `description:"Entry point address." json:"address,omitempty" toml:"address,omitempty" yaml:"address,omitempty"`
@ -8,11 +13,34 @@ type EntryPoint struct {
ForwardedHeaders *ForwardedHeaders `description:"Trust client forwarding headers." json:"forwardedHeaders,omitempty" toml:"forwardedHeaders,omitempty" yaml:"forwardedHeaders,omitempty"`
}
// GetAddress strips any potential protocol part of the address field of the
// entry point, in order to return the actual address.
func (ep EntryPoint) GetAddress() string {
splitN := strings.SplitN(ep.Address, "/", 2)
return splitN[0]
}
// GetProtocol returns the protocol part of the address field of the entry point.
// If none is specified, it defaults to "tcp".
func (ep EntryPoint) GetProtocol() (string, error) {
splitN := strings.SplitN(ep.Address, "/", 2)
if len(splitN) < 2 {
return "tcp", nil
}
protocol := strings.ToLower(splitN[1])
if protocol == "tcp" || protocol == "udp" {
return protocol, nil
}
return "", fmt.Errorf("invalid protocol: %s", splitN[1])
}
// SetDefaults sets the default values.
func (e *EntryPoint) SetDefaults() {
e.Transport = &EntryPointsTransport{}
e.Transport.SetDefaults()
e.ForwardedHeaders = &ForwardedHeaders{}
func (ep *EntryPoint) SetDefaults() {
ep.Transport = &EntryPointsTransport{}
ep.Transport.SetDefaults()
ep.ForwardedHeaders = &ForwardedHeaders{}
}
// ForwardedHeaders Trust client forwarding headers.

View file

@ -0,0 +1,67 @@
package static
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestEntryPointProtocol(t *testing.T) {
tests := []struct {
name string
address string
expectedAddress string
expectedProtocol string
expectedError bool
}{
{
name: "Without protocol",
address: "127.0.0.1:8080",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "tcp",
expectedError: false,
},
{
name: "With TCP protocol in upper case",
address: "127.0.0.1:8080/TCP",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "tcp",
expectedError: false,
},
{
name: "With UDP protocol in upper case",
address: "127.0.0.1:8080/UDP",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "udp",
expectedError: false,
},
{
name: "With UDP protocol in weird case",
address: "127.0.0.1:8080/uDp",
expectedAddress: "127.0.0.1:8080",
expectedProtocol: "udp",
expectedError: false,
},
{
name: "With invalid protocol",
address: "127.0.0.1:8080/toto/tata",
expectedError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ep := EntryPoint{
Address: tt.address,
}
protocol, err := ep.GetProtocol()
if tt.expectedError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expectedProtocol, protocol)
require.Equal(t, tt.expectedAddress, ep.GetAddress())
})
}
}