Use CheckConfig, DefaultClient
package middlewares
import (
+ "bytes"
+ "encoding/json"
"math/rand"
"net/http"
"net/http/httptest"
- "strings"
"testing"
+ "github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
)
@@ -15,19 +17,19 @@ func TestMiddleWareLimitRequest(t *testing.T) {
server := httptest.NewServer(limit.Then(testHandler()))
defer server.Close()
- client := &http.Client{}
- checkBody := strings.NewReader(`{
- "Command": "true",
- "Environment": "default",
- "Interval": 30,
- "Name": "checktest",
- "Organization": "default",
- "Publish": true,
- "Subscriptions": []string{"system"}
- }`)
-
- req, _ := http.NewRequest(http.MethodPost, server.URL+"/checks", checkBody)
- res, err := client.Do(req)
+ check := &types.CheckConfig{
+ Command: "true",
+ Environment: "default",
+ Interval: 30,
+ Name: "checktest",
+ Organization: "default",
+ Publish: true,
+ Subscriptions: []string{"system"},
+ }
+
+ payload, _ := json.Marshal(check)
+ req, _ := http.NewRequest(http.MethodPost, server.URL+"/checks", bytes.NewBuffer(payload))
+ res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}
@@ -37,21 +39,21 @@ func TestMiddleWareInvalidLimitRequest(t *testing.T) {
server := httptest.NewServer(limit.Then(testHandler()))
defer server.Close()
- client := &http.Client{}
maxCheck := make([]byte, 600000)
rand.Read(maxCheck)
- checkBody := strings.NewReader(`{
- "Command": ` + string(maxCheck) + `,
- "Environment": "default",
- "Interval": 30,
- "Name": "checktest",
- "Organization": "default",
- "Publish": true,
- "Subscriptions": []string{"system"}
- }`)
-
- req, _ := http.NewRequest(http.MethodPost, server.URL+"/checks", checkBody)
- res, err := client.Do(req)
+ check := &types.CheckConfig{
+ Command: string(maxCheck),
+ Environment: "default",
+ Interval: 30,
+ Name: "checktest",
+ Organization: "default",
+ Publish: true,
+ Subscriptions: []string{"system"},
+ }
+
+ payload, _ := json.Marshal(check)
+ req, _ := http.NewRequest(http.MethodPost, server.URL+"/checks", bytes.NewBuffer(payload))
+ res, err := http.DefaultClient.Do(req)
assert.Error(t, err)
assert.Equal(t, http.StatusInternalServerError, res.StatusCode)
}
