forked from fnproject/fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn-test-utils.go
92 lines (77 loc) · 2.16 KB
/
fn-test-utils.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
85
86
87
88
89
90
91
92
package main
import (
"context"
"encoding/json"
"io"
"log"
"time"
fdk "github.com/fnproject/fdk-go"
"net/http"
)
type AppRequest struct {
// if specified we 'sleep' the specified msecs
SleepTime int `json:"sleepTime,omitempty"`
// if specified, this is our response http status code
ResponseCode int `json:"responseCode,omitempty"`
// if specified, this is our response content-type
ResponseContentType string `json:"responseContentType,omitempty"`
// if specified, this is echoed back to client
EchoContent string `json:"echoContent,omitempty"`
// verbose mode
IsDebug bool `json:"isDebug,omitempty"`
// simulate crash
IsCrash bool `json:"isCrash,omitempty"`
// TODO: simulate slow read/slow write
// TODO: simulate partial write/read
// TODO: simulate mem leak
// TODO: simulate high cpu usage
// TODO: simulate high mem usage
// TODO: simulate large body upload/download
}
type AppResponse struct {
Request AppRequest `json:"request"`
Headers http.Header `json:"header"`
Config map[string]string `json:"config"`
}
func AppHandler(ctx context.Context, in io.Reader, out io.Writer) {
fnctx := fdk.Context(ctx)
var request AppRequest
json.NewDecoder(in).Decode(&request)
if request.IsDebug {
log.Printf("Received request %v", request)
log.Printf("Received headers %v", fnctx.Header)
log.Printf("Received config %v", fnctx.Config)
}
// simulate load if requested
if request.SleepTime > 0 {
if request.IsDebug {
log.Printf("Sleeping %d", request.SleepTime)
}
time.Sleep(time.Duration(request.SleepTime) * time.Millisecond)
}
// simulate crash
if request.IsCrash {
panic("Crash requested")
}
// custom response code
if request.ResponseCode != 0 {
fdk.WriteStatus(out, request.ResponseCode)
} else {
fdk.WriteStatus(out, 200)
}
// custom content type
if request.ResponseContentType != "" {
fdk.SetHeader(out, "Content-Type", request.ResponseContentType)
} else {
fdk.SetHeader(out, "Content-Type", "application/json")
}
resp := AppResponse{
Request: request,
Headers: fnctx.Header,
Config: fnctx.Config,
}
json.NewEncoder(out).Encode(&resp)
}
func main() {
fdk.Handle(fdk.HandlerFunc(AppHandler))
}