Debian Codegraph @

pkg/services/push/mqtt/mqtt_example_test.go

raw
1 package mqtt_test
2
3 import (
4 "fmt"
5 "io"
6 "log"
7 "net/url"
8
9 "github.com/nicholas-fedor/shoutrrr/pkg/services/push/mqtt"
10 "github.com/nicholas-fedor/shoutrrr/pkg/types"
11 )
12
13 // ExampleService_basic demonstrates basic MQTT usage without authentication.
14 func ExampleService_basic() {
15 // Create a new MQTT service
16 service := &mqtt.Service{}
17
18 // Parse the MQTT URL for a local broker
19 // Format: mqtt://host:port/topic
20 serviceURL, err := url.Parse("mqtt://localhost:1883/notifications")
21 if err != nil {
22 log.Printf("Failed to parse URL: %v", err)
23
24 return
25 }
26
27 // Initialize the service with a discard logger for this example
28 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
29 if err != nil {
30 log.Printf("Failed to initialize service: %v", err)
31
32 return
33 }
34
35 // The service is now configured and ready to send messages
36 // Note: Actual sending requires a running MQTT broker
37 fmt.Println("MQTT service initialized successfully")
38 // Output: MQTT service initialized successfully
39 }
40
41 // ExampleService_withAuth demonstrates MQTT with username/password authentication.
42 func ExampleService_withAuth() {
43 // Create a new MQTT service
44 service := &mqtt.Service{}
45
46 // Parse the MQTT URL with authentication credentials
47 // Format: mqtt://username:password@host:port/topic
48 serviceURL, err := url.Parse("mqtt://myuser:[email protected]:1883/sensors/data")
49 if err != nil {
50 log.Printf("Failed to parse URL: %v", err)
51
52 return
53 }
54
55 // Initialize the service
56 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
57 if err != nil {
58 log.Printf("Failed to initialize service: %v", err)
59
60 return
61 }
62
63 // The service is configured with authentication
64 fmt.Println("MQTT service with auth initialized successfully")
65 // Output: MQTT service with auth initialized successfully
66 }
67
68 // ExampleService_withTLS demonstrates secure MQTT connection using TLS.
69 func ExampleService_withTLS() {
70 // Create a new MQTT service
71 service := &mqtt.Service{}
72
73 // Parse the MQTTS URL for secure connection
74 // Format: mqtts://host:port/topic
75 // The mqtts scheme automatically enables TLS
76 serviceURL, err := url.Parse("mqtts://broker.example.com:8883/secure-topic")
77 if err != nil {
78 log.Printf("Failed to parse URL: %v", err)
79
80 return
81 }
82
83 // Initialize the service
84 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
85 if err != nil {
86 log.Printf("Failed to initialize service: %v", err)
87
88 return
89 }
90
91 // The service is configured for secure TLS connection
92 fmt.Println("MQTTS service with TLS initialized successfully")
93 // Output: MQTTS service with TLS initialized successfully
94 }
95
96 // ExampleService_withQoS demonstrates MQTT with Quality of Service settings.
97 func ExampleService_withQoS() {
98 // Create a new MQTT service
99 service := &mqtt.Service{}
100
101 // Parse the MQTT URL with QoS and retained message options
102 // qos=1 ensures at-least-once delivery
103 // retained=yes keeps the message on the broker for new subscribers
104 serviceURL, err := url.Parse(
105 "mqtt://broker.example.com/alerts?qos=1&retained=yes&clientid=myapp-client",
106 )
107 if err != nil {
108 log.Printf("Failed to parse URL: %v", err)
109
110 return
111 }
112
113 // Initialize the service
114 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
115 if err != nil {
116 log.Printf("Failed to initialize service: %v", err)
117
118 return
119 }
120
121 // The service is configured with QoS 1 and retained messages
122 fmt.Println("MQTT service with QoS settings initialized successfully")
123 // Output: MQTT service with QoS settings initialized successfully
124 }
125
126 // ExampleService_withCustomConfig demonstrates MQTT with custom configuration.
127 func ExampleService_withCustomConfig() {
128 // Create a new MQTT service
129 service := &mqtt.Service{}
130
131 // Parse the MQTTS URL with various configuration options
132 // - cleansession=no: persist session across connections
133 // - disabletlsverification=yes: skip TLS cert verification
134 //
135 // WARNING: disabletlsverification=yes bypasses certificate validation and
136 // should ONLY be used for testing with self-signed certificates.
137 // NEVER use this option in production environments as it exposes
138 // connections to man-in-the-middle attacks.
139 serviceURL, err := url.Parse(
140 "mqtts://broker.example.com/events?cleansession=no&disabletlsverification=yes",
141 )
142 if err != nil {
143 log.Printf("Failed to parse URL: %v", err)
144
145 return
146 }
147
148 // Initialize the service
149 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
150 if err != nil {
151 log.Printf("Failed to initialize service: %v", err)
152
153 return
154 }
155
156 // The service is configured with custom settings including TLS verification bypass
157 // Note: disabletlsverification is only applicable to mqtts:// (TLS) connections
158 fmt.Println("MQTTS service with custom config initialized successfully")
159 // Output: MQTTS service with custom config initialized successfully
160 }
161
162 // ExampleService_withParams demonstrates sending a message with parameters.
163 func ExampleService_withParams() {
164 // Create a new MQTT service
165 service := &mqtt.Service{}
166
167 // Parse the MQTT URL
168 serviceURL, err := url.Parse("mqtt://broker.example.com/notifications")
169 if err != nil {
170 log.Printf("Failed to parse URL: %v", err)
171
172 return
173 }
174
175 // Initialize the service
176 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
177 if err != nil {
178 log.Printf("Failed to initialize service: %v", err)
179
180 return
181 }
182
183 // Create optional parameters for the message
184 params := types.Params{
185 "title": "Alert Title",
186 }
187
188 // The service is ready to send messages with parameters.
189 // Note: Actual sending requires a running MQTT broker.
190 // Example usage (uncomment to send with params, requires a running broker):
191 //
192 // err = service.Send("Your message payload", &params)
193 // if err != nil {
194 // log.Printf("Failed to send message: %v", err)
195 //
196 // return
197 // }
198 _ = params // Placeholder to prevent unused variable error in this example
199
200 fmt.Println("Service ready for parameterized messages")
201 // Output: Service ready for parameterized messages
202 }
203
204 // ExampleService_serviceURL demonstrates accessing the service configuration
205 // after initialization.
206 func ExampleService_serviceURL() {
207 // Create a new MQTT service
208 service := &mqtt.Service{}
209
210 // Parse and initialize with a complex URL
211 serviceURL, err := url.Parse(
212 "mqtt://user:[email protected]:1883/test/topic?qos=2&retained=yes",
213 )
214 if err != nil {
215 log.Printf("Failed to parse URL: %v", err)
216
217 return
218 }
219
220 err = service.Initialize(serviceURL, log.New(io.Discard, "", 0))
221 if err != nil {
222 log.Printf("Failed to initialize service: %v", err)
223
224 return
225 }
226
227 // Access the service configuration fields after initialization
228 // The Config struct contains all parsed settings from the URL
229 fmt.Printf("Topic: %s\n", service.Config.Topic)
230 fmt.Printf("QoS: %d\n", service.Config.QoS)
231 fmt.Printf("Retained: %v\n", service.Config.Retained)
232 // Output:
233 // Topic: test/topic
234 // QoS: 2
235 // Retained: true
236 }