github.com/damirazo/docker@v1.9.0/integration-cli/registry_mock.go (about) 1 package main 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "regexp" 7 "strings" 8 "sync" 9 10 "github.com/go-check/check" 11 ) 12 13 type handlerFunc func(w http.ResponseWriter, r *http.Request) 14 15 type testRegistry struct { 16 server *httptest.Server 17 hostport string 18 handlers map[string]handlerFunc 19 mu sync.Mutex 20 } 21 22 func (tr *testRegistry) registerHandler(path string, h handlerFunc) { 23 tr.mu.Lock() 24 defer tr.mu.Unlock() 25 tr.handlers[path] = h 26 } 27 28 func newTestRegistry(c *check.C) (*testRegistry, error) { 29 testReg := &testRegistry{handlers: make(map[string]handlerFunc)} 30 31 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 32 url := r.URL.String() 33 34 var matched bool 35 var err error 36 for re, function := range testReg.handlers { 37 matched, err = regexp.MatchString(re, url) 38 if err != nil { 39 c.Fatalf("Error with handler regexp") 40 return 41 } 42 if matched { 43 function(w, r) 44 break 45 } 46 } 47 48 if !matched { 49 c.Fatal("Unable to match", url, "with regexp") 50 } 51 })) 52 53 testReg.server = ts 54 testReg.hostport = strings.Replace(ts.URL, "http://", "", 1) 55 return testReg, nil 56 }