From 569113d2d9c1a397775377b496c64135df194091 Mon Sep 17 00:00:00 2001 From: Saul Pinales Date: Fri, 2 Oct 2020 18:22:35 -0400 Subject: [PATCH] Allow setting different responses per request, idea --- internal/server/http/handler.go | 4 +++- internal/server/http/imposter.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/server/http/handler.go b/internal/server/http/handler.go index 83ee85a..60b682e 100644 --- a/internal/server/http/handler.go +++ b/internal/server/http/handler.go @@ -3,6 +3,7 @@ package http import ( "io/ioutil" "log" + "math/rand" "net/http" "os" "time" @@ -31,7 +32,8 @@ func writeHeaders(imposter Imposter, w http.ResponseWriter) { } func writeBody(imposter Imposter, w http.ResponseWriter) { - wb := []byte(imposter.Response.Body) + randomIndex := rand.Intn(len(imposter.Response.Body)) + wb := []byte(imposter.Response.Body[randomIndex]) if imposter.Response.BodyFile != nil { bodyFile := imposter.CalculateFilePath(*imposter.Response.BodyFile) diff --git a/internal/server/http/imposter.go b/internal/server/http/imposter.go index 3e5600f..95b7f43 100644 --- a/internal/server/http/imposter.go +++ b/internal/server/http/imposter.go @@ -40,7 +40,7 @@ type Request struct { // Response represent the structure of real response type Response struct { Status int `json:"status"` - Body string `json:"body"` + Body []string `json:"body"` BodyFile *string `json:"bodyFile"` Headers *map[string]string `json:"headers"` Delay ResponseDelay `json:"delay"`