httpProxy_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "bytes"
  22. "io/ioutil"
  23. "net/http"
  24. "net/url"
  25. "os"
  26. "strconv"
  27. "strings"
  28. "testing"
  29. )
  30. func TestToAbsoluteURL(t *testing.T) {
  31. var urlTests = []struct {
  32. base string
  33. relative string
  34. expected string
  35. }{
  36. {"http://example.com/path1?q=p#hash", "relative/path", "http://example.com/relative/path"},
  37. {"http://example.com/path1?q=p#hash", "relative/path?a=b", "http://example.com/relative/path?a=b"},
  38. {"http://example.com/path1?q=p#hash", "relative/path#c", "http://example.com/relative/path#c"},
  39. {"http://example.com/path1?q=p#hash", "relative/path?a=b#c", "http://example.com/relative/path?a=b#c"},
  40. {"http://example.com/path1/path2?q=p#hash", "relative/path", "http://example.com/path1/relative/path"},
  41. {"http://example.com/path1/path2?q=p#hash", "/relative/path", "http://example.com/relative/path"},
  42. {"http://example.com/path1/path2?q=p#hash", "http://example.org/absolute/path", "http://example.org/absolute/path"},
  43. }
  44. for _, tt := range urlTests {
  45. baseURL, _ := url.Parse(tt.base)
  46. absURL := toAbsoluteURL(baseURL, tt.relative)
  47. if absURL != tt.expected {
  48. t.Errorf("toAbsoluteURL(%s, %s): expected %s, actual %s", tt.base, tt.relative, tt.expected, absURL)
  49. }
  50. }
  51. }
  52. func TestProxifyURL(t *testing.T) {
  53. var urlTests = []struct {
  54. ip string
  55. port int
  56. urlString string
  57. rewriteParams []string
  58. expected string
  59. }{
  60. {"127.0.0.1", 1234, "http://example.com/media/pl.m3u8?q=p&p=q#hash", []string{"rewriter1"}, "http://127.0.0.1:1234/tunneled-rewrite/http%3A%2F%2Fexample.com%2Fmedia%2Fpl.m3u8%3Fq%3Dp%26p%3Dq%23hash?rewriter1="},
  61. {"127.0.0.2", 12345, "http://example.com/media/pl.aaa", []string{"rewriter1", "rewriter2"}, "http://127.0.0.2:12345/tunneled-rewrite/http%3A%2F%2Fexample.com%2Fmedia%2Fpl.aaa?rewriter1=&rewriter2="},
  62. {"127.0.0.3", 12346, "http://example.com/media/bbb", nil, "http://127.0.0.3:12346/tunneled/http%3A%2F%2Fexample.com%2Fmedia%2Fbbb"},
  63. }
  64. for _, tt := range urlTests {
  65. actual := proxifyURL(tt.ip, tt.port, tt.urlString, tt.rewriteParams)
  66. if actual != tt.expected {
  67. t.Errorf("proxifyURL(%d, %s, %v): expected %s, actual %s", tt.port, tt.urlString, tt.rewriteParams, tt.expected, actual)
  68. }
  69. }
  70. }
  71. func TestRewriteM3U8(t *testing.T) {
  72. var tests = []struct {
  73. url string
  74. contentType string
  75. contentEncoding string
  76. inFilename string
  77. expectedFilename string
  78. expectedContentType string
  79. expectError bool
  80. }{
  81. // Relying on file extension to indicate type
  82. {"http://example.com/test.m3u8", "", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL", false},
  83. // No file extension, Content-Type set
  84. {"http://example.com/test", "application/x-mpegURL", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL", false},
  85. // No file extension, Content-Type set
  86. {"http://example.com/test", "vnd.apple.mpegURL", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL", false},
  87. // No file extension, no Content-Type, so no change
  88. {"http://example.com/test", "", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1", "", false},
  89. // Media playlist
  90. {"http://example.com/test.m3u8", "", "", "testdata/media.m3u8.1", "testdata/media.m3u8.1.target", "application/x-mpegURL", false},
  91. // Complex master playlist
  92. {"http://example.com/test.m3u8", "", "", "testdata/master.m3u8.2", "testdata/master.m3u8.2.target", "application/x-mpegURL", false},
  93. // Complex media playlist
  94. {"http://example.com/test.m3u8", "", "", "testdata/media.m3u8.2", "testdata/media.m3u8.2.target", "application/x-mpegURL", false},
  95. // Invalid file
  96. {"http://example.com/test.m3u8", "application/x-mpegURL", "", "httpProxy.go", "httpProxy.go", "", false},
  97. // Gzipped file
  98. {"http://example.com/test.m3u8", "", "gzip", "testdata/master.m3u8.1.gz", "testdata/master.m3u8.1.target", "application/x-mpegURL", false},
  99. // Invalid Gzip file
  100. {"http://example.com/test.m3u8", "", "gzip", "testdata/master.m3u8.1", "", "", true},
  101. }
  102. for i, tt := range tests {
  103. response := http.Response{
  104. Request: new(http.Request),
  105. Header: http.Header{},
  106. }
  107. response.Request.URL, _ = url.Parse(tt.url)
  108. if tt.contentType != "" {
  109. response.Header.Set("Content-Type", tt.contentType)
  110. }
  111. if tt.contentEncoding != "" {
  112. response.Header.Set("Content-Encoding", tt.contentEncoding)
  113. }
  114. inFile, _ := os.Open(tt.inFilename)
  115. inFileInfo, _ := inFile.Stat()
  116. response.Body = inFile
  117. response.Header.Set("Content-Length", strconv.FormatInt(inFileInfo.Size(), 10))
  118. err := rewriteM3U8("127.0.0.1", 12345, &response)
  119. if err != nil {
  120. if !tt.expectError {
  121. t.Errorf("rewriteM3U8 returned error: %s", err)
  122. }
  123. continue
  124. }
  125. rewrittenBody, _ := ioutil.ReadAll(response.Body)
  126. response.Body.Close()
  127. expectedBody, _ := ioutil.ReadFile(tt.expectedFilename)
  128. if !bytes.Equal(rewrittenBody, expectedBody) {
  129. t.Errorf("rewriteM3U8 body mismatch for test %d", i)
  130. }
  131. if tt.expectedContentType != "" && !strings.EqualFold(response.Header.Get("Content-Type"), tt.expectedContentType) {
  132. t.Errorf("rewriteM3U8 Content-Type mismatch for test %d: %s %s", i, tt.expectedContentType, response.Header.Get("Content-Type"))
  133. }
  134. contentLength, _ := strconv.ParseInt(response.Header.Get("Content-Length"), 10, 64)
  135. if contentLength != int64(len(rewrittenBody)) {
  136. t.Errorf("rewriteM3U8 Content-Length incorrect for test %d: %d != %d", i, contentLength, len(rewrittenBody))
  137. }
  138. }
  139. }