http_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package scenarios
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/testing/assert"
  9. v2http "v2ray.com/core/testing/servers/http"
  10. )
  11. func TestHttpProxy(t *testing.T) {
  12. assert := assert.On(t)
  13. httpServer := &v2http.Server{
  14. Port: net.Port(50042),
  15. PathHandler: make(map[string]http.HandlerFunc),
  16. }
  17. _, err := httpServer.Start()
  18. assert.Error(err).IsNil()
  19. defer httpServer.Close()
  20. assert.Error(InitializeServerSetOnce("test_5")).IsNil()
  21. transport := &http.Transport{
  22. Proxy: func(req *http.Request) (*url.URL, error) {
  23. return url.Parse("http://127.0.0.1:50040/")
  24. },
  25. }
  26. client := &http.Client{
  27. Transport: transport,
  28. }
  29. resp, err := client.Get("http://127.0.0.1:50042/")
  30. assert.Error(err).IsNil()
  31. assert.Int(resp.StatusCode).Equals(200)
  32. content, err := ioutil.ReadAll(resp.Body)
  33. assert.Error(err).IsNil()
  34. assert.String(string(content)).Equals("Home")
  35. CloseAllServers()
  36. }
  37. func TestBlockHTTP(t *testing.T) {
  38. assert := assert.On(t)
  39. httpServer := &v2http.Server{
  40. Port: net.Port(50042),
  41. PathHandler: make(map[string]http.HandlerFunc),
  42. }
  43. _, err := httpServer.Start()
  44. assert.Error(err).IsNil()
  45. defer httpServer.Close()
  46. assert.Error(InitializeServerSetOnce("test_5")).IsNil()
  47. transport := &http.Transport{
  48. Proxy: func(req *http.Request) (*url.URL, error) {
  49. return url.Parse("http://127.0.0.1:50040/")
  50. },
  51. }
  52. client := &http.Client{
  53. Transport: transport,
  54. }
  55. resp, err := client.Get("http://127.0.0.1:50049/")
  56. assert.Error(err).IsNil()
  57. assert.Int(resp.StatusCode).Equals(403)
  58. CloseAllServers()
  59. }