From f6bb5498e814053827a3cf2e37b200d5e1a2df3f Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 11:51:20 +0100 Subject: [PATCH 1/4] Add slug field to pipeline struct --- pipeline.go | 1 + pipeline_test.go | 3 ++- pipelines/example.json | 33 ++++++++++++++++++--------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pipeline.go b/pipeline.go index 0fd1ca3..4ea3e9a 100644 --- a/pipeline.go +++ b/pipeline.go @@ -27,6 +27,7 @@ type IPipeline interface { type Pipeline struct { Name string `json:"name"` + Slug string `json:"slug"` Type PipelineType `json:"type"` RemoveMetadata bool `json:"remove_metadata"` Steps []Step `json:"steps"` diff --git a/pipeline_test.go b/pipeline_test.go index 38c16e6..0c91123 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -1,8 +1,9 @@ package main import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestImagePipelineDeserialization(t *testing.T) { diff --git a/pipelines/example.json b/pipelines/example.json index bc42609..3cb6290 100644 --- a/pipelines/example.json +++ b/pipelines/example.json @@ -1,15 +1,18 @@ -{ - "name": "example pipeline", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] -} \ No newline at end of file +[ + { + "name": "example pipeline", + "slug": "example", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] + } +] \ No newline at end of file From f3a7433e1586da48bfa43f2abd1bd4d532862f3c Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 12:29:18 +0100 Subject: [PATCH 2/4] Register routes for pipelines --- main.go | 17 +++++++++++++++- main_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++ pipeline.go | 5 +++++ pipelines/example.json | 34 +++++++++++++++----------------- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index e3d8444..2574244 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,31 @@ type Metadata struct { Version string } +func PipelineHandle(pipeline IPipeline, w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(true) +} + func IndexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(Metadata{Name, Version}) } +func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) { + for _, pipeline := range pipelines { + r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { + PipelineHandle(pipeline, w, r) + }) + } +} + func main() { - LoadPipelines() + pipelines := LoadPipelines() r := mux.NewRouter() r.HandleFunc("/", IndexHandler) + RegisterPipelineRoutes(r, pipelines) + http.ListenAndServe(":8000", r) } diff --git a/main_test.go b/main_test.go index 513828f..64710c3 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -19,3 +20,46 @@ func TestIndexRoute(t *testing.T) { assert.NotNil(t, responseRecorder.Body, "Response should contain body") }) } + +func TestEndpointRoute(t *testing.T) { + // TODO: Use mock/fake for dummy pipeline data + givenEndpoint := Pipeline{ + Name: "example pipeline", + Slug: "example", + Type: 0, + RemoveMetadata: false, + Steps: []Step{ + { + Name: "resize image", + Type: 0, + }, + { + Name: "compress image", + Type: 1, + }, + }, + } + + t.Run("Registered pipelines are valid routes", func(t *testing.T) { + router := mux.NewRouter() + RegisterPipelineRoutes(router, []IPipeline{givenEndpoint}) + + request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 200) + }) + + t.Run("Unregistered pipelines return 404", func(t *testing.T) { + router := mux.NewRouter() + + request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 404) + }) +} diff --git a/pipeline.go b/pipeline.go index 4ea3e9a..c493b23 100644 --- a/pipeline.go +++ b/pipeline.go @@ -21,6 +21,7 @@ type PipelineType int type IPipeline interface { GetName() string + GetSlug() string GetType() PipelineType GetSteps() []Step } @@ -37,6 +38,10 @@ func (p Pipeline) GetName() string { return p.Name } +func (p Pipeline) GetSlug() string { + return p.Slug +} + func (p Pipeline) GetType() PipelineType { return p.Type } diff --git a/pipelines/example.json b/pipelines/example.json index 3cb6290..a8720a7 100644 --- a/pipelines/example.json +++ b/pipelines/example.json @@ -1,18 +1,16 @@ -[ - { - "name": "example pipeline", - "slug": "example", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] - } -] \ No newline at end of file +{ + "name": "example pipeline", + "slug": "example", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] +} \ No newline at end of file From bee20a159c93a072ea017b59bb7896fc09baf838 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 13:01:13 +0100 Subject: [PATCH 3/4] Return pipeline metadata in pipeline route --- main.go | 2 +- main_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 2574244..0f43e55 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ type Metadata struct { func PipelineHandle(pipeline IPipeline, w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(true) + json.NewEncoder(w).Encode(pipeline) } func IndexHandler(w http.ResponseWriter, r *http.Request) { diff --git a/main_test.go b/main_test.go index 64710c3..3540a04 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "net/http" "net/http/httptest" "testing" @@ -50,6 +51,8 @@ func TestEndpointRoute(t *testing.T) { router.ServeHTTP(responseRecorder, request) assert.Equal(t, responseRecorder.Code, 200) + body, _ := json.Marshal(givenEndpoint) + assert.JSONEq(t, string(body), responseRecorder.Body.String()) }) t.Run("Unregistered pipelines return 404", func(t *testing.T) { From e26544ddf550c7fcaf550af88eac5163fa8f2ee2 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 13:19:57 +0100 Subject: [PATCH 4/4] Fake pipeline object in tests --- go.mod | 1 + go.sum | 2 ++ main_test.go | 30 ++++++++++-------------------- pipeline.go | 12 ++++++------ 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 3641441..434e79d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require github.com/gorilla/mux v1.8.0 require ( + github.com/bxcodec/faker/v3 v3.7.0 // indirect github.com/davecgh/go-spew v1.1.0 // indirect github.com/google/go-cmp v0.5.6 // indirect github.com/hexops/valast v1.4.1 // indirect diff --git a/go.sum b/go.sum index 849fa9a..59db30e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/bxcodec/faker/v3 v3.7.0 h1:qWAFFwcyVS0ukF0UoJju1wBLO0cuPQ7JdVBPggM8kNo= +github.com/bxcodec/faker/v3 v3.7.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= diff --git a/main_test.go b/main_test.go index 3540a04..f887bc5 100644 --- a/main_test.go +++ b/main_test.go @@ -2,10 +2,12 @@ package main import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" + "github.com/bxcodec/faker/v3" "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -23,42 +25,30 @@ func TestIndexRoute(t *testing.T) { } func TestEndpointRoute(t *testing.T) { - // TODO: Use mock/fake for dummy pipeline data - givenEndpoint := Pipeline{ - Name: "example pipeline", - Slug: "example", - Type: 0, - RemoveMetadata: false, - Steps: []Step{ - { - Name: "resize image", - Type: 0, - }, - { - Name: "compress image", - Type: 1, - }, - }, + data := Pipeline{} + err := faker.FakeData(&data) + if err != nil { + fmt.Println(err) } t.Run("Registered pipelines are valid routes", func(t *testing.T) { router := mux.NewRouter() - RegisterPipelineRoutes(router, []IPipeline{givenEndpoint}) + RegisterPipelineRoutes(router, []IPipeline{data}) - request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + request, _ := http.NewRequest("GET", "/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() router.ServeHTTP(responseRecorder, request) assert.Equal(t, responseRecorder.Code, 200) - body, _ := json.Marshal(givenEndpoint) + body, _ := json.Marshal(data) assert.JSONEq(t, string(body), responseRecorder.Body.String()) }) t.Run("Unregistered pipelines return 404", func(t *testing.T) { router := mux.NewRouter() - request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + request, _ := http.NewRequest("GET", "/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() router.ServeHTTP(responseRecorder, request) diff --git a/pipeline.go b/pipeline.go index c493b23..56ecc4d 100644 --- a/pipeline.go +++ b/pipeline.go @@ -27,10 +27,10 @@ type IPipeline interface { } type Pipeline struct { - Name string `json:"name"` - Slug string `json:"slug"` - Type PipelineType `json:"type"` - RemoveMetadata bool `json:"remove_metadata"` + Name string `json:"name" faker:"name"` + Slug string `json:"slug" faker:"word"` + Type PipelineType `json:"type" faker:"-"` + RemoveMetadata bool `json:"remove_metadata" faker:"-"` Steps []Step `json:"steps"` } @@ -68,8 +68,8 @@ const ( ) type Step struct { - Name string - Type StepType + Name string `faker:"name"` + Type StepType `faker:"-"` } func (s Step) Translate() (IExecutableStep, error) {