add HasScheme method to check for register scheme handler protocols + test

This commit is contained in:
zelig 2015-06-02 17:48:07 +01:00
parent 5292414f11
commit 618c5d7053
2 changed files with 32 additions and 0 deletions

View file

@ -13,12 +13,14 @@ import (
type DocServer struct { type DocServer struct {
*http.Transport *http.Transport
DocRoot string DocRoot string
schemes []string
} }
func New(docRoot string) (self *DocServer) { func New(docRoot string) (self *DocServer) {
self = &DocServer{ self = &DocServer{
Transport: &http.Transport{}, Transport: &http.Transport{},
DocRoot: docRoot, DocRoot: docRoot,
schemes: []string{"file"},
} }
self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot))) self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
return return
@ -34,6 +36,20 @@ func (self *DocServer) Client() *http.Client {
} }
} }
func (self *DocServer) RegisterScheme(scheme string, rt http.RoundTripper) {
self.schemes = append(self.schemes, scheme)
self.RegisterProtocol(scheme, rt)
}
func (self *DocServer) HasScheme(scheme string) bool {
for _, s := range self.schemes {
if s == scheme {
return true
}
}
return false
}
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) { func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
// retrieve content // retrieve content

View file

@ -2,6 +2,7 @@ package docserver
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"testing" "testing"
@ -36,3 +37,18 @@ func TestGetAuthContent(t *testing.T) {
} }
} }
type rt struct{}
func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return }
func TestRegisterScheme(t *testing.T) {
ds := New("/tmp/")
if ds.HasScheme("scheme") {
t.Errorf("expected scheme not to be registered")
}
ds.RegisterScheme("scheme", rt{})
if !ds.HasScheme("scheme") {
t.Errorf("expected scheme to be registered")
}
}