-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package config | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
) | ||
|
||
type Listener struct { | ||
Network string `yaml:"network"` | ||
Address string `yaml:"address"` | ||
TLS *TLS `yaml:"tls"` | ||
} | ||
|
||
func (l *Listener) Listen() (net.Listener, error) { | ||
ls, lErr := net.Listen(l.Network, l.Address) | ||
if lErr != nil { | ||
return nil, lErr | ||
} | ||
if l.TLS == nil { | ||
return ls, nil | ||
} | ||
config, cErr := l.TLS.Config() | ||
if cErr != nil { | ||
ls.Close() | ||
return nil, cErr | ||
} | ||
return tls.NewListener(ls, config), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestListener(t *testing.T) { | ||
t.Run("No TLS", func(tt *testing.T) { | ||
c := Listener{ | ||
Network: "tcp", | ||
Address: "localhost:0", | ||
} | ||
l, lErr := c.Listen() | ||
assert.Nil(tt, lErr) | ||
defer l.Close() | ||
}) | ||
t.Run("With TLS", func(tt *testing.T) { | ||
c := Listener{ | ||
Network: "tcp", | ||
Address: "localhost:0", | ||
TLS: &TLS{}, | ||
} | ||
l, lErr := c.Listen() | ||
assert.Nil(tt, lErr) | ||
defer l.Close() | ||
}) | ||
t.Run("Invalid Addr", func(tt *testing.T) { | ||
c := Listener{ | ||
Network: "tcp", | ||
Address: "localhost:9999999", | ||
TLS: &TLS{}, | ||
} | ||
_, lErr := c.Listen() | ||
assert.NotNil(tt, lErr) | ||
}) | ||
t.Run("Invalid TLS", func(tt *testing.T) { | ||
c := Listener{ | ||
Network: "tcp", | ||
Address: "localhost:0", | ||
TLS: &TLS{ | ||
CertFile: new(string), | ||
KeyFile: new(string), | ||
}, | ||
} | ||
_, lErr := c.Listen() | ||
assert.NotNil(tt, lErr) | ||
}) | ||
} |