|
| 1 | +package namedpipe |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/microsoft/go-mssqldb/msdsn" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParseServer(t *testing.T) { |
| 11 | + c := &msdsn.Config{ |
| 12 | + Port: 1000, |
| 13 | + } |
| 14 | + n := &namedPipeDialer{} |
| 15 | + err := n.ParseServer("server", c) |
| 16 | + assert.Errorf(t, err, "ParseServer with a Port") |
| 17 | + |
| 18 | + c = &msdsn.Config{ |
| 19 | + Parameters: make(map[string]string), |
| 20 | + ProtocolParameters: make(map[string]interface{}), |
| 21 | + } |
| 22 | + err = n.ParseServer(`\\.\pipe\MSSQL$Instance\sql\query`, c) |
| 23 | + assert.NoError(t, err, "ParseServer with a full pipe name") |
| 24 | + assert.Equal(t, "", c.Host, "Config Host with a full pipe name") |
| 25 | + data, ok := c.ProtocolParameters[n.Protocol()] |
| 26 | + assert.True(t, ok, "Should have added ProtocolParameters when server is pipe name") |
| 27 | + switch d := data.(type) { |
| 28 | + case namedPipeData: |
| 29 | + assert.Equal(t, `\\.\pipe\MSSQL$Instance\sql\query`, d.PipeName, "Pipe name in ProtocolParameters when server is pipe name") |
| 30 | + default: |
| 31 | + assert.Fail(t, "Incorrect protocol parameters type:", d) |
| 32 | + } |
| 33 | + |
| 34 | + c = &msdsn.Config{ |
| 35 | + Parameters: make(map[string]string), |
| 36 | + ProtocolParameters: make(map[string]interface{}), |
| 37 | + } |
| 38 | + err = n.ParseServer(`.\instance`, c) |
| 39 | + assert.NoError(t, err, "ParseServer .") |
| 40 | + assert.Equal(t, "localhost", c.Host, `Config Host with server == .\instance`) |
| 41 | + assert.Equal(t, "instance", c.Instance, `Config Instance with server == .\instance`) |
| 42 | + _, ok = c.ProtocolParameters[n.Protocol()] |
| 43 | + assert.Equal(t, ok, false, "Should have no namedPipeData when pipe name omitted") |
| 44 | + |
| 45 | + c = &msdsn.Config{ |
| 46 | + Host: "server", |
| 47 | + Parameters: make(map[string]string), |
| 48 | + ProtocolParameters: make(map[string]interface{}), |
| 49 | + } |
| 50 | + c.Parameters["pipe"] = `myinstance\sql\query` |
| 51 | + err = n.ParseServer(`anything`, c) |
| 52 | + assert.NoError(t, err, "ParseServer anything") |
| 53 | + data, ok = c.ProtocolParameters[n.Protocol()] |
| 54 | + assert.True(t, ok, "Should have added ProtocolParameters when pipe name is provided") |
| 55 | + switch d := data.(type) { |
| 56 | + case namedPipeData: |
| 57 | + assert.Equal(t, `\\server\pipe\myinstance\sql\query`, d.PipeName, "Pipe name in ProtocolParameters") |
| 58 | + default: |
| 59 | + assert.Fail(t, "Incorrect protocol parameters type:", d) |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments