Преглед на файлове

Only cache responses once

Lu Stadler преди 7 години
родител
ревизия
971982196f
променени са 1 файла, в които са добавени 16 реда и са изтрити 5 реда
  1. 16 5
      go/fake-http/fake-http.go

+ 16 - 5
go/fake-http/fake-http.go

@ -342,11 +342,15 @@ type Header struct {
342 342
}
343 343
344 344
// Responses is a list of responses that will be stubbed/faked.
345
type Responses []Response
345
type Responses struct {
346
	responses []Response
347
348
	seen map[string]bool
349
}
346 350
347 351
// Match returns a response definition matching the request.
348 352
func (rs *Responses) Match(req *http.Request) *Response {
349
	for _, resp := range *rs {
353
	for _, resp := range rs.responses {
350 354
		if req.Method == resp.Method && req.URL.String() == resp.Path {
351 355
			return &resp
352 356
		}
@ -357,7 +361,14 @@ func (rs *Responses) Match(req *http.Request) *Response {
357 361
// Add adds the response to the log, to include it in future Match
358 362
// calls.
359 363
func (rs *Responses) Add(r Response) {
360
	*rs = append(*rs, r)
364
	if rs.seen[r.Method+" "+r.Path] {
365
		return
366
	}
367
	rs.responses = append(rs.responses, r)
368
	if rs.seen == nil {
369
		rs.seen = make(map[string]bool)
370
	}
371
	rs.seen[r.Method+" "+r.Path] = true
361 372
}
362 373
363 374
// Load loads responses from the YAML file at path.
@ -369,7 +380,7 @@ func (rs *Responses) Load(path string) {
369 380
	if err != nil {
370 381
		log.Printf("Error: Parsing %s: %s", path, err)
371 382
	}
372
	*rs = responses
383
	rs.responses = responses
373 384
}
374 385
375 386
func (rs *Responses) loadFile(path string) ([]Response, error) {
@ -378,7 +389,7 @@ func (rs *Responses) loadFile(path string) ([]Response, error) {
378 389
		return nil, err
379 390
	}
380 391
381
	var responses Responses
392
	var responses []Response
382 393
	err = yaml.Unmarshal(out, &responses)
383 394
	if err != nil {
384 395
		return nil, err