Quellcode durchsuchen

Load responses from YAML files

Lu Stadler vor 7 Jahren
Ursprung
Commit
2f59250cf6
1 geänderte Dateien mit 35 neuen und 13 gelöschten Zeilen
  1. 35 13
      go/fake-http/fake-http.go

+ 35 - 13
go/fake-http/fake-http.go

14
	"net/http/httputil"
14
	"net/http/httputil"
15
	"net/url"
15
	"net/url"
16
	"strings"
16
	"strings"
17
18
	"gopkg.in/yaml.v2"
17
)
19
)
18
20
19
var flags struct {
21
var flags struct {
30
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
32
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
31
}
33
}
32
34
33
var responses = []Response{
34
	JSONResponse("GET", "/api", `{"kind": "APIVersions", "versions": ["v1"]}`),
35
	JSONResponse("GET", "/apis", `{}`),
36
	JSONResponse("GET", "/api/v1", `{"kind": "APIResourceList", "resources": [{"name": "pods", "namespaced": true, "kind": "Pod", "verbs": ["get", "list"], "categories": ["all"]}]}`),
37
	JSONResponse("GET", "/api/v1/namespaces/default/pods", `{"kind": "PodList", "apiVersion": "v1", "items": [{"metadata": {"name": "oops-v1-214fbj25k"}, "status": {"phase": "Running", "conditions": [{"type": "Ready", "status": "True"}], "startTime": "2018-06-08T09:48:22Z"}}]}`),
38
}
35
var responses = []Response{}
39
36
40
func main() {
37
func main() {
41
	flag.Parse()
38
	flag.Parse()
42
39
40
	if flag.NArg() == 1 {
41
		rs, err := loadResponses(flag.Arg(0))
42
		if err != nil {
43
			log.Fatalf("Error: Parsing %s: %s", flag.Arg(0), err)
44
		}
45
		responses = rs
46
	}
47
43
	requestLog := make([]Request, 0)
48
	requestLog := make([]Request, 0)
44
49
45
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
50
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
166
	for _, header := range resp.Headers {
171
	for _, header := range resp.Headers {
167
		w.Header().Set(header.Name, header.Value)
172
		w.Header().Set(header.Name, header.Value)
168
	}
173
	}
174
	if resp.Status == 0 {
175
		resp.Status = 200
176
	}
169
	w.WriteHeader(resp.Status)
177
	w.WriteHeader(resp.Status)
170
	w.Write([]byte(resp.Body))
178
	w.Write([]byte(resp.Body))
171
179
247
255
248
// Response is a mocked HTTP response.
256
// Response is a mocked HTTP response.
249
type Response struct {
257
type Response struct {
250
	Method string
251
	Path   string
258
	Method string `yaml:"method"`
259
	Path   string `yaml:"path"`
252
260
253
	Status  int
254
	Headers []Header
255
	Body    string
261
	Status  int      `yaml:"status"`
262
	Headers []Header `yaml:"headers"`
263
	Body    string   `yaml:"body"`
256
}
264
}
257
265
258
func (resp Response) String() string {
266
func (resp Response) String() string {
288
296
289
// Header is a single-valued HTTP header name and value
297
// Header is a single-valued HTTP header name and value
290
type Header struct {
298
type Header struct {
291
	Name  string
292
	Value string
299
	Name  string `yaml:"name"`
300
	Value string `yaml:"value"`
293
}
301
}
294
302
295
// JSONResponse creates a Response with "Content-Type: application/json".
303
// JSONResponse creates a Response with "Content-Type: application/json".
316
	return r
324
	return r
317
}
325
}
318
326
327
func loadResponses(path string) ([]Response, error) {
328
	out, err := ioutil.ReadFile(path)
329
	if err != nil {
330
		return nil, err
331
	}
332
333
	err = yaml.Unmarshal(out, &responses)
334
	if err != nil {
335
		return nil, err
336
	}
337
338
	return responses, nil
339
}
340
319
var stubTmpl = template.Must(template.New("").Parse(`<!doctype html>
341
var stubTmpl = template.Must(template.New("").Parse(`<!doctype html>
320
<html>
342
<html>
321
	<head>
343
	<head>