github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/http/roundtripper_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package http 13 14 import ( 15 "io/ioutil" 16 "net" 17 "net/http" 18 "net/http/httptest" 19 "strings" 20 "testing" 21 "time" 22 ) 23 24 func TestRoundTripper(t *testing.T) { 25 serveMux := http.NewServeMux() 26 serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 27 if r.Method == "GET" { 28 w.Header().Set("Content-Type", "text/plain") 29 http.ServeContent(w, r, "", time.Unix(0, 0), strings.NewReader(r.RequestURI)) 30 } else { 31 http.Error(w, "Method "+r.Method+" is not supported.", http.StatusMethodNotAllowed) 32 } 33 }) 34 35 srv := httptest.NewServer(serveMux) 36 defer srv.Close() 37 38 host, port, _ := net.SplitHostPort(srv.Listener.Addr().String()) 39 rt := &RoundTripper{Host: host, Port: port} 40 trans := &http.Transport{} 41 trans.RegisterProtocol("bzz", rt) 42 client := &http.Client{Transport: trans} 43 resp, err := client.Get("bzz://test.com/path") 44 if err != nil { 45 t.Errorf("expected no error, got %v", err) 46 return 47 } 48 49 defer func() { 50 if resp != nil { 51 resp.Body.Close() 52 } 53 }() 54 55 content, err := ioutil.ReadAll(resp.Body) 56 if err != nil { 57 t.Errorf("expected no error, got %v", err) 58 return 59 } 60 if string(content) != "/HTTP/1.1:/test.com/path" { 61 t.Errorf("incorrect response from http server: expected '%v', got '%v'", "/HTTP/1.1:/test.com/path", string(content)) 62 } 63 64 }