From 7317a215f9044b73ea9d2da2ec214d857e0ea3aa Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 27 Dec 2021 00:01:40 +0100 Subject: [PATCH 1/3] Add metadata response to index route --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a12eff1..5057f09 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,23 @@ package main import ( + "encoding/json" "net/http" "github.com/gorilla/mux" ) +const Name string = "Lithium" +const Version string = "0.1.0" + +type Metadata struct { + Name string + Version string +} + func IndexHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("hello world!")) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(Metadata{Name, Version}) } func main() { From a88766080fc64fd5ede6d3f6833156c82cc4b0b0 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Mon, 27 Dec 2021 00:12:33 +0100 Subject: [PATCH 2/3] Add test for index route --- go.mod | 7 +++++++ go.sum | 10 ++++++++++ main_test.go | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 main_test.go diff --git a/go.mod b/go.mod index e8488cb..7829019 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,10 @@ module github.com/geplauder/lithium go 1.17 require github.com/gorilla/mux v1.8.0 + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum index 5350288..e2ff782 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ +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/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..59a0cc1 --- /dev/null +++ b/main_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIndexRoute(t *testing.T) { + t.Run("test", func(t *testing.T) { + request := httptest.NewRequest(http.MethodGet, "/", nil) + responseRecorder := httptest.NewRecorder() + + IndexHandler(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 200, "Response code should be 200") + assert.NotNil(t, responseRecorder.Body, "Response should contain body") + }) +} From 35a4e68ad6f3142c5bbb7f9ec4671e3514f3f64a Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 18:24:44 +0100 Subject: [PATCH 3/3] Use proper test name for index route test --- main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index 59a0cc1..513828f 100644 --- a/main_test.go +++ b/main_test.go @@ -9,7 +9,7 @@ import ( ) func TestIndexRoute(t *testing.T) { - t.Run("test", func(t *testing.T) { + t.Run("Index route returns valid response", func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "/", nil) responseRecorder := httptest.NewRecorder()