Skip to content

Commit

Permalink
add multi responses handler + nice print of body in record
Browse files Browse the repository at this point in the history
  • Loading branch information
avrahams committed Nov 7, 2022
1 parent 4f213db commit 8237770
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
21 changes: 19 additions & 2 deletions server/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,24 @@ var WithPathSuffix = func(pathSuffix string) RequestHandlerOption {
//WithResponse option sets the response for the handler
var WithResponse = func(response []byte) RequestHandlerOption {
return func(o *requestHandlerOptions) error {
if len(response) != 0 && o.handler != nil {
return fmt.Errorf("response can't be set with handler")
if len(response) != 0 && (o.handler != nil || len(o.responses) != 0) {
return fmt.Errorf("response can't be set with handler or with responses array")
}
o.response = response
return nil
}
}

var WithResponses = func(responses [][]byte) RequestHandlerOption {
return func(o *requestHandlerOptions) error {
if len(responses) != 0 && (o.handler != nil || len(o.response) != 0) {
return fmt.Errorf("responses can't be set with handler or with fixed response")
}
o.responses = responses
return nil
}
}

//WithHandler option sets the handler for the handler
var WithHandler = func(handler RequestHandler) RequestHandlerOption {
return func(o *requestHandlerOptions) error {
Expand Down Expand Up @@ -101,6 +111,7 @@ type requestHandlerOptions struct {
method string
path string
response []byte
responses [][]byte
expectedRequest []byte
expectedRequestFile string
updateExpected bool
Expand Down Expand Up @@ -131,6 +142,12 @@ func (o *requestHandlerOptions) getOrCreateHandler() RequestHandler {
if len(o.response) != 0 {
w.Write(o.response)
}
if len(o.responses) != 0 {
//pop the next response
var response []byte
response, o.responses = o.responses[0], o.responses[1:]
w.Write(response)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions server/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (h *serverRequestHandler) shouldHandle(r *http.Request, reqCount int) bool
if h.options.reqNum != 0 && h.options.reqNum != reqCount {
return false
}
//if handler is configured with responses array and served all responses, return false
if h.options.responses != nil && len(h.options.responses) == 0 {
return false
}
return true
}

Expand Down
11 changes: 10 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,16 @@ func (ts *mockTestingServer) mainHandler(w http.ResponseWriter, r *http.Request)
}

func (ts *mockTestingServer) recordRequest(r *http.Request, reqBody string, handlersCount int) {
if ts.options.recordOnlyUnhandled && handlersCount > 0 {
return
}

var iBody interface{}
json.Unmarshal([]byte(reqBody), &iBody)

record := struct {
Body string `json:"body,omitempty"`
BodyObj interface{} `json:"bodyObj,omitempty"`
RequestNumber int `json:"req_num,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
URL string `json:"url,omitempty"`
Expand All @@ -156,10 +164,11 @@ func (ts *mockTestingServer) recordRequest(r *http.Request, reqBody string, hand
URL: r.URL.String(),
Method: r.Method,
Body: reqBody,
BodyObj: iBody,
RequestNumber: ts.reqCount,
HandlersCount: handlersCount,
}
reqBytes, _ := json.MarshalIndent(&record, "", " ")
reqBytes, _ := json.MarshalIndent(&record, "", " ")
fileName := fmt.Sprintf("%s/request_%d.json", ts.options.recordFolder, ts.reqCount)
_ = ioutil.WriteFile(fileName, reqBytes, 0644)
}
Expand Down
4 changes: 3 additions & 1 deletion server/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var WithTLS = func() ServerOption {
}

//WithRecord option enables recording of requests to the server to a specific folder and after a specific number of requests
var WithRequestsRecorder = func(record bool, recordsFolder string, recordAfterReqNum int) ServerOption {
var WithRequestsRecorder = func(record bool, recordsFolder string, recordAfterReqNum int, recordOnlyUnhandled bool) ServerOption {
return func(o *serverOptions, isUpdate bool) error {
if record {
if recordsFolder == "" {
Expand All @@ -70,6 +70,7 @@ var WithRequestsRecorder = func(record bool, recordsFolder string, recordAfterRe
o.recordFolder = recordsFolder
o.record = record
o.recordAfterReqNum = recordAfterReqNum
o.recordOnlyUnhandled = recordOnlyUnhandled
return nil
}
}
Expand All @@ -81,6 +82,7 @@ type serverOptions struct {
recordFolder string
recordAfterReqNum int
record bool
recordOnlyUnhandled bool
headers map[string]string
defaultRequestHandlers []serverRequestHandler
}
Expand Down

0 comments on commit 8237770

Please sign in to comment.