traefik/pkg/middlewares/emptybackendhandler/empty_backend_handler_test.go

82 lines
1.8 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package emptybackendhandler
2017-07-10 10:11:44 +00:00
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/testhelpers"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/assert"
2017-07-10 10:11:44 +00:00
"github.com/vulcand/oxy/roundrobin"
)
func TestEmptyBackendHandler(t *testing.T) {
2018-11-14 09:18:03 +00:00
testCases := []struct {
amountServer int
expectedStatusCode int
2017-07-10 10:11:44 +00:00
}{
{
2018-11-14 09:18:03 +00:00
amountServer: 0,
expectedStatusCode: http.StatusServiceUnavailable,
2017-07-10 10:11:44 +00:00
},
{
2018-11-14 09:18:03 +00:00
amountServer: 1,
expectedStatusCode: http.StatusOK,
2017-07-10 10:11:44 +00:00
},
}
2018-11-14 09:18:03 +00:00
for _, test := range testCases {
2017-07-10 10:11:44 +00:00
test := test
t.Run(fmt.Sprintf("amount servers %d", test.amountServer), func(t *testing.T) {
t.Parallel()
2018-11-14 09:18:03 +00:00
handler := New(&healthCheckLoadBalancer{amountServer: test.amountServer})
2017-07-10 10:11:44 +00:00
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
handler.ServeHTTP(recorder, req)
2018-11-14 09:18:03 +00:00
assert.Equal(t, test.expectedStatusCode, recorder.Result().StatusCode)
2017-07-10 10:11:44 +00:00
})
}
}
type healthCheckLoadBalancer struct {
amountServer int
}
2018-06-11 09:36:03 +00:00
func (lb *healthCheckLoadBalancer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
2017-07-10 10:11:44 +00:00
}
func (lb *healthCheckLoadBalancer) Servers() []*url.URL {
servers := make([]*url.URL, lb.amountServer)
for i := 0; i < lb.amountServer; i++ {
servers = append(servers, testhelpers.MustParseURL("http://localhost"))
}
return servers
}
2018-06-11 09:36:03 +00:00
func (lb *healthCheckLoadBalancer) RemoveServer(u *url.URL) error {
return nil
}
func (lb *healthCheckLoadBalancer) UpsertServer(u *url.URL, options ...roundrobin.ServerOption) error {
return nil
}
func (lb *healthCheckLoadBalancer) ServerWeight(u *url.URL) (int, bool) {
return 0, false
}
func (lb *healthCheckLoadBalancer) NextServer() (*url.URL, error) {
return nil, nil
}
func (lb *healthCheckLoadBalancer) Next() http.Handler {
return nil
}