From a264d8af0e3002ae95224c1a4db6cf822c86487c Mon Sep 17 00:00:00 2001 From: mloiseleur Date: Thu, 7 Nov 2024 10:54:57 +0000 Subject: [PATCH] chore: update tutorials --- src/api-server/main.go | 5 +++++ src/api-server/main_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/api-server/main.go b/src/api-server/main.go index 1d7ce22..f173e54 100644 --- a/src/api-server/main.go +++ b/src/api-server/main.go @@ -143,6 +143,11 @@ func (a *api) errorRateMiddleWare() func(http.Handler) http.Handler { func (a *api) handleOpenAPISpec(rw http.ResponseWriter, req *http.Request) { var jsonObj interface{} + if a.openAPISpec == nil { + JSONError(rw, http.StatusNotImplemented, "No OpenAPISpec available") + return + } + err := yaml.Unmarshal(a.openAPISpec.Raw(), &jsonObj) if err != nil { JSONError(rw, http.StatusInternalServerError, err.Error()) diff --git a/src/api-server/main_test.go b/src/api-server/main_test.go index 6e56fb9..4529599 100644 --- a/src/api-server/main_test.go +++ b/src/api-server/main_test.go @@ -130,6 +130,26 @@ func Test_handleOpenAPISpec(t *testing.T) { assert.Contains(t, string(body), "openapi: 3.0.0") } +func Test_handleGetOpenAPISpecWithoutSpec(t *testing.T) { + a := api{} + + srv := httptest.NewServer(a.getRouter()) + + req, err := http.NewRequest(http.MethodGet, srv.URL+"/openapi.yaml", http.NoBody) + require.NoError(t, err) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer func() { _ = resp.Body.Close() }() + + assert.Equal(t, http.StatusNotImplemented, resp.StatusCode) + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Log(err) + } + require.NoError(t, err) + assert.Contains(t, string(body), "No OpenAPISpec available") +} + func Test_handleGetAll(t *testing.T) { a := api{}