Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement - Imposter responses #178

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions internal/server/http/handler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package http

import (
"io"
"log"
"net/http"
"os"
"time"
)

// ImposterHandler create specific handler for the received imposter
// Handler create specific handler for the received imposter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a mistake/typo? 🤔

func ImposterHandler(i Imposter) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := i.NextResponse()
Expand All @@ -17,7 +15,7 @@ func ImposterHandler(i Imposter) http.HandlerFunc {
}
writeHeaders(res, w)
w.WriteHeader(res.Status)
writeBody(i, res, w)
writeBody(res, w)
}
}

Expand All @@ -31,27 +29,9 @@ func writeHeaders(r Response, w http.ResponseWriter) {
}
}

func writeBody(i Imposter, r Response, w http.ResponseWriter) {
wb := []byte(r.Body)

if r.BodyFile != nil {
bodyFile := i.CalculateFilePath(*r.BodyFile)
wb = fetchBodyFromFile(bodyFile)
}
w.Write(wb)
}

func fetchBodyFromFile(bodyFile string) (bytes []byte) {
if _, err := os.Stat(bodyFile); os.IsNotExist(err) {
log.Printf("the body file %s not found\n", bodyFile)
return
}

f, _ := os.Open(bodyFile)
defer f.Close()
bytes, err := io.ReadAll(f)
func writeBody(r Response, w http.ResponseWriter) {
_, err := w.Write(r.BodyData)
if err != nil {
log.Printf("imposible read the file %s: %v\n", bodyFile, err)
log.Printf("error writing body: %v\n", err)
}
return
}
10 changes: 8 additions & 2 deletions internal/server/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestImposterHandler(t *testing.T) {
assert.NoError(t, err)

rec := httptest.NewRecorder()
tt.imposter.PopulateBodyData()
handler := ImposterHandler(tt.imposter)

handler.ServeHTTP(rec, req)
Expand Down Expand Up @@ -95,6 +96,7 @@ func TestInvalidRequestWithSchema(t *testing.T) {
req, err := http.NewRequest("POST", "/gophers", bytes.NewBuffer(tt.request))
assert.Nil(t, err)
rec := httptest.NewRecorder()
tt.imposter.PopulateBodyData()
handler := ImposterHandler(tt.imposter)

handler.ServeHTTP(rec, req)
Expand Down Expand Up @@ -125,6 +127,7 @@ func TestImposterHandler_MultipleRequests(t *testing.T) {
},
}

imp.PopulateBodyData()
handler := ImposterHandler(imp)

// First request
Expand All @@ -141,12 +144,15 @@ func TestImposterHandler_MultipleRequests(t *testing.T) {
})

t.Run("idempotent", func(t *testing.T) {
handler := ImposterHandler(Imposter{
imp := Imposter{
Request: Request{Method: "POST", Endpoint: "/gophers"},
Response: Responses{
{Status: http.StatusAccepted, Body: "Accepted"},
},
})
}
imp.PopulateBodyData()

handler := ImposterHandler(imp)

// First request
rec := httptest.NewRecorder()
Expand Down
36 changes: 33 additions & 3 deletions internal/server/http/imposter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -57,6 +58,34 @@ func (i *Imposter) CalculateFilePath(filePath string) string {
return path.Join(i.BasePath, filePath)
}

func (i *Imposter) PopulateBodyData() {
for idx, resp := range i.Response {
resp.BodyData = []byte(resp.Body)

if resp.BodyFile != nil {
bodyFile := i.CalculateFilePath(*resp.BodyFile)
resp.BodyData = fetchBodyFromFile(bodyFile)
}

i.Response[idx] = resp
}
}

func fetchBodyFromFile(bodyFile string) (bytes []byte) {
if _, err := os.Stat(bodyFile); os.IsNotExist(err) {
log.Printf("ERROR: the body file %s not found\n", bodyFile)
return
}

f, _ := os.Open(bodyFile)
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
log.Printf("ERROR: imposible read the file %s: %v\n", bodyFile, err)
}
return
}

// Request represent the structure of real request
type Request struct {
Method string `json:"method"`
Expand All @@ -71,6 +100,7 @@ type Response struct {
Status int `json:"status"`
Body string `json:"body"`
BodyFile *string `json:"bodyFile" yaml:"bodyFile"`
BodyData []byte `json:"-" yaml:"-"`
Headers *map[string]string `json:"headers"`
Delay ResponseDelay `json:"delay" yaml:"delay"`
}
Expand Down Expand Up @@ -201,9 +231,9 @@ func (i ImposterFs) unmarshalImposters(imposterConfig ImposterConfig) ([]Imposte
return nil, fmt.Errorf("%w: error while unmarshalling imposter's file %s", parseError, imposterConfig.FilePath)
}

for i := range imposters {
imposters[i].BasePath = filepath.Dir(imposterConfig.FilePath)
imposters[i].Path = imposterConfig.FilePath
for idx := range imposters {
imposters[idx].BasePath = filepath.Dir(filepath.Join(i.path, imposterConfig.FilePath))
imposters[idx].Path = imposterConfig.FilePath
}

return imposters, nil
Expand Down
1 change: 1 addition & 0 deletions internal/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (s *Server) Shutdown() error {

func (s *Server) addImposterHandler(imposters []Imposter) {
for _, imposter := range imposters {
imposter.PopulateBodyData()
r := s.router.HandleFunc(imposter.Request.Endpoint, ImposterHandler(imposter)).
Methods(imposter.Request.Method).
MatcherFunc(MatcherBySchema(imposter))
Expand Down