Skip to content

Commit 687ab56

Browse files
committed
dsn: Add tests for dsn.Clone
1 parent d58236e commit 687ab56

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

dsn_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,46 @@ func TestParamsAreSorted(t *testing.T) {
318318
}
319319
}
320320

321+
func TestCloneConfig(t *testing.T) {
322+
RegisterServerPubKey("testKey", testPubKeyRSA)
323+
defer DeregisterServerPubKey("testKey")
324+
325+
expectedServerName := "example.com"
326+
dsn := "tcp(example.com:1234)/?tls=true&foobar=baz&serverPubKey=testKey"
327+
cfg, err := ParseDSN(dsn)
328+
if err != nil {
329+
t.Fatal(err.Error())
330+
}
331+
332+
cfg2 := cfg.Clone()
333+
if cfg == cfg2 {
334+
t.Errorf("Config.Clone did not create a separate config struct")
335+
}
336+
337+
if cfg2.tls.ServerName != expectedServerName {
338+
t.Errorf("cfg.tls.ServerName should be %q, got %q (host with port)", expectedServerName, cfg.tls.ServerName)
339+
}
340+
341+
cfg2.tls.ServerName = "example2.com"
342+
if cfg.tls.ServerName == cfg2.tls.ServerName {
343+
t.Errorf("changed cfg.tls.Server name should not propagate to original Config")
344+
}
345+
346+
if _, ok := cfg2.Params["foobar"]; !ok {
347+
t.Errorf("cloned Config is missing custom params")
348+
}
349+
350+
delete(cfg2.Params, "foobar")
351+
352+
if _, ok := cfg.Params["foobar"]; !ok {
353+
t.Errorf("custom params in cloned Config should not propagate to original Config")
354+
}
355+
356+
if !reflect.DeepEqual(cfg.pubKey, cfg2.pubKey) {
357+
t.Errorf("public key in Config should be identical")
358+
}
359+
}
360+
321361
func BenchmarkParseDSN(b *testing.B) {
322362
b.ReportAllocs()
323363

0 commit comments

Comments
 (0)