client_test.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package api
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  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. client, err := ClientFromEnvironment()
  34. if err != v.err {
  35. t.Fatalf("expected %s, got %s", v.err, err)
  36. }
  37. if client.base.String() != v.expect {
  38. t.Fatalf("expected %s, got %s", v.expect, client.base.String())
  39. }
  40. })
  41. }
  42. hostTestCases := map[string]*testCase{
  43. "empty": {value: "", expect: "127.0.0.1:11434"},
  44. "only address": {value: "1.2.3.4", expect: "1.2.3.4:11434"},
  45. "only port": {value: ":1234", expect: ":1234"},
  46. "address and port": {value: "1.2.3.4:1234", expect: "1.2.3.4:1234"},
  47. "hostname": {value: "example.com", expect: "example.com:11434"},
  48. "hostname and port": {value: "example.com:1234", expect: "example.com:1234"},
  49. "zero port": {value: ":0", expect: ":0"},
  50. "too large port": {value: ":66000", err: ErrInvalidHostPort},
  51. "too small port": {value: ":-1", err: ErrInvalidHostPort},
  52. "ipv6 localhost": {value: "[::1]", expect: "[::1]:11434"},
  53. "ipv6 world open": {value: "[::]", expect: "[::]:11434"},
  54. "ipv6 no brackets": {value: "::1", expect: "[::1]:11434"},
  55. "ipv6 + port": {value: "[::1]:1337", expect: "[::1]:1337"},
  56. "extra space": {value: " 1.2.3.4 ", expect: "1.2.3.4:11434"},
  57. "extra quotes": {value: "\"1.2.3.4\"", expect: "1.2.3.4:11434"},
  58. "extra space+quotes": {value: " \" 1.2.3.4 \" ", expect: "1.2.3.4:11434"},
  59. "extra single quotes": {value: "'1.2.3.4'", expect: "1.2.3.4:11434"},
  60. }
  61. for k, v := range hostTestCases {
  62. t.Run(k, func(t *testing.T) {
  63. t.Setenv("OLLAMA_HOST", v.value)
  64. oh, err := GetOllamaHost()
  65. if err != v.err {
  66. t.Fatalf("expected %s, got %s", v.err, err)
  67. }
  68. if err == nil {
  69. host := net.JoinHostPort(oh.Host, oh.Port)
  70. assert.Equal(t, v.expect, host, fmt.Sprintf("%s: expected %s, got %s", k, v.expect, host))
  71. }
  72. })
  73. }
  74. }