refactor: update test, configuration deployment examples
This commit is contained in:
@@ -32,7 +32,7 @@ func TestMiddleware(t *testing.T) {
|
||||
middlewares := []Middleware{
|
||||
{
|
||||
Name: "basic-auth",
|
||||
Type: "basic",
|
||||
Type: BasicAuth,
|
||||
Paths: []string{"/", "/admin"},
|
||||
Rule: BasicRuleMiddleware{
|
||||
Username: "goma",
|
||||
@@ -41,7 +41,7 @@ func TestMiddleware(t *testing.T) {
|
||||
},
|
||||
{
|
||||
Name: "forbidden path access",
|
||||
Type: "access",
|
||||
Type: AccessMiddleware,
|
||||
Paths: []string{"/", "/admin"},
|
||||
Rule: BasicRuleMiddleware{
|
||||
Username: "goma",
|
||||
@@ -51,7 +51,7 @@ func TestMiddleware(t *testing.T) {
|
||||
|
||||
{
|
||||
Name: "jwt",
|
||||
Type: "jwt",
|
||||
Type: JWTAuth,
|
||||
Paths: []string{"/", "/admin"},
|
||||
Rule: JWTRuleMiddleware{
|
||||
URL: "https://www.googleapis.com/auth/userinfo.email",
|
||||
@@ -59,6 +59,35 @@ func TestMiddleware(t *testing.T) {
|
||||
Params: map[string]string{},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "oauth-google",
|
||||
Type: OAuth,
|
||||
Paths: []string{
|
||||
"/protected",
|
||||
"/example-of-oauth",
|
||||
},
|
||||
Rule: OauthRulerMiddleware{
|
||||
ClientID: "xxx",
|
||||
ClientSecret: "xxx",
|
||||
Provider: "google",
|
||||
JWTSecret: "your-strong-jwt-secret | It's optional",
|
||||
RedirectURL: "http://localhost:8080/callback",
|
||||
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email",
|
||||
"https://www.googleapis.com/auth/userinfo.profile"},
|
||||
Endpoint: OauthEndpoint{},
|
||||
State: "randomStateString",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "api-forbidden-paths",
|
||||
Type: AccessMiddleware,
|
||||
Paths: []string{
|
||||
"/swagger-ui/*",
|
||||
"/v2/swagger-ui/*",
|
||||
"/api-docs/*",
|
||||
"/actuator/*",
|
||||
},
|
||||
},
|
||||
}
|
||||
yamlData, err := yaml.Marshal(&middlewares)
|
||||
if err != nil {
|
||||
@@ -74,28 +103,43 @@ func TestMiddleware(t *testing.T) {
|
||||
func TestReadMiddleware(t *testing.T) {
|
||||
TestMiddleware(t)
|
||||
middlewares := getMiddlewares(t)
|
||||
middleware, err := getMiddleware(rules, middlewares)
|
||||
m, err := getMiddleware(rules, middlewares)
|
||||
if err != nil {
|
||||
t.Fatalf("Error searching middleware %s", err.Error())
|
||||
}
|
||||
switch middleware.Type {
|
||||
case "basic":
|
||||
log.Println("Basic auth")
|
||||
basicAuth, err := getBasicAuthMiddleware(middleware.Rule)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
log.Printf("Username: %s and password: %s\n", basicAuth.Username, basicAuth.Password)
|
||||
case "jwt":
|
||||
log.Println("JWT auth")
|
||||
jwt, err := getJWTMiddleware(middleware.Rule)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
log.Printf("JWT authentification URL is %s\n", jwt.URL)
|
||||
default:
|
||||
t.Errorf("Unknown middleware type %s", middleware.Type)
|
||||
log.Printf("Middleware: %v\n", m)
|
||||
|
||||
for _, middleware := range middlewares {
|
||||
|
||||
switch middleware.Type {
|
||||
case BasicAuth:
|
||||
log.Println("Basic auth")
|
||||
basicAuth, err := getBasicAuthMiddleware(middleware.Rule)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
log.Printf("Username: %s and password: %s\n", basicAuth.Username, basicAuth.Password)
|
||||
case JWTAuth:
|
||||
log.Println("JWT auth")
|
||||
jwt, err := getJWTMiddleware(middleware.Rule)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
log.Printf("JWT authentification URL is %s\n", jwt.URL)
|
||||
case OAuth:
|
||||
log.Println("OAuth auth")
|
||||
oauth, err := oAuthMiddleware(middleware.Rule)
|
||||
if err != nil {
|
||||
log.Fatalln("error:", err)
|
||||
}
|
||||
log.Printf("OAuth authentification: provider %s\n", oauth.Provider)
|
||||
case AccessMiddleware:
|
||||
log.Println("Access middleware")
|
||||
log.Printf("Access middleware: paths: [%s]\n", middleware.Paths)
|
||||
default:
|
||||
t.Errorf("Unknown middleware type %s", middleware.Type)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -19,6 +21,16 @@ func TestInit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckConfig(t *testing.T) {
|
||||
TestInit(t)
|
||||
initConfig(configFile)
|
||||
err := CheckConfig(configFile)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
log.Println("Goma Gateway configuration file checked successfully")
|
||||
}
|
||||
|
||||
func TestStart(t *testing.T) {
|
||||
TestInit(t)
|
||||
initConfig(configFile)
|
||||
@@ -28,7 +40,8 @@ func TestStart(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
route := gatewayServer.Initialize()
|
||||
route.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
route.HandleFunc("/test", func(rw http.ResponseWriter, r *http.Request) {
|
||||
_, err := rw.Write([]byte("Hello Goma Proxy"))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed writing HTTP response: %v", err)
|
||||
@@ -43,10 +56,19 @@ func TestStart(t *testing.T) {
|
||||
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
ctx := context.Background()
|
||||
go func() {
|
||||
err = gatewayServer.Start(ctx)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
t.Run("httpServer", func(t *testing.T) {
|
||||
s := httptest.NewServer(route)
|
||||
defer s.Close()
|
||||
assertResponseBody(t, s, "Hello Goma Proxy")
|
||||
})
|
||||
|
||||
ctx.Done()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user