forked from SpectoLabs/hoverfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synthesize.go
45 lines (36 loc) · 1022 Bytes
/
synthesize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bufio"
"fmt"
"net/http"
)
// synthesizeResponse calls middleware to populate response data, nothing gets pass proxy
func synthesizeResponse(req *http.Request, middleware string) *http.Response {
b := bufio.NewScanner(req.Body)
bodyStr := b.Text()
request := requestDetails{
Path: req.URL.Path,
Method: req.Method,
Destination: req.Host,
Query: req.URL.RawQuery,
Body: bodyStr,
RemoteAddr: req.RemoteAddr,
Headers: req.Header,
}
payload := Payload{Request: request}
c := NewConstructor(req, payload)
if middleware != "" {
err := c.ApplyMiddleware(middleware)
if err != nil {
var errorPayload Payload
errorPayload.Response.Status = 503
errorPayload.Response.Body = fmt.Sprintf("Middleware error: %s", err.Error())
c.payload = errorPayload
}
} else {
c.payload.Response.Body = "Precondition failed: middleware not provided."
c.payload.Response.Status = 428
}
response := c.reconstructResponse()
return response
}