github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/swarm/api/http/roundtripper_test.go (about) 1 // Copyright 2016 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 http 18 19 import ( 20 "io/ioutil" 21 "net/http" 22 "strings" 23 "testing" 24 "time" 25 ) 26 27 const port = "3222" 28 29 func TestRoundTripper(t *testing.T) { 30 serveMux := http.NewServeMux() 31 serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 32 if r.Method == "GET" { 33 w.Header().Set("Content-Type", "text/plain") 34 http.ServeContent(w, r, "", time.Unix(0, 0), strings.NewReader(r.RequestURI)) 35 } else { 36 http.Error(w, "Method "+r.Method+" is not supported.", http.StatusMethodNotAllowed) 37 } 38 }) 39 go http.ListenAndServe(":"+port, serveMux) 40 41 rt := &RoundTripper{Port: port} 42 trans := &http.Transport{} 43 trans.RegisterProtocol("bzz", rt) 44 client := &http.Client{Transport: trans} 45 resp, err := client.Get("bzz://test.com/path") 46 if err != nil { 47 t.Errorf("expected no error, got %v", err) 48 return 49 } 50 51 defer func() { 52 if resp != nil { 53 resp.Body.Close() 54 } 55 }() 56 57 content, err := ioutil.ReadAll(resp.Body) 58 if err != nil { 59 t.Errorf("expected no error, got %v", err) 60 return 61 } 62 if string(content) != "/HTTP/1.1:/test.com/path" { 63 t.Errorf("incorrect response from http server: expected '%v', got '%v'", "/HTTP/1.1:/test.com/path", string(content)) 64 } 65 66 }