client_test.go 1.8 KB

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