github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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.Fatal("Error with handler regexp") 40 } 41 if matched { 42 function(w, r) 43 break 44 } 45 } 46 47 if !matched { 48 c.Fatalf("Unable to match %s with regexp", url) 49 } 50 })) 51 52 testReg.server = ts 53 testReg.hostport = strings.Replace(ts.URL, "http://", "", 1) 54 return testReg, nil 55 }