Skip to content

Commit

Permalink
Added test so that we can develop locally
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivaylo Korakov committed Apr 9, 2021
1 parent 212e664 commit 31db8bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
13 changes: 10 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package traefik_routing_plugin
import (
"context"
"fmt"
"log"
"net/http"
)

Expand All @@ -19,19 +20,25 @@ type Router struct {
// Uses a generic http.Handler type from golang that we can use to work with the request
// by overriding different functions of the interface
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
log.Println("NEW FUNCTION CALLED")

if len(config.Routes) == 0 {
return nil, fmt.Errorf("routes cannot be empty")
}

return &Router{
routes: config.Routes,
next: next,
name: name,
routes: config.Routes,
next: next,
name: name,
}, nil
}

func (a *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.Println("SERVE HTTP")
log.Println(req.URL)

for key, value := range a.routes {
log.Println(fmt.Sprintf("Setting header: %s : %s", key, value))
req.Header.Set(key, value)
}

Expand Down
57 changes: 57 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package traefik_routing_plugin_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/kumina/traefik-routing-plugin"
)

func TestRouter(t *testing.T) {
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})

cfg := traefik_routing_plugin.CreateConfig()
cfg.Routes["Route-To-Service"] = "Dummy"
cfg.Routes["Route-To-Service-Test"] = "DummyTest"
cfg.Routes["Route-To-Service-Lambda"] = "Lambda"

handler, err := traefik_routing_plugin.New(ctx, next, cfg, "traefik-routing-plugin")
if err != nil {
t.Fatal(err)
}

recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(recorder, req)

assertHeader(t, req, "Route-To-Service", "Dummy")
assertHeader(t, req, "Route-To-Service-Test", "DummyTest")
assertHeader(t, req, "Route-To-Service-Lambda", "Lambda")

recorder = httptest.NewRecorder()
req, err = http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost/asd", nil)
if err != nil {
t.Fatal(err)
}

handler.ServeHTTP(recorder, req)

assertHeader(t, req, "Route-To-Service", "Dummy")
assertHeader(t, req, "Route-To-Service-Test", "DummyTest")
assertHeader(t, req, "Route-To-Service-Lambda", "Lambda")
}

func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()

if req.Header.Get(key) != expected {
t.Errorf("invalid header value: %s", req.Header.Get(key))
}
}

0 comments on commit 31db8bc

Please sign in to comment.