Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active April 5, 2016 16:03
Show Gist options
  • Save jochasinga/5850355f8c72418535b11acfaa09d7f9 to your computer and use it in GitHub Desktop.
Save jochasinga/5850355f8c72418535b11acfaa09d7f9 to your computer and use it in GitHub Desktop.
Chaining HTTP test servers, proxies and a switcher
// Error handling omitted for brevity
ts1 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world!")
}))
ts2 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello mars!")
}))
ts3 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello pluto!")
}))
delay := time.Duration(0)
// a proxy sitting in front of ts3
p := relay.NewProxy(delay, ts3)
sw := relay.NewSwitcher(delay, []HTTPTestServer{ts1, ts2, p})
resp1, _ := http.Get(sw.URL) // hits ts1
resp2, _ := http.Get(sw.URL) // hits ts2
resp3, _ := http.Get(sw.URL) // hits p, which eventually hits ts3
resp4, _ := http.Get(sw.URL) // hits ts1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment