Files
sablier/plugins/traefik/pkg/strategy/blocking_strategy_test.go
Alexis Couvreur ad4e9ffb8c Add 'plugins/traefik/' from commit 'aef1f9e0dd205ea9cdea9e3ccf11900c5fe79b1f'
git-subtree-dir: plugins/traefik
git-subtree-mainline: 1a14070131
git-subtree-split: aef1f9e0dd
2022-09-30 14:32:09 +00:00

89 lines
2.2 KiB
Go

package strategy
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSingleBlockingStrategy_ServeHTTP(t *testing.T) {
for _, test := range SingleServiceTestCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(test.onDemandServiceResponses[0].status)
fmt.Fprint(w, test.onDemandServiceResponses[0].body)
}))
defer mockServer.Close()
blockingStrategy := &BlockingStrategy{
Name: "whoami",
Requests: []string{mockServer.URL},
Next: next,
BlockDelay: 1 * time.Second,
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://mydomain/whoami", nil)
blockingStrategy.ServeHTTP(recorder, req)
assert.Equal(t, test.expected.blocking, recorder.Code)
})
}
}
func TestMultipleBlockingStrategy_ServeHTTP(t *testing.T) {
for _, test := range MultipleServicesTestCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
urls := make([]string, len(test.onDemandServiceResponses))
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
for responseIndex, response := range test.onDemandServiceResponses {
response := response
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(response.status)
fmt.Fprint(w, response.body)
}))
defer mockServer.Close()
urls[responseIndex] = mockServer.URL
}
fmt.Println(urls)
blockingStrategy := &BlockingStrategy{
Name: "whoami",
Requests: urls,
Next: next,
BlockDelay: 1 * time.Second,
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://mydomain/whoami", nil)
blockingStrategy.ServeHTTP(recorder, req)
assert.Equal(t, test.expected.blocking, recorder.Code)
})
}
}