client_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package api
  2. import (
  3. "net/http"
  4. "net/url"
  5. "testing"
  6. "github.com/ollama/ollama/envconfig"
  7. )
  8. func TestClientFromEnvironment(t *testing.T) {
  9. type testCase struct {
  10. value string
  11. expect string
  12. err error
  13. }
  14. testCases := map[string]*testCase{
  15. "empty": {value: "", expect: "http://127.0.0.1:11434"},
  16. "only address": {value: "1.2.3.4", expect: "http://1.2.3.4:11434"},
  17. "only port": {value: ":1234", expect: "http://:1234"},
  18. "address and port": {value: "1.2.3.4:1234", expect: "http://1.2.3.4:1234"},
  19. "scheme http and address": {value: "http://1.2.3.4", expect: "http://1.2.3.4:80"},
  20. "scheme https and address": {value: "https://1.2.3.4", expect: "https://1.2.3.4:443"},
  21. "scheme, address, and port": {value: "https://1.2.3.4:1234", expect: "https://1.2.3.4:1234"},
  22. "hostname": {value: "example.com", expect: "http://example.com:11434"},
  23. "hostname and port": {value: "example.com:1234", expect: "http://example.com:1234"},
  24. "scheme http and hostname": {value: "http://example.com", expect: "http://example.com:80"},
  25. "scheme https and hostname": {value: "https://example.com", expect: "https://example.com:443"},
  26. "scheme, hostname, and port": {value: "https://example.com:1234", expect: "https://example.com:1234"},
  27. "trailing slash": {value: "example.com/", expect: "http://example.com:11434"},
  28. "trailing slash port": {value: "example.com:1234/", expect: "http://example.com:1234"},
  29. }
  30. for k, v := range testCases {
  31. t.Run(k, func(t *testing.T) {
  32. t.Setenv("OLLAMA_HOST", v.value)
  33. envconfig.LoadConfig()
  34. client, err := ClientFromEnvironment()
  35. if err != v.err {
  36. t.Fatalf("expected %s, got %s", v.err, err)
  37. }
  38. if client.base.String() != v.expect {
  39. t.Fatalf("expected %s, got %s", v.expect, client.base.String())
  40. }
  41. })
  42. }
  43. }
  44. // Test function
  45. func TestIsLocal(t *testing.T) {
  46. type test struct {
  47. client *Client
  48. want bool
  49. err error
  50. }
  51. tests := map[string]test{
  52. "localhost": {
  53. client: func() *Client {
  54. baseURL, _ := url.Parse("http://localhost:1234")
  55. return &Client{base: baseURL, http: &http.Client{}}
  56. }(),
  57. want: true,
  58. err: nil,
  59. },
  60. "127.0.0.1": {
  61. client: func() *Client {
  62. baseURL, _ := url.Parse("http://127.0.0.1:1234")
  63. return &Client{base: baseURL, http: &http.Client{}}
  64. }(),
  65. want: true,
  66. err: nil,
  67. },
  68. "example.com": {
  69. client: func() *Client {
  70. baseURL, _ := url.Parse("http://example.com:1111")
  71. return &Client{base: baseURL, http: &http.Client{}}
  72. }(),
  73. want: false,
  74. err: nil,
  75. },
  76. "8.8.8.8": {
  77. client: func() *Client {
  78. baseURL, _ := url.Parse("http://8.8.8.8:1234")
  79. return &Client{base: baseURL, http: &http.Client{}}
  80. }(),
  81. want: false,
  82. err: nil,
  83. },
  84. "empty host with port": {
  85. client: func() *Client {
  86. baseURL, _ := url.Parse("http://:1234")
  87. return &Client{base: baseURL, http: &http.Client{}}
  88. }(),
  89. want: true,
  90. err: nil,
  91. },
  92. "empty host without port": {
  93. client: func() *Client {
  94. baseURL, _ := url.Parse("http://")
  95. return &Client{base: baseURL, http: &http.Client{}}
  96. }(),
  97. want: true,
  98. err: nil,
  99. },
  100. "remote host without port": {
  101. client: func() *Client {
  102. baseURL, _ := url.Parse("http://example.com")
  103. return &Client{base: baseURL, http: &http.Client{}}
  104. }(),
  105. want: false,
  106. err: nil,
  107. },
  108. }
  109. for name, tc := range tests {
  110. t.Run(name, func(t *testing.T) {
  111. got := tc.client.IsLocal()
  112. if got != tc.want {
  113. t.Errorf("test %s failed: got %v, want %v", name, got, tc.want)
  114. }
  115. })
  116. }
  117. }