Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supply library-defined props with NewConnectionProperties #157

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ type Config struct {
Dial func(network, addr string) (net.Conn, error)
}

// NewConnectionProperties initialises an amqp.Table struct to empty value. This
// amqp.Table can be used as Properties in amqp.Config to set the connection
// name, using amqp.DialConfig()
// NewConnectionProperties creates an amqp.Table to be used as amqp.Config.Properties.
//
// Defaults to library-defined values. For empty properties, use make(amqp.Table) instead.
func NewConnectionProperties() Table {
return make(Table)
return Table{
"product": defaultProduct,
"version": buildVersion,
"platform": platform,
}
}

// Connection manages the serialization and deserialization of frames from IO
Expand Down Expand Up @@ -875,11 +879,7 @@ func (c *Connection) openStart(config Config) error {

func (c *Connection) openTune(config Config, auth Authentication) error {
if len(config.Properties) == 0 {
config.Properties = Table{
"product": defaultProduct,
"version": buildVersion,
"platform": platform,
}
config.Properties = NewConnectionProperties()
}

config.Properties["capabilities"] = Table{
Expand Down
47 changes: 47 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package amqp091
import (
"crypto/tls"
"net"
"regexp"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -285,3 +286,49 @@ func TestConnectionConfigPropertiesWithClientProvidedConnectionName(t *testing.T
expectedConnectionName)
}
}

func TestNewConnectionProperties_HasDefaultProperties(t *testing.T) {
expectedProductName := defaultProduct
expectedPlatform := platform

props := NewConnectionProperties()

productName, ok := props["product"]
if !ok {
t.Fatal("Product name was not set by NewConnectionProperties")
}
if productName != expectedProductName {
t.Fatalf("Product name is set to: %s. Expected: %s",
productName,
expectedProductName,
)
}

platform, ok := props["platform"]
if !ok {
t.Fatal("Platform was not set by NewConnectionProperties")
}
if platform != expectedPlatform {
t.Fatalf("Platform is set to: %s. Expected: %s",
platform,
expectedPlatform,
)
}

versionUntyped, ok := props["version"]
if !ok {
t.Fatal("Version was not set by NewConnectionProperties")
}

version, ok := versionUntyped.(string)
if !ok {
t.Fatalf("Version in NewConnectionProperties should be string. Type given was: %T", versionUntyped)
}

// semver regexp as specified by https://semver.org/
semverRegexp := regexp.MustCompile(`^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`)

if !semverRegexp.MatchString(version) {
t.Fatalf("Version in NewConnectionProperties is not a valid semver value: %s", version)
}
}