Skip to content

Commit

Permalink
Put headers on responses from mocked endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
wcsanders1 committed Feb 3, 2019
1 parent 473a2e1 commit 7c770d1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (api *API) Start(dir, defaultCert, defaultKey string) error {
}

contextLoggerEndpoint.Debug("registered endpoint; now assigning handler")
api.handlers[method][registeredRoute] = api.creator.getHandler(endpoint.EnforceValidJSON, dir, file, api.file)
api.handlers[method][registeredRoute] = api.creator.getHandler(endpoint.EnforceValidJSON, endpoint.Headers, dir, file, api.file)
}

return api.creator.startAPI(defaultCert, defaultKey, api.server, api.httpConfig)
Expand Down
21 changes: 15 additions & 6 deletions api/api_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

type (
iCreator interface {
getHandler(enforceValidJSON bool, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request)
getHandler(enforceValidJSON bool, headers []config.Header, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request)
startAPI(defaultCert, defaultKey string, server wrapper.IServerOps, httpConfig config.HTTP) error
}

Expand All @@ -32,7 +32,7 @@ func newCreator(logger *logrus.Entry) *creator {
}
}

func (c creator) getHandler(enforceValidJSON bool, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request) {
func (c creator) getHandler(enforceValidJSON bool, headers []config.Header, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request) {
path := fmt.Sprintf("%s/%s/%s", constants.APIDir, dir, fileName)
contextLogger := c.log.WithFields(logrus.Fields{
log.FuncField: "handler for mock API",
Expand All @@ -41,13 +41,18 @@ func (c creator) getHandler(enforceValidJSON bool, dir, fileName string, file wr
})

if enforceValidJSON {
return getJSONHandler(path, file, contextLogger)
return getJSONHandler(path, headers, file, contextLogger)
}
return getGeneralHandler(path, file, contextLogger)
return getGeneralHandler(path, headers, file, contextLogger)
}

func getJSONHandler(path string, file wrapper.IFileOps, logger *logrus.Entry) func(w http.ResponseWriter, r *http.Request) {
func getJSONHandler(path string, headers []config.Header, file wrapper.IFileOps, logger *logrus.Entry) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {

for _, header := range headers {
w.Header().Set(header.Key, header.Value)
}

bytes, err := json.GetJSON(path, file)
if err != nil {
logger.WithError(err).Error("error serving JSON from this endpoint")
Expand All @@ -59,8 +64,12 @@ func getJSONHandler(path string, file wrapper.IFileOps, logger *logrus.Entry) fu
}
}

func getGeneralHandler(path string, file wrapper.IFileOps, logger *logrus.Entry) func(w http.ResponseWriter, r *http.Request) {
func getGeneralHandler(path string, headers []config.Header, file wrapper.IFileOps, logger *logrus.Entry) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
for _, header := range headers {
w.Header().Set(header.Key, header.Value)
}

fileInfo, err := file.Open(path)
defer fileInfo.Close()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/api_creator_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type fakeAPICreator struct {
mock.Mock
}

func (c *fakeAPICreator) getHandler(enforceValidJSON bool, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request) {
func (c *fakeAPICreator) getHandler(enforceValidJSON bool, headers []config.Header, dir, fileName string, file wrapper.IFileOps) func(w http.ResponseWriter, r *http.Request) {
args := c.Called(enforceValidJSON, dir, fileName, file)
return args.Get(0).(func(w http.ResponseWriter, r *http.Request))
}
Expand Down
18 changes: 9 additions & 9 deletions api/api_creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGetHandler_ReturnsHandler_WhenEnforceJSONFalse(t *testing.T) {
log: log.GetFakeLogger(),
}

result := creator.getHandler(false, "testDir", "testFile", &wrapper.FakeFileOps{})
result := creator.getHandler(false, nil, "testDir", "testFile", &wrapper.FakeFileOps{})

assert := assert.New(t)
assert.NotNil(result)
Expand All @@ -45,7 +45,7 @@ func TestGetHandler_ReturnsHandler_WhenEnforceJSONTrue(t *testing.T) {
log: log.GetFakeLogger(),
}

result := creator.getHandler(true, "testDir", "testFile", &wrapper.FakeFileOps{})
result := creator.getHandler(true, nil, "testDir", "testFile", &wrapper.FakeFileOps{})

assert := assert.New(t)
assert.NotNil(result)
Expand All @@ -55,7 +55,7 @@ func TestGetHandler_ReturnsHandler_WhenEnforceJSONTrue(t *testing.T) {
func TestGetJSONHandler_ReturnsHandler_WhenCalled(t *testing.T) {
fileOps := wrapper.FakeFileOps{}
logger := log.GetFakeLogger()
funcResult := getJSONHandler("test", &fileOps, logger)
funcResult := getJSONHandler("test", nil, &fileOps, logger)

assert.NotNil(t, funcResult)
}
Expand All @@ -66,7 +66,7 @@ func TestJSONHandler_Writes_OnSuccess(t *testing.T) {
logger := log.GetFakeLogger()
fileOps.On("Open", mock.AnythingOfType("string")).Return(os.NewFile(1, "fakefile"), nil)
fileOps.On("ReadAll", mock.AnythingOfType("*os.File")).Return(goodJSON, nil)
funcResult := getJSONHandler(path, &fileOps, logger)
funcResult := getJSONHandler(path, nil, &fileOps, logger)
w := fake.ResponseWriter{}
w.On("WriteHeader", mock.AnythingOfType("int")).Return(1)
w.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
Expand All @@ -85,7 +85,7 @@ func TestJSONHandler_WritesError_OnFailure(t *testing.T) {
logger := log.GetFakeLogger()
fileOps.On("Open", mock.AnythingOfType("string")).Return(os.NewFile(1, "fakefile"), nil)
fileOps.On("ReadAll", mock.AnythingOfType("*os.File")).Return([]byte{}, errors.New(""))
funcResult := getJSONHandler(path, &fileOps, logger)
funcResult := getJSONHandler(path, nil, &fileOps, logger)
w := fake.ResponseWriter{}
w.On("WriteHeader", mock.AnythingOfType("int")).Return(1)
w.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
Expand All @@ -102,7 +102,7 @@ func TestJSONHandler_WritesError_OnFailure(t *testing.T) {
func TestGetGeneralHanlder_ReturnsFunc_WhenCalled(t *testing.T) {
fileOps := wrapper.FakeFileOps{}
logger := log.GetFakeLogger()
funcResult := getGeneralHandler("test", &fileOps, logger)
funcResult := getGeneralHandler("test", nil, &fileOps, logger)

assert.NotNil(t, funcResult)
}
Expand All @@ -113,7 +113,7 @@ func TestGeneralHandler_Writes_OnSuccess(t *testing.T) {
logger := log.GetFakeLogger()
fileOps.On("Open", mock.AnythingOfType("string")).Return(os.NewFile(1, "fakefile"), nil)
fileOps.On("ReadAll", mock.AnythingOfType("*os.File")).Return(goodJSON, nil)
funcResult := getGeneralHandler(path, &fileOps, logger)
funcResult := getGeneralHandler(path, nil, &fileOps, logger)
w := fake.ResponseWriter{}
w.On("WriteHeader", mock.AnythingOfType("int")).Return(1)
w.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
Expand All @@ -132,7 +132,7 @@ func TestGeneralHandler_WritesError_WhenReadFails(t *testing.T) {
logger := log.GetFakeLogger()
fileOps.On("Open", mock.AnythingOfType("string")).Return(os.NewFile(1, "fakefile"), nil)
fileOps.On("ReadAll", mock.AnythingOfType("*os.File")).Return([]byte{}, errors.New(""))
funcResult := getGeneralHandler(path, &fileOps, logger)
funcResult := getGeneralHandler(path, nil, &fileOps, logger)
w := fake.ResponseWriter{}
w.On("WriteHeader", mock.AnythingOfType("int")).Return(1)
w.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
Expand All @@ -151,7 +151,7 @@ func TestGeneralHandler_WritesError_WhenFileOpenFails(t *testing.T) {
fileOps := wrapper.FakeFileOps{}
logger := log.GetFakeLogger()
fileOps.On("Open", mock.AnythingOfType("string")).Return(os.NewFile(1, "fakefile"), errors.New(""))
funcResult := getGeneralHandler(path, &fileOps, logger)
funcResult := getGeneralHandler(path, nil, &fileOps, logger)
w := fake.ResponseWriter{}
w.On("WriteHeader", mock.AnythingOfType("int")).Return(1)
w.On("Write", mock.AnythingOfType("[]uint8")).Return(1, nil)
Expand Down

0 comments on commit 7c770d1

Please sign in to comment.