From 71926e97131c3112c3ab3dbfe8cc552c9a5aa4ed Mon Sep 17 00:00:00 2001 From: gjahn <gregor.jahn@bht-berlin.de> Date: Wed, 25 Oct 2023 00:58:38 +0200 Subject: [PATCH] Add test route that dumps all server runtime environment variables if env not 'production' The three dots ('./...') are required to ensure recursive dependency installation, meaning dependencies of sub-packages. --- routing/routes.go | 34 +++++++++++++++++++++++--- routing/routes_test.go | 55 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/routing/routes.go b/routing/routes.go index 70ca5f9..8c43e4f 100644 --- a/routing/routes.go +++ b/routing/routes.go @@ -1,16 +1,44 @@ package routing import ( + "os" + "fmt" + "net/http" + + "webservice/configuration" + f "github.com/gofiber/fiber/v2" ) -func SetRoutes( router *f.App ){ +func SetRoutes( router *f.App, config *configuration.Config ){ + router.Get( "/", func( c *f.Ctx ) error { - return c.SendString( "Hello World!" ) + return c.SendString( "Hello, World!" ) }) + router.Get( "/env", func( c *f.Ctx ) error { + c.Type( "txt", "utf-8" ) + + if config.Environment == "production" { + c.Status( http.StatusForbidden ) + return nil + } + + for _, envVar := range os.Environ() { + _, err := c.WriteString( fmt.Sprintln( envVar ) ) + if err != nil { + c.Status( http.StatusInternalServerError ) + return err + } + } + c.Status( http.StatusOK ) + + return nil + }) + + router.Use( func( c *f.Ctx ) error { - return c.SendStatus( 418 ) + return c.SendStatus( http.StatusTeapot ) }) } diff --git a/routing/routes_test.go b/routing/routes_test.go index cb4b89d..385f49f 100644 --- a/routing/routes_test.go +++ b/routing/routes_test.go @@ -1,21 +1,34 @@ package routing import ( + "fmt" "io" + "os" + "time" + "math/rand" "testing" + "net/http" ht "net/http/httptest" f "github.com/gofiber/fiber/v2" "github.com/stretchr/testify/assert" + + "webservice/configuration" ) -func setup() *f.App { - router := f.New() +func setup() ( *f.App, *configuration.Config ){ + os.Setenv( "ENV_NAME", "testing" ) + config, _ := configuration.New() + + server := f.New( f.Config{ + AppName: "test", + DisableStartupMessage: false, + }) - SetRoutes( router ) + SetRoutes( server, config ) - return router + return server, config } @@ -30,13 +43,43 @@ func bodyToString( body *io.ReadCloser ) ( string, error ) { } +func generateRandomNumberString() string { + r := rand.New( rand.NewSource( time.Now().Unix() ) ) + randomNumber := r.Int63() + return fmt.Sprintf( "%d", randomNumber ) +} + + func TestIndexRoute( t *testing.T ){ - router := setup() + router, _ := setup() req := ht.NewRequest( "GET", "/", nil ) res, err := router.Test( req, -1 ) bodyContent, err := bodyToString( &res.Body ) assert.Nil( t, err ) - assert.Equal( t, "Hello World!", bodyContent ) + assert.Equal( t, "Hello, World!", bodyContent ) +} + + +func TestEnvRoute( t *testing.T ){ + router, config := setup() + + envVarName := "TEST_ENV_VAR" + envVarValue := generateRandomNumberString() + + os.Setenv( envVarName, envVarValue ) + + req := ht.NewRequest( "GET", "/env", nil ) + res, err := router.Test( req, -1 ) + bodyContent, err := bodyToString( &res.Body ) + assert.Equal( t, http.StatusOK, res.StatusCode ) + assert.Nil( t, err ) + assert.Contains( t, bodyContent, fmt.Sprintf( "%s=%s", envVarName, envVarValue ) ) + + ( *config ).Environment = "production" + + req = ht.NewRequest( "GET", "/env", nil ) + res, err = router.Test( req, -1 ) + assert.Equal( t, http.StatusForbidden, res.StatusCode ) } -- GitLab