summaryrefslogtreecommitdiff
path: root/internal/test/handler.go
blob: bb5f7a0c6b91e2b84f4ab994ca6017a48b702a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package test

import (
	"net/http"
	"sync"
)

// HandlerSet is a struct with a mutex that allows us to swap handlers while a test server is running
type HandlerSet struct {
	mu      sync.Mutex
	handler http.Handler
}

// SetHandler sets the handler to `handler`
func (hs *HandlerSet) SetHandler(handler http.Handler) {
	hs.mu.Lock()
	hs.handler = handler
	hs.mu.Unlock()
}

// ServeHTTP serves HTTP using the handler
func (hs *HandlerSet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	hs.mu.Lock()
	handler := hs.handler
	hs.mu.Unlock()
	handler.ServeHTTP(w, r)
}