roundtripper_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "github.com/ethereum/go-ethereum/common/httpclient"
  24. )
  25. const port = "3222"
  26. func TestRoundTripper(t *testing.T) {
  27. serveMux := http.NewServeMux()
  28. serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  29. if r.Method == "GET" {
  30. w.Header().Set("Content-Type", "text/plain")
  31. http.ServeContent(w, r, "", time.Unix(0, 0), strings.NewReader(r.RequestURI))
  32. } else {
  33. http.Error(w, "Method "+r.Method+" is not supported.", http.StatusMethodNotAllowed)
  34. }
  35. })
  36. go http.ListenAndServe(":"+port, serveMux)
  37. rt := &RoundTripper{Port: port}
  38. client := httpclient.New("/")
  39. client.RegisterProtocol("bzz", rt)
  40. resp, err := client.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. }