Bladeren bron

Support delaying responses

Lu Stadler 7 jaren geleden
bovenliggende
commit
9356299434
2 gewijzigde bestanden met toevoegingen van 10 en 1 verwijderingen
  1. 0 1
      go/fake-http/README.md
  2. 10 0
      go/fake-http/fake-http.go

+ 0 - 1
go/fake-http/README.md

@ -5,7 +5,6 @@
5 5
- support matching query strings
6 6
- refactoring
7 7
- /_clear endpoint
8
- (randomized) delays
9 8
- some kind of templating (request info + random functions?)
10 9
- lua integration? ;)
11 10

+ 10 - 0
go/fake-http/fake-http.go

@ -9,12 +9,14 @@ import (
9 9
	"io"
10 10
	"io/ioutil"
11 11
	"log"
12
	"math/rand"
12 13
	"net/http"
13 14
	"net/url"
14 15
	"os"
15 16
	"os/exec"
16 17
	"path"
17 18
	"strings"
19
	"time"
18 20
19 21
	"gopkg.in/yaml.v2"
20 22
)
@ -136,6 +138,11 @@ func respondWithStub(responses Responses, w http.ResponseWriter, req *http.Reque
136 138
		resp = &Response{Status: 404, Body: "Not found"}
137 139
	}
138 140
141
	time.Sleep(resp.Delay)
142
	if resp.RandomDelay > 0 {
143
		time.Sleep(time.Duration(rand.Intn(int(resp.RandomDelay))))
144
	}
145
139 146
	for _, header := range resp.Headers {
140 147
		w.Header().Set(header.Name, header.Value)
141 148
	}
@ -291,6 +298,9 @@ type Response struct {
291 298
	Status  int      `yaml:"status"`
292 299
	Headers []Header `yaml:"headers"`
293 300
	Body    string   `yaml:"body"`
301
302
	Delay       time.Duration `yaml:"delay"`
303
	RandomDelay time.Duration `yaml:"randomDelay"`
294 304
}
295 305
296 306
func (resp Response) String() string {