unit_message_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2013 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution, and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andrew Young
  11. */
  12. package mqtt
  13. import (
  14. "net/url"
  15. "testing"
  16. )
  17. func Test_UsernamePassword(t *testing.T) {
  18. options := NewClientOptions()
  19. options.Username = "username"
  20. options.Password = "password"
  21. m := newConnectMsgFromOptions(options, &url.URL{})
  22. if m.Username != "username" {
  23. t.Fatalf("Username not set correctly")
  24. }
  25. if string(m.Password) != "password" {
  26. t.Fatalf("Password not set correctly")
  27. }
  28. }
  29. func Test_CredentialsProvider(t *testing.T) {
  30. options := NewClientOptions()
  31. options.Username = "incorrect"
  32. options.Password = "incorrect"
  33. options.SetCredentialsProvider(func() (username string, password string) {
  34. return "username", "password"
  35. })
  36. m := newConnectMsgFromOptions(options, &url.URL{})
  37. if m.Username != "username" {
  38. t.Fatalf("Username not set correctly")
  39. }
  40. if string(m.Password) != "password" {
  41. t.Fatalf("Password not set correctly")
  42. }
  43. }
  44. func Test_BrokerCredentials(t *testing.T) {
  45. m := newConnectMsgFromOptions(
  46. NewClientOptions(),
  47. &url.URL{User: url.UserPassword("username", "password")},
  48. )
  49. if m.Username != "username" {
  50. t.Fatalf("Username not set correctly")
  51. }
  52. if string(m.Password) != "password" {
  53. t.Fatalf("Password not set correctly")
  54. }
  55. }