Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 10 additions & 0 deletions configuration.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions test/configuration_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}