github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/common/httpclient/httpclient_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package httpclient 18 19 import ( 20 "io/ioutil" 21 "net/http" 22 "os" 23 "path" 24 "testing" 25 26 "github.com/ethereumproject/go-ethereum/common" 27 "github.com/ethereumproject/go-ethereum/crypto" 28 ) 29 30 func TestGetAuthContent(t *testing.T) { 31 dir, err := ioutil.TempDir("", "httpclient-test") 32 if err != nil { 33 t.Fatal("cannot create temporary directory:", err) 34 } 35 defer os.RemoveAll(dir) 36 client := New(dir) 37 38 text := "test" 39 hash := crypto.Keccak256Hash([]byte(text)) 40 if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil { 41 t.Fatal("could not write test file", err) 42 } 43 content, err := client.GetAuthContent("file:///test.content", hash) 44 if err != nil { 45 t.Errorf("no error expected, got %v", err) 46 } 47 if string(content) != text { 48 t.Errorf("incorrect content. expected %v, got %v", text, string(content)) 49 } 50 51 hash = common.Hash{} 52 content, err = client.GetAuthContent("file:///test.content", hash) 53 expected := "content hash mismatch 0000000000000000000000000000000000000000000000000000000000000000 != 9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658 (exp)" 54 if err == nil { 55 t.Errorf("expected error, got nothing") 56 } else { 57 if err.Error() != expected { 58 t.Errorf("expected error '%s' got '%v'", expected, err) 59 } 60 } 61 62 } 63 64 type rt struct{} 65 66 func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return } 67 68 func TestRegisterScheme(t *testing.T) { 69 client := New("/tmp/") 70 if client.HasScheme("scheme") { 71 t.Errorf("expected scheme not to be registered") 72 } 73 client.RegisterScheme("scheme", rt{}) 74 if !client.HasScheme("scheme") { 75 t.Errorf("expected scheme to be registered") 76 } 77 }