Pārlūkot izejas kodu

Load responses from YAML files

Lu Stadler 7 gadi atpakaļ
vecāks
revīzija
2f59250cf6
1 mainītis faili ar 35 papildinājumiem un 13 dzēšanām
  1. 35 13
      go/fake-http/fake-http.go

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

@ -14,6 +14,8 @@ import (
14 14
	"net/http/httputil"
15 15
	"net/url"
16 16
	"strings"
17
18
	"gopkg.in/yaml.v2"
17 19
)
18 20
19 21
var flags struct {
@ -30,16 +32,19 @@ func init() {
30 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 37
func main() {
41 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 48
	requestLog := make([]Request, 0)
44 49
45 50
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
@ -166,6 +171,9 @@ func respondWithStub(responses []Response, w http.ResponseWriter, req *http.Requ
166 171
	for _, header := range resp.Headers {
167 172
		w.Header().Set(header.Name, header.Value)
168 173
	}
174
	if resp.Status == 0 {
175
		resp.Status = 200
176
	}
169 177
	w.WriteHeader(resp.Status)
170 178
	w.Write([]byte(resp.Body))
171 179
@ -247,12 +255,12 @@ type Request []byte
247 255
248 256
// Response is a mocked HTTP response.
249 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 266
func (resp Response) String() string {
@ -288,8 +296,8 @@ func (resp Response) AsHTTP() *http.Response {
288 296
289 297
// Header is a single-valued HTTP header name and value
290 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 303
// JSONResponse creates a Response with "Content-Type: application/json".
@ -316,6 +324,20 @@ func readResponse(form url.Values) Response {
316 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 341
var stubTmpl = template.Must(template.New("").Parse(`<!doctype html>
320 342
<html>
321 343
	<head>