Bladeren bron

Move response matching onto the type that owns them

Lu Stadler 7 jaren geleden
bovenliggende
commit
3041287b71
1 gewijzigde bestanden met toevoegingen van 15 en 14 verwijderingen
  1. 15 14
      go/fake-http/fake-http.go

+ 15 - 14
go/fake-http/fake-http.go

@ -39,9 +39,6 @@ func init() {
39 39
40 40
var responses Responses
41 41
42
// Responses is a list of responses that will be stubbed/faked.
43
type Responses []Response
44
45 42
func main() {
46 43
	flag.Parse()
47 44
@ -140,17 +137,8 @@ func proxyMinikube() error {
140 137
	return nil
141 138
}
142 139
143
func matchResponse(req *http.Request, responses []Response) *Response {
144
	for _, resp := range responses {
145
		if req.Method == resp.Method && req.URL.Path == resp.Path {
146
			return &resp
147
		}
148
	}
149
	return nil
150
}
151
152
func respondWithStub(responses []Response, w http.ResponseWriter, req *http.Request) *http.Response {
153
	resp := matchResponse(req, responses)
140
func respondWithStub(responses Responses, w http.ResponseWriter, req *http.Request) *http.Response {
141
	resp := responses.Match(req)
154 142
	if resp == nil {
155 143
		resp = &Response{Status: 404, Body: "Not found"}
156 144
	}
@ -358,6 +346,19 @@ type Header struct {
358 346
	Value string `yaml:"value"`
359 347
}
360 348
349
// Responses is a list of responses that will be stubbed/faked.
350
type Responses []Response
351
352
// Match returns a response definition matching the request.
353
func (rs *Responses) Match(req *http.Request) *Response {
354
	for _, resp := range *rs {
355
		if req.Method == resp.Method && req.URL.Path == resp.Path {
356
			return &resp
357
		}
358
	}
359
	return nil
360
}
361
361 362
// Load loads responses from the YAML file at path.
362 363
func (rs *Responses) Load(path string) {
363 364
	if path == "" {