Просмотр исходного кода

Move response matching onto the type that owns them

Lu Stadler лет назад: 7
Родитель
Сommit
3041287b71
1 измененных файлов с 15 добавлено и 14 удалено
  1. 15 14
      go/fake-http/fake-http.go

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

39
39
40
var responses Responses
40
var responses Responses
41
41
42
// Responses is a list of responses that will be stubbed/faked.
43
type Responses []Response
44
45
func main() {
42
func main() {
46
	flag.Parse()
43
	flag.Parse()
47
44
140
	return nil
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
	if resp == nil {
142
	if resp == nil {
155
		resp = &Response{Status: 404, Body: "Not found"}
143
		resp = &Response{Status: 404, Body: "Not found"}
156
	}
144
	}
358
	Value string `yaml:"value"`
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
// Load loads responses from the YAML file at path.
362
// Load loads responses from the YAML file at path.
362
func (rs *Responses) Load(path string) {
363
func (rs *Responses) Load(path string) {
363
	if path == "" {
364
	if path == "" {