From 63ac199ee68712cf1c2ea9f1969038de6b6b7d7e Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 6 Feb 2022 15:45:04 +0100 Subject: [PATCH] Add pipelines list to index route metadata --- main.go | 20 ++++++++++++++------ main_test.go | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 06607ed..c677469 100644 --- a/main.go +++ b/main.go @@ -22,9 +22,10 @@ const Version string = "0.1.0" var GitCommit string type Metadata struct { - Name string `json:"name"` - Version string `json:"version"` - CommitHash string `json:"commit_hash"` + Name string `json:"name"` + Version string `json:"version"` + CommitHash string `json:"commit_hash"` + Pipelines []string `json:"pipelines"` } func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) { @@ -35,9 +36,14 @@ func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStor } } -func IndexHandler(w http.ResponseWriter, r *http.Request) { +func IndexHandler(pipelines []pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { + var pipelineNames []string + for _, x := range pipelines { + pipelineNames = append(pipelineNames, x.GetName()) + } + w.Header().Set("Content-Type", "application/json") - err := json.NewEncoder(w).Encode(Metadata{Name, Version, GitCommit}) + err := json.NewEncoder(w).Encode(Metadata{Name, Version, GitCommit, pipelineNames}) if err != nil { w.WriteHeader(http.StatusInternalServerError) } @@ -132,7 +138,9 @@ func UploadHandler(w http.ResponseWriter, r *http.Request, pipes []pipelines.IPi func RegisterRoutes(r *mux.Router, appSettings settings.Settings, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) { index := r.Methods(http.MethodGet).Subrouter() - index.HandleFunc("/", IndexHandler) + index.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + IndexHandler(pipelines, w, r) + }) upload := r.Methods(http.MethodPost).Subrouter() upload.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { diff --git a/main_test.go b/main_test.go index e1c8a74..3286a51 100644 --- a/main_test.go +++ b/main_test.go @@ -18,15 +18,34 @@ import ( ) func TestIndexRoute(t *testing.T) { + data := pipelines.Pipeline{} + err := faker.FakeData(&data) + assert.Nil(t, err) + t.Run("Index route returns valid response", func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "/", nil) responseRecorder := httptest.NewRecorder() - IndexHandler(responseRecorder, request) + IndexHandler([]pipelines.IPipeline{}, responseRecorder, request) assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200") assert.NotNil(t, responseRecorder.Body, "Response should contain body") }) + + t.Run("Index route returns valid amount of pipelines", func(t *testing.T) { + request := httptest.NewRequest(http.MethodGet, "/", nil) + responseRecorder := httptest.NewRecorder() + + IndexHandler([]pipelines.IPipeline{data}, responseRecorder, request) + + assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200") + + var body = Metadata{} + err = json.Unmarshal(responseRecorder.Body.Bytes(), &body) + assert.Nil(t, err) + assert.Equal(t, 1, len(body.Pipelines)) + assert.Equal(t, data.Name, body.Pipelines[0]) + }) } func TestEndpointRoute(t *testing.T) {