Files
sablier/main_test.go
Alexis Couvreur e357030b8c fix: use text/plain content-type instead of application/json
The returned string is not a valid json
2021-09-26 14:32:33 +00:00

74 lines
1.7 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/docker/docker/client"
)
type ScalerMock struct {
isUp bool
}
func (s ScalerMock) IsUp(client *client.Client, name string) bool {
return s.isUp
}
func (ScalerMock) ScaleUp(client *client.Client, name string, replicas *uint64) {}
func (ScalerMock) ScaleDown(client *client.Client, name string) {}
func TestOndemand_ServeHTTP(t *testing.T) {
testCases := []struct {
desc string
scaler ScalerMock
status string
statusCode int
contentType string
}{
{
desc: "service is starting",
status: "starting",
scaler: ScalerMock{isUp: false},
statusCode: http.StatusAccepted,
contentType: "text/plain",
},
{
desc: "service is started",
status: "started",
scaler: ScalerMock{isUp: true},
statusCode: http.StatusCreated,
contentType: "text/plain",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Logf("IsUp: %t", test.scaler.isUp)
request := httptest.NewRequest("GET", "/?name=whoami&timeout=5m", nil)
responseRecorder := httptest.NewRecorder()
onDemandHandler := onDemand(nil, test.scaler)
onDemandHandler(responseRecorder, request)
body := responseRecorder.Body.String()
if responseRecorder.Code != test.statusCode {
t.Errorf("Want status '%d', got '%d'", test.statusCode, responseRecorder.Code)
}
if responseRecorder.Body.String() != test.status {
t.Errorf("Want body '%s', got '%s'", test.status, body)
}
if responseRecorder.Header().Get("Content-Type") != test.contentType {
t.Errorf("Want content type '%s', got '%s'", test.contentType, responseRecorder.Header().Get("Content-Type"))
}
})
}
}