Skip to content

Commit

Permalink
chore: update tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
mloiseleur authored and Traefiker committed Nov 7, 2024
1 parent 66f06ad commit a264d8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 20 additions & 0 deletions src/api-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit a264d8a

Please sign in to comment.