-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test so that we can develop locally
- Loading branch information
Ivaylo Korakov
committed
Apr 9, 2021
1 parent
212e664
commit 31db8bc
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |