client_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package api
  2. import (
  3. "testing"
  4. "github.com/ollama/ollama/envconfig"
  5. )
  6. func TestClientFromEnvironment(t *testing.T) {
  7. type testCase struct {
  8. value string
  9. expect string
  10. err error
  11. }
  12. testCases := map[string]*testCase{
  13. "empty": {value: "", expect: "http://127.0.0.1:11434"},
  14. "only address": {value: "1.2.3.4", expect: "http://1.2.3.4:11434"},
  15. "only port": {value: ":1234", expect: "http://:1234"},
  16. "address and port": {value: "1.2.3.4:1234", expect: "http://1.2.3.4:1234"},
  17. "scheme http and address": {value: "http://1.2.3.4", expect: "http://1.2.3.4:80"},
  18. "scheme https and address": {value: "https://1.2.3.4", expect: "https://1.2.3.4:443"},
  19. "scheme, address, and port": {value: "https://1.2.3.4:1234", expect: "https://1.2.3.4:1234"},
  20. "hostname": {value: "example.com", expect: "http://example.com:11434"},
  21. "hostname and port": {value: "example.com:1234", expect: "http://example.com:1234"},
  22. "scheme http and hostname": {value: "http://example.com", expect: "http://example.com:80"},
  23. "scheme https and hostname": {value: "https://example.com", expect: "https://example.com:443"},
  24. "scheme, hostname, and port": {value: "https://example.com:1234", expect: "https://example.com:1234"},
  25. "trailing slash": {value: "example.com/", expect: "http://example.com:11434"},
  26. "trailing slash port": {value: "example.com:1234/", expect: "http://example.com:1234"},
  27. }
  28. for k, v := range testCases {
  29. t.Run(k, func(t *testing.T) {
  30. t.Setenv("OLLAMA_HOST", v.value)
  31. envconfig.LoadConfig()
  32. client, err := ClientFromEnvironment()
  33. if err != v.err {
  34. t.Fatalf("expected %s, got %s", v.err, err)
  35. }
  36. if client.base.String() != v.expect {
  37. t.Fatalf("expected %s, got %s", v.expect, client.base.String())
  38. }
  39. })
  40. }
  41. }