-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
84 lines (66 loc) · 2.28 KB
/
middleware.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"time"
)
type RequestResponsePairViewV1 struct {
Response ResponseDetailsView `json:"response"`
Request RequestDetailsView `json:"request"`
}
type ResponseDetailsView struct {
Status int `json:"status"`
Body string `json:"body"`
EncodedBody bool `json:"encodedBody"`
Headers map[string][]string `json:"headers,omitempty"`
}
// RequestDetailsView is used when marshalling and unmarshalling RequestDetails
type RequestDetailsView struct {
RequestType *string `json:"requestType,omitempty"`
Path *string `json:"path"`
Method *string `json:"method"`
Destination *string `json:"destination"`
Scheme *string `json:"scheme"`
Query *string `json:"query"`
QueryMap map[string][]string `json:"-"`
Body *string `json:"body"`
Headers map[string][]string `json:"headers"`
}
func main() {
fmt.Println("Starting Hoverfly middleware...")
l := log.New(os.Stderr, "", 0)
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
var payload RequestResponsePairViewV1
err := json.Unmarshal(s.Bytes(), &payload)
if err != nil {
l.Println("Failed to unmarshal payload from hoverfly")
}
payload = transform(payload)
bts, err := json.Marshal(payload)
if err != nil {
l.Println("Failed to marshal new payload")
}
os.Stdout.Write(bts)
}
}
//////////////////////////////////////////////////////////////////////////////
// DON'T CHANGE ANYTHING ABOVE THIS LINE!!
//
// Just replace the transform function with your own middleware logic
//////////////////////////////////////////////////////////////////////////////
func transform(payload RequestResponsePairViewV1) RequestResponsePairViewV1 {
// Set the payload.Request.Body to a value simply so we can show how to
// use it in the response later
sample_request_body := "abc"
payload.Request.Body = &sample_request_body
payload.Response.Body = "abc" + "def" + fmt.Sprint(4) + *payload.Request.Body
payload.Response.Body = fmt.Sprint(time.Now())
payload.Response.Status = 200
payload.Response.Headers = map[string][]string{"header": {"value"}}
payload.Response.EncodedBody = false
return payload
}