Browse Source

Merge commit 'df92fc18178737d0eb5fa9ee04c86a6c567c2d1b' into HEAD

feature/update-route-registration
Jenkins 3 years ago
committed by Fabian Vowie
parent
commit
b139b6556d
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 20
      main.go
  2. 21
      main_test.go

20
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) {

21
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) {

Loading…
Cancel
Save