roundtripper_test.go 1.9 KB

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