Skip to content

Commit

Permalink
Merge pull request #1536 from hyperledger/1535
Browse files Browse the repository at this point in the history
fix: set default namespace for apis from config
  • Loading branch information
peterbroadhurst authored Jul 15, 2024
2 parents c00ecf8 + 46cf5cd commit 07cc54a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type apiServer struct {
ffiSwaggerGen FFISwaggerGen
apiPublicURL string
dynamicPublicURLHeader string
defaultNamespace string
}

func InitConfig() {
Expand All @@ -76,6 +77,7 @@ func NewAPIServer() Server {
apiTimeout: config.GetDuration(coreconfig.APIRequestTimeout),
apiMaxTimeout: config.GetDuration(coreconfig.APIRequestMaxTimeout),
dynamicPublicURLHeader: config.GetString(coreconfig.APIDynamicPublicURLHeader),
defaultNamespace: config.GetString(coreconfig.NamespacesDefault),
metricsEnabled: config.GetBool(coreconfig.MetricsEnabled),
ffiSwaggerGen: &ffiSwaggerGen{},
}
Expand Down Expand Up @@ -188,7 +190,11 @@ func (as *apiServer) getBaseURL(req *http.Request) string {
vars := mux.Vars(req)
if ns, ok := vars["ns"]; ok && ns != "" {
baseURL += `/namespaces/` + ns
} else {
// Use the default namespace
baseURL += `/namespaces/` + as.defaultNamespace
}

return baseURL
}

Expand Down
26 changes: 26 additions & 0 deletions internal/apiserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,29 @@ func TestGetNamespacedWebSocketHandlerUnknownNamespace(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, 404, status)
}

func TestContractAPIDefaultNS(t *testing.T) {
mgr, o, as := newTestServer()
r := as.createMuxRouter(context.Background(), mgr)
mcm := &contractmocks.Manager{}
o.On("Contracts").Return(mcm)
mffi := apiservermocks.NewFFISwaggerGen(t)
as.ffiSwaggerGen = mffi
s := httptest.NewServer(r)
defer s.Close()

o.On("Authorize", mock.Anything, mock.Anything).Return(nil)

api := &core.ContractAPI{
Interface: &fftypes.FFIReference{
ID: fftypes.NewUUID(),
},
}

mcm.On("GetContractAPIs", mock.Anything, "http://127.0.0.1:5000/api/v1/namespaces/default", mock.Anything).Return([]*core.ContractAPI{api}, nil, nil)

res, err := resty.New().R().
Get(fmt.Sprintf("http://%s/api/v1/apis", s.Listener.Addr()))
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode())
}

0 comments on commit 07cc54a

Please sign in to comment.