client_test.go 1.8 KB

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