MCPcopy
hub / github.com/elastic/go-elasticsearch / TestAddrsToURLs

Function TestAddrsToURLs

elasticsearch_internal_test.go:258–335  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

256}
257
258func TestAddrsToURLs(t *testing.T) {
259 tt := []struct {
260 name string
261 addrs []string
262 urls []*url.URL
263 err error
264 }{
265 {
266 name: "valid",
267 addrs: []string{
268 "http://example.com",
269 "https://example.com",
270 "http://192.168.255.255",
271 "http://example.com:8080",
272 },
273 urls: []*url.URL{
274 {Scheme: "http", Host: "example.com"},
275 {Scheme: "https", Host: "example.com"},
276 {Scheme: "http", Host: "192.168.255.255"},
277 {Scheme: "http", Host: "example.com:8080"},
278 },
279 err: nil,
280 },
281 {
282 name: "trim trailing slash",
283 addrs: []string{"http://example.com/", "http://example.com//"},
284 urls: []*url.URL{
285 {Scheme: "http", Host: "example.com", Path: ""},
286 {Scheme: "http", Host: "example.com", Path: ""},
287 },
288 },
289 {
290 name: "keep suffix",
291 addrs: []string{"http://example.com/foo"},
292 urls: []*url.URL{{Scheme: "http", Host: "example.com", Path: "/foo"}},
293 },
294 {
295 name: "invalid url",
296 addrs: []string{"://invalid.com"},
297 urls: nil,
298 err: errors.New("missing protocol scheme"),
299 },
300 }
301 for _, tc := range tt {
302 t.Run(tc.name, func(t *testing.T) {
303 res, err := addrsToURLs(tc.addrs)
304
305 if tc.err != nil {
306 if err == nil {
307 t.Errorf("Expected error, got: %v", err)
308 }
309 match, matchErr := regexp.MatchString(tc.err.Error(), err.Error())
310 if matchErr != nil {
311 t.Fatalf("Unexpected error: %s", matchErr)
312 }
313 if !match {
314 t.Errorf("Expected err [%s] to match: %s", matchErr.Error(), tc.err.Error())
315 }

Callers

nothing calls this directly

Calls 3

addrsToURLsFunction · 0.85
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected