forked from eclipse-paho/paho.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass
*url.URL
to ConnectPacketBuilder
As per discussion in eclipse-paho#192 I've added URL as a parameter, and also an example on how to use it to extract password from URL
- Loading branch information
Showing
4 changed files
with
43 additions
and
10 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
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 |
---|---|---|
|
@@ -413,7 +413,7 @@ func TestClientConfig_buildConnectPacket(t *testing.T) { | |
} | ||
|
||
// Validate initial state | ||
cp := config.buildConnectPacket(true) | ||
cp := config.buildConnectPacket(true, nil) | ||
|
||
if !cp.CleanStart { | ||
t.Errorf("Expected Clean Start to be true") | ||
|
@@ -434,7 +434,7 @@ func TestClientConfig_buildConnectPacket(t *testing.T) { | |
config.SetUsernamePassword("testuser", []byte("testpassword")) | ||
config.SetWillMessage(fmt.Sprintf("client/%s/state", config.ClientID), []byte("disconnected"), 1, true) | ||
|
||
cp = config.buildConnectPacket(false) | ||
cp = config.buildConnectPacket(false, nil) | ||
if cp.CleanStart { | ||
t.Errorf("Expected Clean Start to be false") | ||
} | ||
|
@@ -470,16 +470,50 @@ func TestClientConfig_buildConnectPacket(t *testing.T) { | |
} | ||
|
||
// Set an override method for the CONNECT packet | ||
config.SetConnectPacketConfigurator(func(c *paho.Connect) *paho.Connect { | ||
config.SetConnectPacketConfigurator(func(c *paho.Connect, u *url.URL) *paho.Connect { | ||
delay := uint32(200) | ||
c.WillProperties.WillDelayInterval = &delay | ||
c.Username = u.User.Username() | ||
p, _ := u.User.Password() | ||
c.Password = []byte(p) | ||
return c | ||
}) | ||
|
||
cp = config.buildConnectPacket(false) | ||
testUrl, _ := url.Parse("mqtt://mqtt_user:[email protected]:1883") | ||
cp = config.buildConnectPacket(false, testUrl) | ||
|
||
if *(cp.WillProperties.WillDelayInterval) != 200 { // verifies the override | ||
t.Errorf("Will message Delay Interval did not match expected [200]: found [%v]", *(cp.Properties.WillDelayInterval)) | ||
} | ||
if cp.Username != "mqtt_user" { // verifies the override | ||
t.Errorf("username did not match expected [mqtt_user]: found [%v]", cp.Username) | ||
} | ||
if string(cp.Password) != "mqtt_pass" { // verifies the override | ||
t.Errorf("password did not match expected [mqtt_pass]: found [%v]", string(cp.Password)) | ||
} | ||
|
||
} | ||
|
||
// Example of using ConnectPacketBuilder to extract server password from URL | ||
func ExampleClientConfig_ConnectPacketBuilder() { | ||
serverURL, _ := url.Parse("mqtt://mqtt_user:[email protected]:1883") | ||
config := ClientConfig{ | ||
ServerUrls: []*url.URL{serverURL}, | ||
ConnectRetryDelay: 5 * time.Second, | ||
ConnectTimeout: 5 * time.Second, | ||
ClientConfig: paho.ClientConfig{ | ||
ClientID: "test", | ||
}, | ||
} | ||
config.ConnectPacketBuilder = func(c *paho.Connect, u *url.URL) *paho.Connect { | ||
// Extracting password from URL | ||
c.Username = u.User.Username() | ||
// up to user to catch empty password passed via URL | ||
p, _ := u.User.Password() | ||
c.Password = []byte(p) | ||
return c | ||
} | ||
cp := config.buildConnectPacket(false, serverURL) | ||
fmt.Printf("user: %s, pass: %s", cp.Username, string(cp.Password)) | ||
// Output: user: mqtt_user, pass: mqtt_pass | ||
} |
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
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