diff --git a/pkg/fake/server.go b/pkg/fake/server.go new file mode 100644 index 0000000..8a0dcd3 --- /dev/null +++ b/pkg/fake/server.go @@ -0,0 +1,80 @@ +package fake + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/heathcliff26/fleetlock/pkg/api" + "github.com/stretchr/testify/assert" +) + +type FakeServer struct { + server *httptest.Server + assert *assert.Assertions + + Path string + StatusCode int + Group string + ID string +} + +func NewFakeServer(t *testing.T, statusCode int, path string) *FakeServer { + s := &FakeServer{ + assert: assert.New(t), + Path: path, + StatusCode: statusCode, + } + + s.server = httptest.NewServer(http.HandlerFunc(s.handleRequest)) + + return s +} + +func (s *FakeServer) handleRequest(rw http.ResponseWriter, req *http.Request) { + s.assert.Contains([]string{"/v1/pre-reboot", "/v1/steady-state"}, req.URL.String(), "Should request a valid url") + if s.Path != "" { + s.assert.Equal(s.Path, req.URL.String(), "Should use the specified URL") + } + + s.assert.Equal(http.MethodPost, req.Method, "Should be POST request") + s.assert.Equal("true", strings.ToLower(req.Header.Get("fleet-lock-protocol")), "fleet-lock-protocol header should be set") + + params, err := api.ParseRequest(req.Body) + s.assert.NoError(err, "Request should have the correct format") + + if s.Group != "" { + s.assert.Equal(s.Group, params.Client.Group, "Should have expected group") + } else { + s.assert.NotEmpty(params.Client.Group, "Should have group set") + } + if s.ID != "" { + s.assert.Equal(s.ID, params.Client.ID, "Should have expected id") + } else { + s.assert.NotEmpty(params.Client.ID, "Should have id set") + } + + rw.WriteHeader(s.StatusCode) + b, err := json.MarshalIndent(api.FleetLockResponse{ + Kind: "ok", + Value: "Success", + }, "", " ") + if !s.assert.NoError(err, "Error in fake server: failed to prepare response") { + return + } + + _, err = rw.Write(b) + s.assert.NoError(err, "Error in fake server: failed to send response") +} + +func (s *FakeServer) URL() string { + return s.server.URL +} + +func (s *FakeServer) Close() { + if s != nil && s.server != nil { + s.server.Close() + } +} diff --git a/pkg/server/client/client_test.go b/pkg/server/client/client_test.go index 2de87e5..f559625 100644 --- a/pkg/server/client/client_test.go +++ b/pkg/server/client/client_test.go @@ -1,13 +1,10 @@ package client import ( - "encoding/json" "net/http" - "net/http/httptest" - "strings" "testing" - "github.com/heathcliff26/fleetlock/pkg/api" + "github.com/heathcliff26/fleetlock/pkg/fake" "github.com/stretchr/testify/assert" ) @@ -131,35 +128,17 @@ func TestGetAndSet(t *testing.T) { }) } -func NewFakeServer(t *testing.T, statusCode int, path string) (*FleetlockClient, *httptest.Server) { - assert := assert.New(t) +func NewFakeServer(t *testing.T, statusCode int, path string) (*FleetlockClient, *fake.FakeServer) { + testGroup, testID := "testGroup", "testID" + + srv := fake.NewFakeServer(t, statusCode, path) + srv.Group = testGroup + srv.ID = testID - srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - assert.Equal(path, req.URL.String(), "Request use the correct request URL") - assert.Equal(http.MethodPost, req.Method, "Should be POST request") - assert.Equal("true", strings.ToLower(req.Header.Get("fleet-lock-protocol")), "fleet-lock-protocol header should be set") - - params, err := api.ParseRequest(req.Body) - assert.NoError(err, "Request should have the correct format") - assert.Equal("testGroup", params.Client.Group, "Should have Group set") - assert.Equal("testID", params.Client.ID, "Should have ID set") - - rw.WriteHeader(statusCode) - b, err := json.MarshalIndent(api.FleetLockResponse{ - Kind: "ok", - Value: "Success", - }, "", " ") - if !assert.NoError(err, "Error in fake server: failed to prepare response") { - return - } - - _, err = rw.Write(b) - assert.NoError(err, "Error in fake server: failed to send response") - })) c := &FleetlockClient{ - url: srv.URL, - group: "testGroup", - appID: "testID", + url: srv.URL(), + group: testGroup, + appID: testID, } return c, srv }