diff --git a/README.md b/README.md index 0ef35c0..f8ff1e4 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,28 @@ os.Setenv("HTTP_PROXY", "http://proxy_name:proxy_port") Default configuration comes with `Servers` field that contains server objects as defined in the OpenAPI specification. +### Overriding the OSS Index URL + +You can override the default OSS Index server URL (https://ossindex.sonatype.org) using the `SetOSSIndexURL` method: + +```go +configuration := ossindex.NewConfiguration() +configuration.SetOSSIndexURL("https://custom.ossindex.example.com") +apiClient := ossindex.NewAPIClient(configuration) +``` + +This is useful for: +- Using a custom OSS Index instance +- Testing against a local or staging environment +- Routing requests through a proxy or gateway + +Example with localhost: +```go +configuration := ossindex.NewConfiguration() +configuration.SetOSSIndexURL("http://localhost:8080") +apiClient := ossindex.NewAPIClient(configuration) +``` + ### Select Server Configuration For using other server than the one defined on index 0 set context value `ossindex.ContextServerIndex` of type `int`. diff --git a/configuration.go b/configuration.go index b3d5d9e..291ef58 100644 --- a/configuration.go +++ b/configuration.go @@ -108,6 +108,16 @@ func (c *Configuration) AddDefaultHeader(key string, value string) { c.DefaultHeader[key] = value } +// SetOSSIndexURL allows users to override the default OSS Index server URL +func (c *Configuration) SetOSSIndexURL(url string) { + c.Servers = ServerConfigurations{ + { + URL: url, + Description: "Custom OSS Index server", + }, + } +} + // URL formats template on a index using given variables func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) { if index < 0 || len(sc) <= index { diff --git a/test/configuration_test.go b/test/configuration_test.go new file mode 100644 index 0000000..c9f35dd --- /dev/null +++ b/test/configuration_test.go @@ -0,0 +1,71 @@ +/* +Sonatype OSS Index + +Testing Configuration + +*/ + +package ossindex + +import ( + "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + ossindex "github.com/sonatype-nexus-community/ossindex-api-client-go" +) + +func TestNewConfiguration(t *testing.T) { + t.Run("Default configuration uses ossindex.sonatype.org", func(t *testing.T) { + cfg := ossindex.NewConfiguration() + + require.NotNil(t, cfg) + require.NotNil(t, cfg.Servers) + require.Len(t, cfg.Servers, 1) + assert.Equal(t, "https://ossindex.sonatype.org", cfg.Servers[0].URL) + }) +} + +func TestSetOSSIndexURL(t *testing.T) { + t.Run("SetOSSIndexURL overrides default URL", func(t *testing.T) { + cfg := ossindex.NewConfiguration() + customURL := "https://custom.ossindex.example.com" + + cfg.SetOSSIndexURL(customURL) + + require.NotNil(t, cfg.Servers) + require.Len(t, cfg.Servers, 1) + assert.Equal(t, customURL, cfg.Servers[0].URL) + assert.Equal(t, "Custom OSS Index server", cfg.Servers[0].Description) + }) + + t.Run("SetOSSIndexURL replaces existing server configuration", func(t *testing.T) { + cfg := ossindex.NewConfiguration() + firstURL := "https://first.example.com" + secondURL := "https://second.example.com" + + cfg.SetOSSIndexURL(firstURL) + assert.Equal(t, firstURL, cfg.Servers[0].URL) + + cfg.SetOSSIndexURL(secondURL) + require.Len(t, cfg.Servers, 1) + assert.Equal(t, secondURL, cfg.Servers[0].URL) + }) + + t.Run("SetOSSIndexURL works with localhost", func(t *testing.T) { + cfg := ossindex.NewConfiguration() + localURL := "http://localhost:8080" + + cfg.SetOSSIndexURL(localURL) + + assert.Equal(t, localURL, cfg.Servers[0].URL) + }) + + t.Run("SetOSSIndexURL works with IP addresses", func(t *testing.T) { + cfg := ossindex.NewConfiguration() + ipURL := "https://192.168.1.100:8443" + + cfg.SetOSSIndexURL(ipURL) + + assert.Equal(t, ipURL, cfg.Servers[0].URL) + }) +}