本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.0KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_parsePostgreSQLHostPort(t *testing.T) {
  10. tests := []struct {
  11. HostPort string
  12. Host string
  13. Port string
  14. }{
  15. {
  16. HostPort: "127.0.0.1:1234",
  17. Host: "127.0.0.1",
  18. Port: "1234",
  19. },
  20. {
  21. HostPort: "127.0.0.1",
  22. Host: "127.0.0.1",
  23. Port: "5432",
  24. },
  25. {
  26. HostPort: "[::1]:1234",
  27. Host: "[::1]",
  28. Port: "1234",
  29. },
  30. {
  31. HostPort: "[::1]",
  32. Host: "[::1]",
  33. Port: "5432",
  34. },
  35. {
  36. HostPort: "/tmp/pg.sock:1234",
  37. Host: "/tmp/pg.sock",
  38. Port: "1234",
  39. },
  40. {
  41. HostPort: "/tmp/pg.sock",
  42. Host: "/tmp/pg.sock",
  43. Port: "5432",
  44. },
  45. }
  46. for _, test := range tests {
  47. host, port := parsePostgreSQLHostPort(test.HostPort)
  48. assert.Equal(t, test.Host, host)
  49. assert.Equal(t, test.Port, port)
  50. }
  51. }
  52. func Test_getPostgreSQLConnectionString(t *testing.T) {
  53. tests := []struct {
  54. Host string
  55. Port string
  56. User string
  57. Passwd string
  58. Name string
  59. Param string
  60. SSLMode string
  61. Output string
  62. }{
  63. {
  64. Host: "/tmp/pg.sock",
  65. Port: "4321",
  66. User: "testuser",
  67. Passwd: "space space !#$%^^%^```-=?=",
  68. Name: "gitea",
  69. Param: "",
  70. SSLMode: "false",
  71. Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/giteasslmode=false&host=/tmp/pg.sock",
  72. },
  73. {
  74. Host: "localhost",
  75. Port: "1234",
  76. User: "pgsqlusername",
  77. Passwd: "I love Gitea!",
  78. Name: "gitea",
  79. Param: "",
  80. SSLMode: "true",
  81. Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/giteasslmode=true",
  82. },
  83. }
  84. for _, test := range tests {
  85. connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
  86. assert.Equal(t, test.Output, connStr)
  87. }
  88. }
上海开阖软件有限公司 沪ICP备12045867号-1