http_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package http
  2. import (
  3. "bufio"
  4. "github.com/v2ray/v2ray-core/testing/assert"
  5. "net/http"
  6. "strings"
  7. "testing"
  8. )
  9. func TestHopByHopHeadersStrip(t *testing.T) {
  10. var rawRequest = `GET /pkg/net/http/ HTTP/1.1
  11. Host: golang.org
  12. Connection: keep-alive,Foo, Bar
  13. Foo: foo
  14. Bar: bar
  15. Proxy-Connection: keep-alive
  16. Proxy-Authenticate: abc
  17. User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10
  18. Accept-Encoding: gzip
  19. Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
  20. Cache-Control: no-cache
  21. Accept-Language: de,en;q=0.7,en-us;q=0.3
  22. `
  23. b := bufio.NewReader(strings.NewReader(rawRequest))
  24. req, err := http.ReadRequest(b)
  25. assert.Error(err).IsNil()
  26. assert.StringLiteral(req.Header.Get("Foo")).Equals("foo")
  27. assert.StringLiteral(req.Header.Get("Bar")).Equals("bar")
  28. assert.StringLiteral(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar")
  29. assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("keep-alive")
  30. assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("abc")
  31. stripHopByHopHeaders(req)
  32. assert.StringLiteral(req.Header.Get("Connection")).Equals("close")
  33. assert.StringLiteral(req.Header.Get("Foo")).Equals("")
  34. assert.StringLiteral(req.Header.Get("Bar")).Equals("")
  35. assert.StringLiteral(req.Header.Get("Proxy-Connection")).Equals("")
  36. assert.StringLiteral(req.Header.Get("Proxy-Authenticate")).Equals("")
  37. }