Skip to content

Commit

Permalink
Fix bug causing request to fail if no info configured to be returned
Browse files Browse the repository at this point in the history
  • Loading branch information
wcsanders1 committed Feb 23, 2019
1 parent 53e8d76 commit a1b7bf7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 15 additions & 1 deletion api/api_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ func newCreator(logger *logrus.Entry) *creator {
}

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)
var path string
if len(fileName) > 0 {
path = fmt.Sprintf("%s/%s/%s", constants.APIDir, dir, fileName)
} else {
path = ""
}

contextLogger := c.log.WithFields(logrus.Fields{
log.FuncField: "handler for mock API",
"enforceValidJSON": enforceValidJSON,
Expand All @@ -53,6 +59,10 @@ func getJSONHandler(path string, headers []config.Header, file wrapper.IFileOps,
w.Header().Set(header.Key, header.Value)
}

if len(path) == 0 {
return
}

bytes, err := json.GetJSON(path, file)
if err != nil {
logger.WithError(err).Error("error serving JSON from this endpoint")
Expand All @@ -70,6 +80,10 @@ func getGeneralHandler(path string, headers []config.Header, file wrapper.IFileO
w.Header().Set(header.Key, header.Value)
}

if len(path) == 0 {
return
}

fileInfo, err := file.Open(path)
defer fileInfo.Close()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion mockApis/exampleStudentsApi/studentsApi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ keyFile = ""

[endpoints.getStudents]
path = "students"
file = "students.json"
method = "GET"

[endpoints.getNothing]
Expand Down

0 comments on commit a1b7bf7

Please sign in to comment.