diff --git a/main.go b/main.go index b50422f..254a4c4 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.GetSlug()) + } + 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..1fb910a 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.Slug, body.Pipelines[0]) + }) } func TestEndpointRoute(t *testing.T) {