From b021520512a0a8c5ecef2bec0a36854b4154ba00 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 6 Feb 2022 15:45:04 +0100 Subject: [PATCH 1/2] 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 b50422f..d7748d4 100644 --- a/main.go +++ b/main.go @@ -23,9 +23,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) { @@ -36,9 +37,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) } @@ -133,7 +139,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) { From c1ebd9c651fc842507b032efba7128b567f55b3e Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sun, 6 Feb 2022 15:45:48 +0100 Subject: [PATCH 2/2] Show slug instead of name for pipelines in index metadata --- main.go | 2 +- main_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d7748d4..254a4c4 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStor func IndexHandler(pipelines []pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { var pipelineNames []string for _, x := range pipelines { - pipelineNames = append(pipelineNames, x.GetName()) + pipelineNames = append(pipelineNames, x.GetSlug()) } w.Header().Set("Content-Type", "application/json") diff --git a/main_test.go b/main_test.go index 3286a51..1fb910a 100644 --- a/main_test.go +++ b/main_test.go @@ -44,7 +44,7 @@ func TestIndexRoute(t *testing.T) { 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]) + assert.Equal(t, data.Slug, body.Pipelines[0]) }) }