Skip to content

Commit

Permalink
Add request/response header processing support
Browse files Browse the repository at this point in the history
  • Loading branch information
enginyoyen committed Apr 8, 2021
1 parent ca58bbd commit dbfb519
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pkg/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ type Mapping struct {
}

type Request struct {
Url string
Method string
Url string
Method string
Headers map[string]string
}

type Response struct {
Status int
Body json.RawMessage
WithDelay time.Duration
Headers map[string]string
}

func MapRequests(files *[]string) *[]Mapping {
Expand Down
10 changes: 9 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func Serve(config *cmd.Config, maps *[]mapping.Mapping) {

for _, v := range *mappings {
m := v
r.HandleFunc(m.Request.Url, func(w http.ResponseWriter, r *http.Request) {
route := r.HandleFunc(m.Request.Url, func(w http.ResponseWriter, r *http.Request) {
// Set headers in the
for header, value := range m.Response.Headers {
w.Header().Set(header, value)
}
w.WriteHeader(m.Response.Status)
body := string(m.Response.Body)
body = strings.TrimLeft(body, "\"")
Expand All @@ -32,6 +36,10 @@ func Serve(config *cmd.Config, maps *[]mapping.Mapping) {
time.Sleep(time.Millisecond * m.Response.WithDelay)
}
}).Methods(m.Request.Method)
//Set request headers
for header, value := range m.Request.Headers {
route.Headers(header, value)
}
}

addr := config.BindAddress + ":" + strconv.Itoa(config.Port)
Expand Down

0 comments on commit dbfb519

Please sign in to comment.