http_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package scenarios
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. v2testing "github.com/v2ray/v2ray-core/testing"
  9. "github.com/v2ray/v2ray-core/testing/assert"
  10. v2http "github.com/v2ray/v2ray-core/testing/servers/http"
  11. )
  12. func TestHttpProxy(t *testing.T) {
  13. v2testing.Current(t)
  14. httpServer := &v2http.Server{
  15. Port: v2net.Port(50042),
  16. PathHandler: make(map[string]http.HandlerFunc),
  17. }
  18. _, err := httpServer.Start()
  19. assert.Error(err).IsNil()
  20. defer httpServer.Close()
  21. assert.Error(InitializeServerSetOnce("test_5")).IsNil()
  22. transport := &http.Transport{
  23. Proxy: func(req *http.Request) (*url.URL, error) {
  24. return url.Parse("http://127.0.0.1:50040/")
  25. },
  26. }
  27. client := &http.Client{
  28. Transport: transport,
  29. }
  30. resp, err := client.Get("http://127.0.0.1:50042/")
  31. assert.Error(err).IsNil()
  32. assert.Int(resp.StatusCode).Equals(200)
  33. content, err := ioutil.ReadAll(resp.Body)
  34. assert.Error(err).IsNil()
  35. assert.StringLiteral(string(content)).Equals("Home")
  36. CloseAllServers()
  37. }