httpProxy_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "testing"
  28. )
  29. func TestToAbsoluteURL(t *testing.T) {
  30. var urlTests = []struct {
  31. base string
  32. relative string
  33. expected string
  34. }{
  35. {"http://example.com/path1?q=p#hash", "relative/path", "http://example.com/relative/path"},
  36. {"http://example.com/path1?q=p#hash", "relative/path?a=b", "http://example.com/relative/path?a=b"},
  37. {"http://example.com/path1?q=p#hash", "relative/path#c", "http://example.com/relative/path#c"},
  38. {"http://example.com/path1?q=p#hash", "relative/path?a=b#c", "http://example.com/relative/path?a=b#c"},
  39. {"http://example.com/path1/path2?q=p#hash", "relative/path", "http://example.com/path1/relative/path"},
  40. {"http://example.com/path1/path2?q=p#hash", "/relative/path", "http://example.com/relative/path"},
  41. {"http://example.com/path1/path2?q=p#hash", "http://example.org/absolute/path", "http://example.org/absolute/path"},
  42. }
  43. for _, tt := range urlTests {
  44. baseURL, _ := url.Parse(tt.base)
  45. absURL := toAbsoluteURL(baseURL, tt.relative)
  46. if absURL != tt.expected {
  47. t.Errorf("toAbsoluteURL(%s, %s): expected %s, actual %s", tt.base, tt.relative, tt.expected, absURL)
  48. }
  49. }
  50. }
  51. func TestProxifyURL(t *testing.T) {
  52. var urlTests = []struct {
  53. port int
  54. urlString string
  55. rewriteParams []string
  56. expected string
  57. }{
  58. {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="},
  59. {12345, "http://example.com/media/pl.aaa", []string{"rewriter1", "rewriter2"}, "http://127.0.0.1:12345/tunneled-rewrite/http%3A%2F%2Fexample.com%2Fmedia%2Fpl.aaa?rewriter1=&rewriter2="},
  60. {12346, "http://example.com/media/bbb", nil, "http://127.0.0.1:12346/tunneled/http%3A%2F%2Fexample.com%2Fmedia%2Fbbb"},
  61. }
  62. for _, tt := range urlTests {
  63. actual := proxifyURL(tt.port, tt.urlString, tt.rewriteParams)
  64. if actual != tt.expected {
  65. t.Errorf("proxifyURL(%d, %s, %v): expected %s, actual %s", tt.port, tt.urlString, tt.rewriteParams, tt.expected, actual)
  66. }
  67. }
  68. }
  69. func TestRewriteM3U8(t *testing.T) {
  70. var tests = []struct {
  71. url string
  72. contentType string
  73. inFilename string
  74. expectedFilename string
  75. expectedContentType string
  76. }{
  77. // Relying on file extension to indicate type
  78. {"http://example.com/test.m3u8", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL"},
  79. // No file extension, Content-Type set
  80. {"http://example.com/test", "application/x-mpegURL", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL"},
  81. // No file extension, Content-Type set
  82. {"http://example.com/test", "vnd.apple.mpegURL", "testdata/master.m3u8.1", "testdata/master.m3u8.1.target", "application/x-mpegURL"},
  83. // No file extension, no Content-Type, so no change
  84. {"http://example.com/test", "", "testdata/master.m3u8.1", "testdata/master.m3u8.1", ""},
  85. // Media playlist
  86. {"http://example.com/test.m3u8", "", "testdata/media.m3u8.1", "testdata/media.m3u8.1.target", "application/x-mpegURL"},
  87. // Complex master playlist
  88. {"http://example.com/test.m3u8", "", "testdata/master.m3u8.2", "testdata/master.m3u8.2.target", "application/x-mpegURL"},
  89. // Complex media playlist
  90. {"http://example.com/test.m3u8", "", "testdata/media.m3u8.2", "testdata/media.m3u8.2.target", "application/x-mpegURL"},
  91. // Invalid file
  92. {"http://example.com/test.m3u8", "application/x-mpegURL", "httpProxy.go", "httpProxy.go", ""},
  93. }
  94. for i, tt := range tests {
  95. response := http.Response{
  96. Request: new(http.Request),
  97. Header: http.Header{},
  98. }
  99. response.Request.URL, _ = url.Parse(tt.url)
  100. if tt.contentType != "" {
  101. response.Header.Set("Content-Type", tt.contentType)
  102. }
  103. inFile, _ := os.Open(tt.inFilename)
  104. inFileInfo, _ := inFile.Stat()
  105. response.Body = inFile
  106. response.Header.Set("Content-Length", strconv.FormatInt(inFileInfo.Size(), 10))
  107. err := rewriteM3U8(12345, &response)
  108. if err != nil {
  109. t.Errorf("rewriteM3U8 returned error: %s", err)
  110. }
  111. rewrittenBody, _ := ioutil.ReadAll(response.Body)
  112. response.Body.Close()
  113. expectedBody, _ := ioutil.ReadFile(tt.expectedFilename)
  114. if bytes.Compare(rewrittenBody, expectedBody) != 0 {
  115. t.Errorf("rewriteM3U8 body mismatch for test %d", i)
  116. }
  117. if tt.expectedContentType != "" && response.Header.Get("Content-Type") != tt.expectedContentType {
  118. t.Errorf("rewriteM3U8 Content-Type mismatch for test %d: %s %s", i, tt.expectedContentType, response.Header.Get("Content-Type"))
  119. }
  120. contentLength, _ := strconv.ParseInt(response.Header.Get("Content-Length"), 10, 64)
  121. if contentLength != int64(len(rewrittenBody)) {
  122. t.Errorf("rewriteM3U8 Content-Length incorrect for test %d: %d != %d", i, contentLength, len(rewrittenBody))
  123. }
  124. }
  125. }